Cod sursa(job #1524242)

Utilizator SmarandaMaria Pandele Smaranda Data 13 noiembrie 2015 18:37:54
Problema Indep Scor 25
Compilator cpp Status done
Runda Arhiva de probleme Marime 1.66 kb
#include <cstdio>
#include <vector>
#include <cstring>

using namespace std;

const int N = 501;

int dp [2][1002][1001], a [N], unu [9];

inline int cmmdc (int x, int y) {
    int r;

    while (y) {
        r = x % y;
        x = y;
        y = r;
    }
    return x;
}

void adunare (int x [], int y [], int z []) {
    int i, tr, s, lim;

    tr = 0;
    lim = max (x [0],y [0]);
    for (i = 1; i <= lim; i ++) {
        s = 0;
        if (i <= x [0])
            s = s + x [i];
        if (i <= y [0])
            s = s + y [i];
        s = s + tr;
        z [i] = s % 10;
        tr = s / 10;
    }
    z [0] = lim;
    while (tr) {
        z [++ z [0]] = tr % 10;
        tr = tr / 10;
    }
}

bool estezero (int x []) {
    if (x [0] == 1 && x [1] == 0)
        return 1;
    if (x [0] == 0)
        return 1;
    return 0;
}

int main () {
    int n, i, x, j, l1, l2;

    freopen ("indep.in", "r", stdin);
    freopen ("indep.out", "w", stdout);

    scanf ("%d", &n);
    unu [0] = unu [1] = 1;
    for (i = 1; i <= n; i ++)
        scanf ("%d", &a [i]);
    dp [1][a [1]][0] = dp [1][a [1]][1] = 1;
    for (i = 2; i <= n; i ++) {
        l1 = i % 2;
        l2 = (i - 1) % 2;
        memcpy (dp [l1], dp [l2], sizeof (dp [l2]));
        adunare (dp [l1][a [i]], unu, dp [l1][a [i]]);
        for (j = 1; j <= 1000; j ++)
            if (estezero (dp [l2][j]) == 0) {
                x = cmmdc (j, a [i]);
                adunare (dp [l1][x], dp [l2][j], dp [l1][x]);
            }
    }
    for (i = dp [n % 2][1][0]; i >= 1; i --)
        printf ("%d", dp [n % 2][1][i]);
    printf ("\n");
    return 0;
}