Cod sursa(job #1753131)

Utilizator Bodo171Bogdan Pop Bodo171 Data 5 septembrie 2016 21:41:18
Problema Indep Scor 25
Compilator cpp Status done
Runda Arhiva de probleme Marime 1.55 kb
#include <iostream>
#include<fstream>
using namespace std;
const unsigned int base=1e9;
const unsigned int nmax=1000;
unsigned int n,i,x,modes[1005][55],c,nr,zeros,j,id,unu[3],a,b,cpy[55];
unsigned int cmmdc(unsigned int x,unsigned int y)
{
    a=x,b=y;
    if(a<b) swap(a,b);
    while(b!=0)
    {
        c=a%b;
        a=b;
        b=c;
    }
    return a;
}
void add(unsigned int A[],unsigned int B[])
{
    if(B[0]>A[0]) A[0]=B[0];
    for(id=1;id<=A[0];id++)
    {
        A[id]+=B[id];
        if(A[id]>=base)
        {
            A[id]-=base;
            A[id+1]++;
            if(id+1>A[0]) A[0]++;
        }
    }
}
int main()
{
    ifstream f("indep.in");
    ofstream g("indep.out");
    f>>n;
    unu[0]=1;
    unu[1]=1;
    for(i=1;i<=n;i++)
    {
        f>>x;
        for(j=1;j<=nmax;j++)
        {
            nr=cmmdc(j,x);
            if(modes[j][0]!=0)
            {
                if(nr!=j)add(modes[nr],modes[j]);
                else
                {
                    for(id=0;id<=modes[j][0];id++)
                        cpy[id]=modes[j][id];
                    add(modes[nr],cpy);
                }
            }
        }
        add(modes[x],unu);
    }
    for(i=modes[1][0];i>=1;i--)
    {
         if(i!=modes[1][0])
        {
            x=base;
            zeros=0;
            while(x>modes[1][i])
                x/=10,zeros++;
            zeros--;
        }
        for(j=1;j<=zeros;j++) g<<'0';
        g<<modes[1][i];
    }
    if(modes[1][0]==0) g<<'0';
    return 0;
}