Cod sursa(job #2677085)

Utilizator stefdascalescuStefan Dascalescu stefdascalescu Data 25 noiembrie 2020 19:34:28
Problema Pairs Scor 10
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 0.92 kb
#include<bits/stdc++.h>
#define god dimasi5eks
#pragma GCC optimize("O3")
#define fi first
#define se second
#define pb push_back
#define pf push_front

#define fisier 1
 
using namespace std;
 
typedef long long ll;
 
const int mod = 1000000007;
const double dancila = 3.14159265359; // PI 
const double eps = 1e-9;
 
int n, fr[1000002], val, maxval;

int cnt[1000002];

ll ans;


int main()
{
	#ifdef fisier
		ifstream cin("pairs.in");
		ofstream cout("pairs.out");
	#endif
	
	ios_base::sync_with_stdio(false);
	cin.tie(NULL);
	
	cin >> n;
	for(int i = 1; i <= n; ++i)
	{
		cin >> val;
		fr[val]++;
		maxval = max(maxval, val);
	}
	ans = 1LL * n * (n-1) / 2;
	
	for(int i = maxval; i >= 2; --i)
	{
		val = 0;
		for(int j = i; j <= maxval; j += i)
		{
			val += fr[j];
			cnt[i] -= cnt[j];
		}
		cnt[i] += 1LL * val * (val-1) / 2;
		ans -= cnt[i];
	}
	
	cout << ans;
	return 0;
}