Cod sursa(job #2297414)

Utilizator EdgeLordXDOvidiuPita EdgeLordXD Data 5 decembrie 2018 20:05:44
Problema Indep Scor 95
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 1.24 kb
#include <bits/stdc++.h>
#define N 500
#define M 1000
#define ll long long
#define f first
#define s second
using namespace std;
ifstream in("indep.in");
ofstream out("indep.out");
ll a[N*2+1][N];
int gcd(int a,int b){
    if(!a || !b)
        return a+b;
    if(a>b)
        return gcd(a%b,b);
    return gcd(a,b%a);
}
int main(){
    ll i,n,x,j,g;
    in>>n;
    while(n--){
        in>>x;
        for(i=1; i<=N*2; ++i){
            if(a[i][0]){
                g=gcd(x,i);
                for(j=1; j<=max(a[g][0], a[i][0]); ++j)
                    a[g][j]+=a[i][j];
                j=1;
                while((j<=a[g][0]) || (j>a[g][0] && a[g][j])){
                    a[g][j+1]+=a[g][j]/M;
                    a[g][j++]%=M;
                }
                a[g][0]=j-1;
            }
        }
        ++a[x][1];
        j=1;
        while((j<=a[x][0]) || (j>a[x][0] && a[x][j])){
            a[x][j+1]+=a[x][j]/M;
            a[x][j++]%=M;
        }
        a[x][0]=j-1;
    }
    if(!a[1][0]) out<<0;
    else{
        out<<a[1][a[1][0]];
        for(i=a[1][0]-1; i>=1; --i){
            for(j=M/10; j!=0 && a[1][i]/j==0; j/=10)
                out<<0;
            out<<a[1][i];
        }
    }
    return 0;
}