Cod sursa(job #1713574)

Utilizator killer301Ioan Andrei Nicolae killer301 Data 5 iunie 2016 23:11:05
Problema Indep Scor 10
Compilator cpp Status done
Runda Arhiva de probleme Marime 6.04 kb
#include <cstdio>
#include <cmath>
#include <cstring>
#include <algorithm>

using namespace std;

const int NMAX = 500;
const int CIFMAX = 300;

int a[NMAX+5];
int primes[] = {2, 3, 5, 7, 11, 13, 17, 19, 23, 27, 29, 31, 37, 41};

int p2[NMAX+5][CIFMAX];
int Ans[NMAX+5];

int main()
{
    freopen("indep.in", "r", stdin);
    freopen("indep.out", "w", stdout);
    int n;
    scanf("%d", &n);
    int Max = -1;
    for(int i=0; i<n; i++)
    {
        scanf("%d", &a[i]);
        Max = max(Max, a[i]);
    }
    p2[0][0] = 1;
    for(int i=1; i<=n; i++)
    {
        p2[i][0] = p2[i-1][0];
        int c = 0;
        for(int j=1; j<=p2[i][0]; j++)
        {
            p2[i][j] = p2[i-1][j]*2+c;
            c = p2[i][j]/10;
            p2[i][j]%=10;
        }
        if(c)p2[i][++p2[i][0]] = 1;
        p2[i][1]++;
    }
    Ans[0] = 1;
    for(int T=2; T<=Max; T++)
    {
        int lim = (int)sqrt(1.0*T);
        int pos = 0, e = 0;
        int c = T;
        bool ok = true;
        while(primes[pos]<=lim)
        {
            if(c%primes[pos] == 0)
            {
                c/=primes[pos];
                e++;
            }
            if(c%primes[pos] == 0)
            {
                ok = false;
                break;
            }
            pos++;
        }
        if(c>1)e++;
        if(!ok)continue;
        int nr = 0;
        for(int i=0; i<n; i++)
            if(a[i]%T == 0)
                nr++;
        if(e%2==1)
        {
            Ans[0] = max(Ans[0], p2[nr][0]);
            int c = 0;
            for(int i=1; i<=Ans[0]; i++)
            {
                Ans[i] = Ans[i]+p2[nr][i]+c;
                c=Ans[i]/10;
                Ans[i]%=10;
            }
            if(c)Ans[++Ans[0]] = 1;
        }
        else
        {
            int c = 0;
            for(int i=1; i<=Ans[0]; i++)
            {
                Ans[i] = Ans[i]-p2[nr][i]-c;
                if(Ans[i]<0)
                {
                    Ans[i]+=10;
                    c = 1;
                }
            }
            while(Ans[0] > 1 && Ans[Ans[0]] == 0)Ans[0]--;
        }
    }
    int c = 0;
    for(int i=1; i<=p2[n][0]; i++)
    {
        p2[n][i] = p2[n][i]-Ans[i]-c;
        if(p2[n][i]<0)
        {
            p2[n][i]+=10;
            c = 1;
        }
    }
    while(p2[n][0] > 1 && p2[n][p2[n][0]] == 0)p2[n][0]--;
    for(int i=p2[n][0]; i>0; i--)
        printf("%d", p2[n][i]);
    printf("\n");
    return 0;
}



//#include <cstdio>
//#include <cmath>
//#include <cstring>
//#include <algorithm>
//
//using namespace std;
//
//const int NMAX = 500;
//const int CIFMAX = 300;
//
//int a[NMAX+5];
//int primes[] = {2, 3, 5, 7, 11, 13, 17, 19, 23, 27, 29, 31, 37, 41};
//
//class HN
//{
//private:
//    int n;
//    int a[CIFMAX];
//public:
//    HN()
//    {
//        n = 1;
//        memset(a, 0, sizeof(a));
//    }
//    HN(int x)
//    {
//        n = 0;
//        memset(a, 0, sizeof(a));
//        do
//        {
//            a[n++] = x%10;
//            x/=10;
//        }
//        while(x);
//    }
//    HN &operator + (const HN &other)
//    {
//        HN ans;
//        ans.n = max(n, other.n);
//        int c = 0;
//        for(int i=0; i<ans.n; i++)
//        {
//            ans.a[i] = a[i]+other.a[i]+c;
//            c = ans.a[i]/10;
//            ans.a[i]%=10;
//        }
//        if(c) ans.a[ans.n++] = c;
//        return ans;
//    }
//    HN &operator - (const HN &other)
//    {
//        HN ans;
//        ans.n = n;
//        int c = 0;
//        for(int i=0; i<n; i++)
//        {
//            ans.a[i] = a[i]-other.a[i]-c;
//            if(ans.a[i] < 0)
//            {
//                c=1;ans.a[i]+=10;
//            }
//        }
//        while(ans.n>1 && ans.a[ans.n-1] == 0)ans.n--;
//        return ans;
//    }
//    HN operator * (const HN &other)
//    {
//        HN ans;
//        int m = other.n;
//        for(int i=0; i<n; i++)
//        {
//            for(int j=0; j<m; j++)
//            {
//                ans.a[i+j] += a[i]*other.a[j];
//            }
//        }
//        int c = 0;
//        for(int i=0; i<n+m-1; i++)
//        {
//            ans.a[i]+=c;
//            c = ans.a[i]/10;
//            ans.a[i]%=10;
//        }
//        ans.n = n+m-1;
//        if(c)
//        {
//            ans.a[ans.n++] = c%10;
//            c/=10;
//        }
//        if(c)ans.a[ans.n++] = c;
//        return ans;
//    }
//    void print()
//    {
//        for(int i=n-1; i>=0; i--)
//            printf("%d", a[i]);
//        printf("\n");
//    }
//};
//
//HN p2[NMAX+5];
//
//int main()
//{
//    freopen("indep.in", "r", stdin);
//    freopen("indep.out", "w", stdout);
//    int n;
//    scanf("%d", &n);
//    int Max = -1;
//    for(int i=0; i<n; i++)
//    {
//        scanf("%d", &a[i]);
//        Max = max(Max, a[i]);
//    }
//    HN p = 1;
//    for(int i=0; i<=n; i++)
//    {
//        p2[i] = p;
//        p = p*(HN)2;
//    }
//    HN Ans;
//    for(int i=2; i<=Max; i++)
//    {
//        int lim = (int)sqrt(1.0*i);
//        int pos = 0, e = 0;
//        int c = i;
//        bool ok = true;
//        while(primes[pos]<=lim)
//        {
//            if(c%primes[pos] == 0)
//            {
//                c/=primes[pos];
//                e++;
//            }
//            if(c%primes[pos] == 0)
//            {
//                ok = false;
//                break;
//            }
//            pos++;
//        }
//        if(!ok)continue;
//        int nr = 0;
//        for(int j=0; j<n; j++)
//            if(a[j]%i == 0)
//                nr++;
//        if(c>1)e++;
//        if(e%2==1)
//        {
//            Ans = Ans + p2[nr];
//            Ans = Ans - (HN)1;
//        }
//        else
//        {
//            Ans = Ans - p2[nr];
//            Ans = Ans + (HN)1;
//        }
//    }
//    Ans = p2[n] - Ans;
//    Ans = Ans - 1;
//    Ans.print();
//    return 0;
//}