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;
}