Cod sursa(job #1419644)

Utilizator chiriacandrei25Chiriac Andrei chiriacandrei25 Data 16 aprilie 2015 00:47:11
Problema Pairs Scor 40
Compilator cpp Status done
Runda Arhiva de probleme Marime 1.29 kb
#include <cstdio>
#define ValMax 1000005

using namespace std;

bool fr[ValMax];
int cnt[ValMax],a[100005],v[100],len;

inline void Desc(int x)
{
    int d=3,k=0;
    len=0;
    while(x%2==0)
    {
        ++k;
        x/=2;
    }
    if(k) v[++len]=2;
    while(x>1 && d*d<=x)
    {
        k=0;
        while(x%d==0)
        {
            ++k;
            x/=d;
        }
        if(k) v[++len]=d;
        d+=2;
    }
    if(x>1) v[++len]=x;
}

int main()
{
    int n,i,j,x,nr,produs,stare;
    long long sol=0;
    freopen ("pairs.in","r",stdin);
    freopen ("pairs.out","w",stdout);
    scanf("%d", &n);
    for(i=1;i<=n;++i)
    {
        scanf("%d", &a[i]); fr[a[i]]=true;
    }
    for(i=1;i<=1000000;++i)
        for(j=i;j<=1000000;j+=i)
            if(fr[j]) ++cnt[i];
    for(i=1;i<=n;++i)
    {
        Desc(a[i]);
        for(stare=1;stare<(1<<len);++stare)
        {
            produs=1;
            for(j=nr=0;j<len;++j)
                if((1<<j)&stare)
                {
                    ++nr;
                    produs=produs*v[j+1];
                }
            if(nr&1) sol+=cnt[produs]-1;
            else sol-=cnt[produs]-1;
        }
    }
    printf("%lld\n", 1LL*n*(n-1)/2-sol/2);
    //printf("%lld\n", sol);
    return 0;
}