Pagini recente » Cod sursa (job #2594537) | Cod sursa (job #1292019) | Cod sursa (job #299971) | Cod sursa (job #688534) | Cod sursa (job #951103)
Cod sursa(job #951103)
#include<fstream>
#include<bitset>
#include<algorithm>
using namespace std;
ifstream fin("pairs.in");
ofstream fout("pairs.out");
#define maxn 1000001
bitset<maxn> ok ;
int v[maxn] ;
int N, lim ;
int sol ;
int main()
{
fin >> N ;
sol = N * ( N - 1 ) / 2 ;
while( N-- )
{
int x ;
fin >> x ;
ok[x] = 1 ;
lim = max( lim, x ) ;
}
for(int i = 2; i <= lim; ++i )
if( v[i] == 0 )
for(int j = i; j <= lim; j += i )
++v[j] ;
for(int i = 2; i * i <= lim; ++i )
for(int j = i * i; j <= lim; j += i * i )
v[j] = 0 ;
for(int i = 2; i <= lim; ++i )
{
if( v[i] )
{
int nr = 0 ;
int semn ;
for(int j = i; j <= lim; j += i )
if( ok[j] )
++nr ;
if( v[i] % 2 )
semn = -1 ;
else
semn = 1 ;
sol += semn * nr * ( nr - 1 ) / 2 ;
}
}
fout << sol ;
return 0 ;
}