Pagini recente » Cod sursa (job #1608221) | Cod sursa (job #2210155) | Cod sursa (job #1883109) | Cod sursa (job #2709385) | Cod sursa (job #1370577)
#include <bits/stdc++.h>
using namespace std;
///-------------------------
const int BS = (1 << 20);
char buffer[BS];
int position = BS;
char getChar()
{
if ( position == BS )
{
position = 0;
fread(buffer, BS, 1, stdin);
}
return buffer[position++];
}
int getNr()
{
int a = 0;
char ch;
do
{
ch = getChar();
} while ( !isdigit(ch) );
do
{
a = (a << 3) + (a << 1) + ch - '0';
ch = getChar();
} while ( isdigit(ch) );
return a;
}
///-------------------------
const int Vmax = 1000000 + 1;
int sieve[Vmax];
int use[Vmax];
bool prime[Vmax];
bool valid[Vmax];
int N, M;
int main()
{
freopen("pairs.in", "r", stdin);
freopen("pairs.out", "w", stdout);
N = getNr();
for ( int i = 1; i <= N; ++i )
{
int a;
a = getNr();
use[a] = 1;
M = max(M, a);
}
for ( int i = 2; i <= M; ++i )
{
for ( int j = i + i; j <= M; j += i )
if ( use[j] )
use[i]++;
if ( prime[i] == false )
{
sieve[i] = 1;
for ( int j = i + i; j <= M; j += i )
{
prime[j] = true;
if ( valid[j] == false )
{
if ( (j / i) % i == 0 )
valid[j] = true;
sieve[j]++;
}
}
}
}
long long sol = 1LL * N * (N - 1) / 2;
for ( int i = 2; i <= M; ++i )
if ( valid[i] == false )
{
if ( sieve[i] & 1 )
sol -= 1LL * use[i] * (use[i] - 1) / 2;
else
sol += 1LL * use[i] * (use[i] - 1) / 2;
}
printf("%lld\n", sol);
return 0;
}