CF 1900D Small GCD
nynkqh_yzq · · 题解
CF 1900D Small GCD
题意:给定一个
其中
数据范围:
前置芝士(欧拉反演):
首先,反正都是枚举三个数,那么顺序并不重要,所以我们可以把
看到
既然
那么转换一下,则有:
此时,后面的
单次时间复杂度为
好了,直接上代码:
#include <bits/stdc++.h>
#define int long long
using namespace std;
const int N = 1e5 + 10;
int phi[N], cnt[N], a[N], p[N];
bool zs[N];
int n, t, tot, ans;
vector<int> prime;
int split(int x) {
tot = 0; int xx = sqrt(x), res = 0;
for (int i = 1; i <= xx; i ++) {
if (!(x % i)) {
res += phi[i] * cnt[i], cnt[i] ++; //分解因数,顺便求贡献
if (i * i != x) res += phi[x / i] * cnt[x / i], cnt[x / i] ++; //同理
}
}
return res;
}
void get_phi() {
phi[1] = 1;
for (int i = 2; i <= N - 5; i ++) {
if (!zs[i]) zs[i] = true, prime.push_back(i), phi[i] = i - 1;
for (int j = 0; j < prime.size() && prime[j] * i <= N - 5; j ++) {
auto h = prime[j]; zs[i * h] = true;
if (!(i % h)) { phi[i * h] = phi[i] * h; break; }
phi[i * h] = phi[i] * phi[h];
}
}
return ;
}
signed main() {
ios::sync_with_stdio(false), cin.tie(0), cout.tie(0);
cin >> t; get_phi(); //线性筛求欧拉函数
while (t --) {
cin >> n; ans = 0;
for (int i = 1; i <= N - 5; i ++) cnt[i] = 0;
for (int i = 1; i <= n; i ++) cin >> a[i];
sort(a + 1, a + n + 1);
for (int i = 1; i < n; i ++) {
int h = split(a[i]);
ans += h * (n - i); //三元组中的最大的一个数可以任选,故乘上后面的数的个数
}
cout << ans << '\n';
}
return 0;
}