Pagini recente » Cod sursa (job #563192) | Cod sursa (job #2272581) | Cod sursa (job #2797807) | Cod sursa (job #1702326) | Cod sursa (job #918581)
Cod sursa(job #918581)
#include <fstream>
#include <iostream>
#include <cmath>
#include <vector>
#define pb push_back
#define N 1000000
using namespace std;
bool neprim[N+2];
vector<int>divs;
int card, n, i, x, m, half, conf, prod, j, p, nr[N+2], primes[N+2];
long long rez;
void ciur()
{
int i, j;
for(i = 2; i <= N; i++)
if(!neprim[i])
{
primes[++p] = i;
for(j = 2*i; j <= N; j += i) neprim[j] = 1;
}
}
int main()
{
ifstream fi("pairs.in");
ofstream fo("pairs.out");
fi >> n;
ciur();
for(i = 1; i <= n; i++)
{
fi >> x;
divs.clear();
half = sqrt(double(x));
for(j = 1; j <= p and primes[j] <= half and x; j++)
{
if(x%primes[j] == 0) divs.pb(primes[j]);
while(x%primes[j] == 0) x /= primes[j];
}
if(x != 1) divs.pb(x);
m = divs.size();
rez += i-1;
for(conf = 1; conf < (1<<m); ++conf)
{
prod = 1;
for(j = 0, card = 0; j < m; j++)
if(((1<<j) & conf) > 0)
{
prod *= divs[j];
card++;
}
if(card % 2) rez -= 1LL*nr[prod]; else rez += 1LL*nr[prod];
nr[prod]++;
}
}
fo << rez << "\n";
return 0;
}