首页 技术 正文
技术 2022年11月14日
0 收藏 549 点赞 3,534 浏览 1560 个字

Luogu5221 Product

求 \(\displaystyle\prod_{i=1}^n\prod_{j=1}^n{\frac{\operatorname{lcm}(i,\ j)}{\gcd(i,\ j)}}\)

\(n\leq10^6\)

小清新数学题、、、


化式子

\[\begin{aligned}&\displaystyle\prod_{i=1}^n\prod_{j=1}^n{\frac{\operatorname{lcm}(i,\ j)}{\gcd(i,\ j)}}\\&=(\displaystyle\prod_{i=1}^n\prod_{j=1}^nij)(\displaystyle\prod_{i=1}^n\prod_{j=1}^n\gcd(i,\ j))^{-2}\\&=(\displaystyle\prod_{i=1}^ni^nn!)(\displaystyle\prod_{d=1}^n\prod_{i=1}^n\prod_{j=1}^nd[\gcd(i,\ j)=d])^{-2}\\&=n!^{2n}(\displaystyle\prod_{d=1}^nd^{\displaystyle\sum_{i=1}^{\lfloor\frac{n}{d}\rfloor}\sum_{j=1}^{\lfloor\frac{n}{d}\rfloor}[\gcd(i,\ j)=1]})^{-2}\\&=n!^{2n}(\displaystyle\prod_{d=1}^nd^{2\times(\displaystyle\sum_{i=1}^{\lfloor\frac{n}{d}\rfloor}\varphi(i))-1})^{-2}\end{aligned}
\]

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

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

时间复杂度 \(O(n\log 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;
}
相关推荐
python开发_常用的python模块及安装方法
adodb:我们领导推荐的数据库连接组件bsddb3:BerkeleyDB的连接组件Cheetah-1.0:我比较喜欢这个版本的cheeta…
日期:2022-11-24 点赞:878 阅读:8,907
Educational Codeforces Round 11 C. Hard Process 二分
C. Hard Process题目连接:http://www.codeforces.com/contest/660/problem/CDes…
日期:2022-11-24 点赞:807 阅读:5,432
下载Ubuntn 17.04 内核源代码
zengkefu@server1:/usr/src$ uname -aLinux server1 4.10.0-19-generic #21…
日期:2022-11-24 点赞:569 阅读:6,247
可用Active Desktop Calendar V7.86 注册码序列号
可用Active Desktop Calendar V7.86 注册码序列号Name: www.greendown.cn Code: &nb…
日期:2022-11-24 点赞:733 阅读:6,058
Android调用系统相机、自定义相机、处理大图片
Android调用系统相机和自定义相机实例本博文主要是介绍了android上使用相机进行拍照并显示的两种方式,并且由于涉及到要把拍到的照片显…
日期:2022-11-24 点赞:512 阅读:7,690
Struts的使用
一、Struts2的获取  Struts的官方网站为:http://struts.apache.org/  下载完Struts2的jar包,…
日期:2022-11-24 点赞:671 阅读:4,728