Cod sursa(job #1524239)

Utilizator SmarandaMaria Pandele Smaranda Data 13 noiembrie 2015 18:35:32
Problema Indep Scor 40
Compilator cpp Status done
Runda Arhiva de probleme Marime 1.61 kb
#include <cstdio>
#include <vector>
#include <cstring>

using namespace std;

const int N = 501;

int dp [N][1002][101], a [N], unu [101];

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;

    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 ++) {
        memcpy (dp [i], dp [i - 1], sizeof (dp [i - 1]));
        adunare (dp [i][a [i]], unu, dp [i][a [i]]);
        for (j = 1; j <= 1000; j ++)
            if (estezero (dp [i - 1][j]) == 0) {
                x = cmmdc (j, a [i]);
                adunare (dp [i][x], dp [i - 1][j], dp [i][x]);
            }
    }
    for (i = dp [n][1][0]; i >= 1; i --)
        printf ("%d", dp [n][1][i]);
    printf ("\n");
    return 0;
}