Cod sursa(job #2513617)

Utilizator NicolaalexandraNicola Alexandra Mihaela Nicolaalexandra Data 23 decembrie 2019 15:27:45
Problema Indep Scor 95
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 2.94 kb
#include <bits/stdc++.h>
#define DIM 1010
using namespace std;

ifstream fin ("indep.in");
ofstream fout ("indep.out");
int sol[DIM],p2[DIM][DIM],v[DIM],aux[DIM],a[DIM],b[DIM];
pair <int,int> w[DIM];
int n,i,j,k,k2,maxi;
void inmultire (int a[], int b){
    int t = 0;
    for (int i=1;i<=a[0];i++){
        a[i] = a[i]*b + t;
        t = a[i]/10;
        a[i] %= 10;
    }
    while (t){
        a[++a[0]] = t%10;
        t /= 10;
    }
}
void scadere1 (int a[]){
    int i = 1;
    while (i <= a[0] && a[i] == 0){
        a[i] = 9;
        i++;
    }
    a[i]--;
    while (a[0] >= 1 && a[a[0]] == 0)
        a[0]--;
}
void adunare (int a[], int b[], int c[]){
    int m;
    if (a[0] > b[0]){
        m = a[0];
        for (int i=b[0]+1;i<=m;i++)
            b[i] = 0;
    } else {
        m = b[0];
        for (int i=a[0]+1;i<=m;i++)
            a[i] = 0;
    }
    int t = 0;
    for (int i=1;i<=m;i++){
        c[i] = a[i] + b[i] + t;
        t = c[i]/10;
        c[i] %= 10;
    }
    c[0] = m;
    while (t){
        c[++c[0]] = t%10;
        t /= 10;
    }
}
void scadere (int a[], int b[]){
    for (int i=b[0]+1;i<=a[0];i++)
        b[i] = 0;
    int t = 0;
    for (int i=1;i<=a[0];i++){
        a[i] -= (b[i]+t);
        if (a[i] < 0){
            t = 1;
            a[i] += 10;
        }
        else t = 0;
    }
    while (a[0] >= 1 && !a[a[0]])
        a[0]--;
}
void _copy (int a[], int b[]){
    for (int i=0;i<=b[0];i++)
        a[i] = b[i];
}
int main (){

    fin>>n;
    for (i=1;i<=n;i++){
        fin>>v[i];
        maxi = max (maxi,v[i]);
    }
    sol[0] = sol[1] = 1;
    p2[0][0] = 1, p2[0][1] = 0;
    for (i=1;i<=n;i++){
        inmultire(sol,2);
        for (j=0;j<=sol[0];j++)
            p2[i][j] = sol[j];
        scadere1 (p2[i]);
    }
    scadere1 (sol);

    for (i=2;i<=maxi;i++){
        int nr = i, d = 2, ap = 0, val = 1;
        while (nr != 1 && d*d <= nr){
            int e = 0;
            while (nr % d == 0){
                nr /= d;
                e++;
            }
            if (e){
                val *= d;
                ap++;
            }
            d++;
        }
        if (nr != 1)
            ap++, val *= nr;
        if (val > 1)
            w[++k] = make_pair (val,ap); /// prod factorilor primi si nr lor

    }
    sort (w+1,w+k+1);
    k2 = 1;
    for (i=2;i<=k;i++){
        if (w[i].first != w[i-1].first)
            w[++k2] = w[i];
    }
    k = k2;

    for (i=1;i<=k;i++){
        int x = w[i].first, nr = 0;
        for (j=1;j<=n;j++){
            if (v[j] % x == 0)
                nr++;
        }

        if (w[i].second % 2 == 0)
            adunare (a,p2[nr],a);
        else adunare (b,p2[nr],b);

    }
    adunare (sol,a,sol);
    scadere (sol,b);

    if (sol[0] == 0){
        fout<<0;
        return 0;
    }
    for (i=sol[0];i;i--)
        fout<<sol[i];

    return 0;
}