Cod sursa(job #3225361)

Utilizator and_Turcu Andrei and_ Data 17 aprilie 2024 13:48:06
Problema Pairs Scor 100
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 0.63 kb
#include <bits/stdc++.h>
#define int long long
using namespace std;
ifstream fin("pairs.in");ofstream fout("pairs.out");

const int N=1000009;
int n,fr[N],ans[N];



int32_t main()
{
    fin >> n;
    int ma=0;
    for(int i=1;i<=n;i++)
    {
        int x;
        fin >> x;
        fr[ x ]++;
        ma=max(ma,x);
    }
    for(int cm=ma;cm>=1;cm--) /// nr perechi de elem cu cmmdc   =   cm
    {
        for(int i=cm;i<=ma;i+=cm)
            ans[cm]+=fr[i];  /// nr elemente

        ans[cm] = ans[cm]*(ans[cm]-1)/2; /// nr perechi
        for(int i=2*cm;i<=ma;i+=cm)
            ans[cm]-=ans[i];
    }
    fout << ans[1];
    return 0;
}