- 问题转化为C(n, 4) - num
- 发现num = sum (-mu[i]) * C(cnt[i], 4)
- 这时候就可以预处理mu以及C来求解,cnt[i]可以刷表法递推
(填表:由谁来,刷表:到谁去)
#include <bits/stdc++.h>
using namespace std;
const int N = 1e4 + 10;
#define lowbit(x) (x & (-x))
#define endl '\n'
#define int long long
#define pii pair<int, int>
#define mkp make_pair
#define LL long long
int n, k, m, mu[N], p[N], tot, C[N], w[N], a[N];
bool flg[N];
void getMu() {
mu[1] = 1;
for (int i = 2; i < N; ++i) {
if (!flg[i]) p[++tot] = i, mu[i] = -1;
for (int j = 1; j <= tot && i * p[j] < N; ++j) {
flg[i * p[j]] = 1;
if (i % p[j] == 0) {
mu[i * p[j]] = 0;
break;
}
mu[i * p[j]] = -mu[i];
}
}
}
void solve()
{
for (int i = 1; i <= n; i++) cin >> a[i], w[a[i]]++;
LL num = 0;
for (int i = 1; i <= 1e4; i++) {
for (int j = i * 2; j <= 1e4; j += i) {
w[i] += w[j];
// cout << j << ' ' << w[i]<< endl;
}
}
// cout << w[1] << endl;
for (int i = 1; i <= 1e4; i++) {
if (w[i] >= 4) num += mu[i] * C[w[i]];
}
for (int i = 0; i <= 1e4; i++) w[i] = 0;
cout << num << endl;
}
signed main()
{
ios::sync_with_stdio(false);
cin.tie(nullptr);
getMu();
for (int i = 4; i <= 10000; i++) {
C[i] = i * (i - 1) * (i - 2) * (i - 3) / (2 * 3 * 4);
}
int T = 1;
// cin >> T;
while (cin >> n) {
solve();
}
return 0;
}
标签:int,long,mu,++,num,P2714,define
From: https://www.cnblogs.com/lyrrr/p/18619909