Cod sursa(job #3220274)

Utilizator Radu_MocanasuMocanasu Radu Radu_Mocanasu Data 2 aprilie 2024 23:48:39
Problema Pairs Scor 0
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 1.29 kb
#include <bits/stdc++.h>

using namespace std;
ifstream fin("pairs.in");
ofstream fout("pairs.out");
bitset <1000005> c;
vector <int> fp[1000005];
long long rez;
int f[1000002];
void ciur(int n){
    c[0] = c[1] = 1;
    fp[2].push_back(2);
    for(int i = 4; i <= n; i += 2){
        fp[i].push_back(2);
        c[i] = 1;
    }
    for(int i = 3; i <= n; i++){
        if(!c[i]){
            fp[i].push_back(i);
            for(int j = 2 * i; j <= n; j += i){
                fp[j].push_back(i);
                c[j] = 1;
            }
        }
    }
}
void solve(int n){
    int t = fp[n].size();
    for(int e = 1; e < (1 << t); e++){
        int p = 1,nrb = 0;
        for(int i = 0; i < t; i++){
            if((1 << i) & e){
                p *= fp[n][i];
                nrb++;
            }
        }
        if(nrb & 1) rez -= f[p];
        else rez += f[p];
    }
}

int main()
{
    int n,i,x;
    fin >> n;
    ciur(1e6);
    rez = n * (n - 1) / 2;
    for(i = 1; i <= n; i++){
        fin >> x;
        solve(x);
        int d = 1;
        for(d = 1; d * d < x; d++){
            if(x % d == 0){
                f[d]++;
                f[x / d]++;
            }
        }
        if(d * d == x) f[d]++;
    }
    fout << rez;
    return 0;
}