Luogu5221 Product

Luogu5221 Product

(displaystyleprod_{i=1}^nprod_{j=1}^n{frac{operatorname{lcm}(i, j)}{gcd(i, j)}})

(nleq10^6)

小清新数学题、、、


化式子

[egin{aligned}&displaystyleprod_{i=1}^nprod_{j=1}^n{frac{operatorname{lcm}(i, j)}{gcd(i, j)}}\&=(displaystyleprod_{i=1}^nprod_{j=1}^nij)(displaystyleprod_{i=1}^nprod_{j=1}^ngcd(i, j))^{-2}\&=(displaystyleprod_{i=1}^ni^nn!)(displaystyleprod_{d=1}^nprod_{i=1}^nprod_{j=1}^nd[gcd(i, j)=d])^{-2}\&=n!^{2n}(displaystyleprod_{d=1}^nd^{displaystylesum_{i=1}^{lfloorfrac{n}{d} floor}sum_{j=1}^{lfloorfrac{n}{d} floor}[gcd(i, j)=1]})^{-2}\&=n!^{2n}(displaystyleprod_{d=1}^nd^{2 imes(displaystylesum_{i=1}^{lfloorfrac{n}{d} floor}varphi(i))-1})^{-2}end{aligned} ]

但是这道题卡时间卡空间、、、

(varphi) 前缀和会爆 (int) ,所以用欧拉定理,卡卡空间卡卡常就吼辣

时间复杂度 (O(nlog n))

代码

#include <bits/stdc++.h>
using namespace std;

const int maxn = 1e6 + 10, P = 104857601;
int n, tot, p[maxn / 10], phi[maxn];
bitset <maxn> f;

inline int mod(int x, int P) {
	return x < P ? x : x - P;
}

inline int qp(int a, int k) {
  int res = 1;
  for (; k; k >>= 1, a = 1ll * a * a % P) {
    if (k & 1) res = 1ll * res * a % P;
  }
  return res;
}

inline void sieve() {
  phi[1] = 1;
  for (int i = 2; i <= n; i++) {
    if (!f[i]) p[++tot] = i, phi[i] = i - 1;
    for (int j = 1; j <= tot && i * p[j] <= n; j++) {
      f[i * p[j]] = 1;
      if (i % p[j] == 0) {
        phi[i * p[j]] = phi[i] * p[j]; break;
      }
      phi[i * p[j]] = phi[i] * phi[p[j]];
    }
  }
  for (int i = 1; i <= n; i++) {
    phi[i] = mod(mod(phi[i] << 1, P - 1) + phi[i - 1], P - 1);
  }
}

int main() {
  scanf("%d", &n);
  sieve();
  int s = 1;
  for (int i = 1; i <= n; i++) {
	  s = 1ll * s * i % P;
  }
	int ans = qp(s, n << 1), sum = 1;
  for (int i = 1; i <= n; i++) {
    sum = 1ll * sum * qp(i, mod(phi[n / i] + P - 2, P - 1)) % P;
  }
  sum = qp(sum, P - 2);
  sum = 1ll * sum * sum % P;
  printf("%d", 1ll * ans * sum % P);
  return 0;
}
原文地址:https://www.cnblogs.com/Juanzhang/p/10426075.html