Cod sursa(job #2949689)

Utilizator hhhhhhhAndrei Boaca hhhhhhh Data 1 decembrie 2022 14:15:54
Problema Pairs Scor 100
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 1.15 kb
#include <bits/stdc++.h>
#pragma GCC optimize("O3")
using namespace std;
ifstream fin("pairs.in");
ofstream fout("pairs.out");
long long ans;
int f[1000001],n,x,aux,d,factori[11],lgfact,nr;
int main()
{
    ios_base::sync_with_stdio(false);
    fin.tie(0);
    fout.tie(0);
    fin>>n;
    for(int i=1;i<=n;i++)
    {
        fin>>x;
        lgfact=0;
        d=2;
        while(x!=1)
        {
            if(x%d==0)
            {
                factori[++lgfact]=d;
                while(x%d==0)
                    x/=d;
            }
            d++;
            if(d%2==0)
                d++;
            if(d*d>x&&x>1)
            {
                factori[++lgfact]=x;
                break;
            }
        }
        nr=0;
        for(int j=1;j<(1<<lgfact);j++)
        {
            int a=1;
            int x=1;
            for(int l=0;l<lgfact;l++)
                if((j>>l)&1)
                {
                    a=a*factori[l+1];
                    x*=-1;
                }
                nr=nr+f[a]*x;
                f[a]++;
        }
        ans+=i-1+nr;
    }
    fout<<ans;
    return 0;
}