Cod sursa(job #2506776)

Utilizator victoreVictor Popa victore Data 8 decembrie 2019 19:22:43
Problema Pairs Scor 0
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 1.44 kb
//check check check
#include<iostream>
#include<vector>
#include<algorithm>
#include<fstream>
#include<queue>
#include<cstring>
#include<map>
#include<iomanip>
#include<set>
#include<unordered_map>

#define ll long long
#define pb(x) push_back(x)

using namespace std;

typedef pair<int,int> ii;

const int NMAX = 1e5+5;
const int VALMAX = 1e6+5;

ifstream fin("pairs.in");
ofstream fout("pairs.out");

int v[NMAX],cntdiv[VALMAX],cnt[VALMAX];
bool viz[VALMAX];

unordered_map<int,bool> mp;

int main()
{
    int N,i,j,maxim = 1;

    fin>>N;
    ll rez = 1LL*N*(1LL*N-1)/2;
    for(i = 1 ; i <= N ; ++i)
    {
        fin>>v[i];
        mp[v[i]] = 1;
        maxim = max(maxim , v[i]);
    }

    //cnt[i] = cate numere din M sunt divizibile cu i
    for(i = 2 ; i <= N ; ++i)
    {
        if(cntdiv[i] == 0)
        {
            for(j = i ; j <= maxim ; j+= i)
                cntdiv[i]++;

            for(j = i*i ; j <= maxim ; j += i*i)
                viz[j] = 1;
        }

        if(viz[i] == 0)
        {
            for(j = i ; j <= maxim ; j += i)
            {
                if(mp[j])
                    cnt[i]++;
            }
            if(cntdiv[i]&1)
            {
                rez -= 1LL*cnt[i]*(1LL*cnt[i]-1)/2;
            }
            else
            {
                rez += 1LL*cnt[i]*(1LL*cnt[i]-1)/2;
            }
        }
    }

    fout<<rez;

    return 0;
}