首页 > 其他分享 >P2714

P2714

时间:2024-12-20 20:11:23浏览次数:3  
标签:int long mu ++ num P2714 define

  1. 问题转化为C(n, 4) - num
  2. 发现num = sum (-mu[i]) * C(cnt[i], 4)
  3. 这时候就可以预处理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

相关文章

  • P2714 循环不变式的基础应用
    你听说过循环不变式吗?不妨来品鉴一下吧:WeLikeStudying大佬的博客:循环不变式而这篇文章,只是对大佬博客的小小注解,外加一点实际应用。我们可以把循环不变式理解成一组条件。在每次循环中,我们保证这组条件均为真。而最后循环就可以得出我们想要的结果。如果思考本质,这实际上......