Product

V.Product

要求这个东西:

\(\prod\limits_{i=1}^n\prod\limits_{j=1}^n\dfrac{\operatorname{lcm}(i,j)}{\gcd(i,j)}\)

开始推式子。

\(\begin{aligned}\\&\prod_{i=1}^n\prod_{j=1}^n\dfrac{\operatorname{lcm}(i,j)}{\gcd(i,j)}\\=&\prod_{i=1}^n\prod_{j=1}^n\dfrac{ij}{\gcd^2(i,j)}\\=&\dfrac{\prod_{i=1}^n\prod_{j=1}^nij}{\prod_{i=1}^n\prod_{j=1}^n\gcd^2(i,j)}\end{aligned}\)

上下分别考虑。首先,我们有\(\prod_{i=1}^n\prod_{j=1}^nij=(n!)^{2n}\)

考虑下面的东西。

\(\begin{aligned}\\&\prod_{i=1}^n\prod_{j=1}^n\gcd\!^2(i,j)\\=&\Big(\prod_{i=1}^n\prod_{j=1}^n\gcd(i,j)\Big)^2\\=&\Big(\prod_{d=1}^n\prod_{i=1}^n\prod_{j=1}^nd[\gcd(i,j)=d]\Big)^2\\=&\Big(\prod_{d=1}^nd^{\sum_{i=1}^{n/d}\sum_{j=1}^{n/d}[\gcd(i,j)=1]}\Big)^2\\=&\Big(\prod_{d=1}^nd^{\sum_{x=1}^{n/d}\mu(x)(\frac{n}{dx})^2}\Big)^2\end{aligned}\)

然后我们最后有

\(ans=\dfrac{(n!)^{2n}}{\Big(\prod_{d=1}^nd^{\sum_{x=1}^{n/d}\mu(x)(\frac{n}{dx})^2}\Big)^2}\)

下面的东西,是可以\(\sqrt{n}\log n\)或者类似的亚线性时间里,用两次整除分块加快速幂做出来的。但是,如果这样的话,就需要预处理出阶乘数组。但是毒瘤出题人卡空间,8M几乎开不了什么东西。先把一份空间8.4M,刚好被卡掉的正确代码奉上:

#include<bits/stdc++.h>
using namespace std;
const int N=1e6;
const int mod=104857601;
int n,pri[N+2],mu[N+2],ans=1;
void init(){
	mu[1]=1;
	for(int i=2;i<=N;i++){
		if(!pri[i])pri[++pri[0]]=i,mu[i]=-1;
		for(int j=1;j<=pri[0]&&i*pri[j]<=N;j++){
			pri[i*pri[j]]=true;
			if(!(i%pri[j]))break;
			mu[i*pri[j]]=-mu[i];
		}
	}
	pri[0]=1;
	for(int i=1;i<=N;i++)pri[i]=1ll*pri[i-1]*i%mod,mu[i]=(0ll+mu[i]+mu[i-1]+mod-1)%(mod-1);
}
int ksm(int x,int y){
	int res=1;
	for(;y;x=(1ll*x*x)%mod,y>>=1)if(y&1)res=(1ll*res*x)%mod;
	return res;
}
int calc(int x){
	int res=0;
	for(int l=1,r;l<=x;l=r+1)r=x/(x/l),res=(res+1ll*(mu[r]-mu[l-1]+(mod-1))%(mod-1)*(x/l)%(mod-1)*(x/l)%(mod-1))%(mod-1);
	return res;
}
int main(){
	scanf("%d",&n),init();
	for(int l=1,r;l<=n;l=r+1)r=n/(n/l),ans=(1ll*ans*ksm(1ll*pri[r]*ksm(pri[l-1],mod-2)%mod,calc(n/l)))%mod;
	ans=(1ll*ans*ans)%mod;
	ans=ksm(ans,mod-2);
	ans=1ll*ans*ksm(pri[n],2*n)%mod;
	printf("%d\n",ans);
	return 0;
} 

为了避免MLE,我们只能放弃外层的分块,只保留内层的分块。则复杂度为\(\sum\limits_{i=1}^n\sqrt{\dfrac{n}{i}}=n\log n\)

代码:

#include<bits/stdc++.h>
using namespace std;
const int N=1e6;
const int mod=104857601;
int n,pri[80100],mu[N+2],ans=1,ans2=1;
bool vis[N+2];
void init(){
	mu[1]=1;
	for(int i=2;i<=N;i++){
		if(!vis[i])pri[++pri[0]]=i,mu[i]=-1;
		for(int j=1;j<=pri[0]&&i*pri[j]<=N;j++){
			vis[i*pri[j]]=true;
			if(!(i%pri[j]))break;
			mu[i*pri[j]]=-mu[i];
		}
	}
	for(int i=1;i<=N;i++)mu[i]=(0ll+mu[i]+mu[i-1]+(mod-1))%(mod-1);
}
int ksm(int x,int y){
	int res=1;
	for(;y;x=(1ll*x*x)%mod,y>>=1)if(y&1)res=(1ll*res*x)%mod;
	return res;
}
int calc(int x){
	int res=0;
	for(int l=1,r;l<=x;l=r+1)r=x/(x/l),res=(1ll*(mu[r]-mu[l-1]+(mod-1))%(mod-1)*(x/l)%(mod-1)*(x/l)%(mod-1)+res)%(mod-1);
	return res;
}
int main(){
	scanf("%d",&n),init();
	for(int i=1;i<=n;i++)ans=1ll*ans*i%mod;
	ans=ksm(ans,2*n);
	for(int i=1;i<=n;i++)ans2=(1ll*ans2*ksm(i,calc(n/i)))%mod;
	ans2=(1ll*ans2*ans2)%mod;
	ans2=ksm(ans2,mod-2);
	printf("%d\n",1ll*ans*ans2%mod);
	return 0;
} 

原文地址:https://www.cnblogs.com/Troverld/p/14619653.html