这里是一篇比较离谱的暴力题解。
题目传送门:Three Chairs
我们首先把 数组从小到大排序,然后问题就变成了求三元组 (,) 的个数。
考虑枚举 ,然后每次就是求有多少对二元组 满足 ,,。
可以考虑每次把 往右移一位,考虑对数 会发生什么变化。
如果我们设 表示第 个数之前与 互质的数的个数, 表示第 个数之后与 互质的数的个数。
明显每次从 变成 , 就要加上 。
然后问题就变成了如何求 和 ,这里以 为例, 同理。
我们可以考虑直接容斥,由于 ,而 ,所以 的不同的质因子个数最多只有 个。
所以我们开一个桶 , 表示当前多少数能够被 整除,假设 。
设
然后实现好一点就可以过了,时间复杂度 。
#include <bits/stdc++.h>
using namespace std;
using i64 = long long;
int n, a[300005];
int cnt[300005], pre[300005], suc[300005], mul[ 1 << 6 ];
int main() {
#ifndef ONLINE_JUDGE
freopen("code.in", "r", stdin);
#endif
ios :: sync_with_stdio(0), cin.tie(0);
cin >> n;
for (int i = 1; i <= n; ++i) {
cin >> a[i];
}
sort(a + 1, a + n + 1);
memset(cnt, 0, sizeof(cnt));
for (int i = 1; i <= n; ++i) {
int now = a[i];
vector <int> fac;
for (int j = 2; j * j <= now; ++j) {
if (now % j == 0) {
fac.push_back(j);
while (now % j == 0) now /= j;
}
}
if (now > 1) fac.push_back(now);
int m = fac.size(), ans = cnt[1];
mul[0] = 1, ++cnt[1];
for (int s = 1; s < 1 << m; ++s) {
int t = __builtin_ctz(s), b = __builtin_popcount(s);
mul[s] = mul[ s ^ 1 << t ] * fac[t];
if (b & 1) ans -= cnt[ mul[s] ];
else ans += cnt[ mul[s] ];
++cnt[ mul[s] ];
}
pre[i] = ans;
}
memset(cnt, 0, sizeof(cnt));
for (int i = n; i; --i) {
int now = a[i];
vector <int> fac;
for (int j = 2; j * j <= now; ++j) {
if (now % j == 0) {
fac.push_back(j);
while (now % j == 0) now /= j;
}
}
if (now > 1) fac.push_back(now);
int m = fac.size(), ans = cnt[1];
mul[0] = 1, ++cnt[1];
for (int s = 1; s < 1 << m; ++s) {
int t = __builtin_ctz(s), b = __builtin_popcount(s);
mul[s] = mul[ s ^ 1 << t ] * fac[t];
if (b & 1) ans -= cnt[ mul[s] ];
else ans += cnt[ mul[s] ];
++cnt[ mul[s] ];
}
suc[i] = ans;
}
i64 ans = 0, now = 0;
for (int i = 2; i < n; ++i) {
now += suc[ i - 1 ] - pre[i];
ans += now;
}
cout << ans << '\n';
return 0;
}