Pagini recente » Cod sursa (job #1857563) | Cod sursa (job #2351187) | Cod sursa (job #2352659) | Cod sursa (job #2904338) | Cod sursa (job #3306553)
#include <bits/stdc++.h>
using namespace std;
ifstream fin("pairs.in");
ofstream fout("pairs.out");
const int NMAX = 100001;
const int VMAX = 1e6 + 1;
int n;
vector<int> cnt(VMAX, 0);
vector<int> freq(VMAX, 0);
vector<int> dp(VMAX, 0);
signed main()
{
fin >> n;
int vmax = -1;
for(int i = 1; i <= n; i++)
{
int x;
fin >> x;
vmax = max(vmax, x);
freq[x]++;
}
for(int i = 1; i <= vmax; i++)
{
int dub_pairs = 0;
for(int j = i; j <= vmax; j += i)
{
cnt[i] += freq[j];
if(freq[j] >= 2)
{
dub_pairs -= freq[j] * (freq[j] - 1) / 2;
}
}
dp[i] = cnt[i] * (cnt[i] - 1) / 2 - dub_pairs;
// cout << i << ": " << cnt[i] << "\n";
}
for(int d = vmax; d >= 1; d--)
{
int total = dp[d];
for(int i = 2 * d; i <= vmax; i += d)
{
total -= dp[i];
}
dp[d] = total;
}
fout << dp[1];
return 0;
}