Cod sursa(job #3293179)

Utilizator nistor_dora_valentinaNistor Dora Valentina nistor_dora_valentina Data 10 aprilie 2025 16:13:12
Problema Indep Scor 0
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 1.26 kb
#include <fstream>
#define int long long
using namespace std;
ifstream fin("indep.in");
ofstream fout("indep.out");
int n, i, a[505], dp[1005], sum[505][1005], j, mobius[1005], ans;
int cmmdc(int a, int b)
{
    int rest=0;
    while(b)
    {
        rest=a%b;
        a=b;
        b=rest;
    }
    return a;
}
signed main()
{
    fin>>n;
    for(i=1; i<=n; i++)
    {
        fin>>a[i];
        for(j=1; j<=1000; j++)
            if(a[i]%j==0)
                sum[i][j]=sum[i-1][j]+1;
            else
                sum[i][j]=sum[i-1][j];
    }
    for(i=1; i<=n; i++)
        for(j=i+1; j<=n; j++)
        {
            int c=cmmdc(a[i], a[j]);
            for(int k=1; k*k<=c; k++)
                if(c%k==0)
                {
                    int nr=sum[j-1][k]-sum[i][k];
                    dp[k]+=(1<<nr);
                    nr=sum[j-1][c/k]-sum[i][c/k];
                    dp[c/k]+=(1<<nr);
                }
        }
    mobius[1]=-1;
    for(i=1; i<=1000; i++)
        if(mobius[i])
        {
            mobius[i]=-mobius[i];
            for(j=2*i; j<=1000; j+=i)
                mobius[j]+=mobius[i];
        }
    for(i=1; i<=1000; i++)
        ans=ans+dp[i]*mobius[i];
    fout<<(1<<n)-ans-1;
    return 0;
}