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

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 

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;
}

你可能感兴趣的:(数论)