Three Chairs
Fido_Puppy · · 题解
这里是一篇比较离谱的暴力题解。
题目传送门: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;
}