J. Product of GCDs(莫比乌斯反演)(2021牛客暑期多校训练营2)

本文探讨了如何利用欧拉函数φ(n)和质因数分解技巧简化Product of GCDs的计算,通过筛选质数并运用φ值,优化了整体复杂度至O(n log² n)。通过实例展示了如何在C++代码中实现这一算法,涉及到了欧拉函数计算、质数判断和组合计数。
摘要由CSDN通过智能技术生成

Product of GCDs

∏ d = 1 n d ∑ [ gcd ⁡ ( s 1 d , s 2 d , … , s k d ) = 1 ] ∏ d = 1 n d ∑ i = 1 n d μ ( i ) C f [ i d ] k \prod_{d = 1} ^{n} d ^{\sum[\gcd(\frac{s_1}{d}, \frac{s_2}{d}, \dots, \frac{s_k}{d}) = 1]}\\ \prod_{d = 1} ^{n} d ^{\sum\limits_{i = 1} ^{\frac{n}{d}} \mu(i) C_{f[id]} ^{k}}\\ d=1nd[gcd(ds1,ds2,,dsk)=1]d=1ndi=1dnμ(i)Cf[id]k
筛出质数,然后得到 p h i ( m o d ) phi(mod) phi(mod),即可欧拉降幂写(提供一个思路整体复杂度 n log ⁡ 2 n n \log ^ 2 n nlog2n了)。

#pragma GCC optimize(3,"Ofast","inline")
#include <bits/stdc++.h>

using namespace std;

const int M = 1e7 + 10, N = 8e4 + 10, maxn = 80000;

int prime[M], mu[M], cnt;

int f[N], n, k;

long long C[N][32], mod, phi;

bool st[M];

inline long long read() {
  long long x = 0;
  char c = getchar();
  while (c < '0' || c > '9') {
    c = getchar();
  }
  while (c >= '0' && c <= '9') {
    x = (x << 1) + (x << 3) + (c ^ 48);
    c = getchar();
  }
  return x;
}

void Init() {
  mu[1] = 1;
  for (int i = 2; i < M; i++) {
    if (!st[i]) {
      prime[++cnt] = i;
      mu[i] = -1;
    }
    for (int j = 1; j <= cnt && 1ll * i * prime[j] < M; j++) {
      st[i * prime[j]] = 1;
      if (i % prime[j] == 0) {
        break;
      }
      mu[i * prime[j]] = -mu[i];
    }
  }
}

long long Phi(long long n) {
  long long ans = n;
  for (int i = 1; i <= cnt && 1ll * prime[i] * prime[i] <= n; i++) {
    if (n % prime[i] == 0) {
      ans = ans / prime[i] * (prime[i] - 1);
      while (n % prime[i] == 0) {
        n /= prime[i];
      }
    }
  }
  if (n > 1) {
    ans = ans / n * (n - 1);
  }
  return ans;
}

long long mul(long long x, long long y) { 
	return (__int128)x * y % mod; 
}

long long quick_pow(long long a, long long n) {
  long long ans = 1;
  while (n) {
    if (n & 1) {
      ans = mul(ans, a);
    }
    a = mul(a, a);
    n >>= 1;
  }
  return ans;
}

int main() {
  // freopen("data05.in", "r", stdin);
  // freopen("out.txt", "w", stdout);
  Init();
  int T;
  T = read();
  while (T--) {
    n = read(), k = read(), mod = read();
    for (int i = 1; i < N; i++) {
      f[i] = 0;
    }
    for (int i = 1, x; i <= n; i++) {
      x = read();
      f[x]++;
    }
    for (int i = 1; i <= maxn; i++) {
      for (int j = 2 * i; j <= maxn; j += i) {
        f[i] += f[j];
      }
    }
    phi = Phi(mod);
    C[0][0] = 1;
    for (int i = 1; i <= maxn; i++) {
      C[i][0] = 1;
      for (int j = 1; j <= 30; j++) {
        C[i][j] = C[i - 1][j - 1] + C[i - 1][j];
        if (C[i][j] > phi) {
          C[i][j] -= phi;
        }
      }
    }
    long long ans = 1;
    for (int d = 2; d <= maxn; d++) {
      long long cur = 0;
      for (int i = 1; i <= maxn / d; i++) {
        if (mu[i] == -1) {
          cur -= C[f[i * d]][k];
          if (cur < 0) {
            cur += phi;
          }
        }
        else if (mu[i] == 1) {
          cur += C[f[i * d]][k];
          if (cur > phi) {
            cur -= phi;
          }
        }
      }
      ans = mul(ans, quick_pow(d, cur));
    }
    printf("%lld\n", ans);
  }
  return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值