#莫比乌斯反演#洛谷 5176 公约数

题目

∑ i = 1 n ∑ j = 1 m ∑ k = 1 p g c d ( i j , i k , j k ) × g c d ( i , j , k ) × ( g c d ( i , j ) g c d ( i , k ) g c d ( j , k ) + g c d ( i , k ) g c d ( i , j ) g c d ( j , k ) + g c d ( j , k ) g c d ( i , j ) g c d ( i , k ) ) \sum_{i=1}^n\sum_{j=1}^m\sum_{k=1}^pgcd(ij,ik,jk)\times gcd(i,j,k)\times (\frac{gcd(i,j)}{gcd(i,k)gcd(j,k)}+\frac{gcd(i,k)}{gcd(i,j)gcd(j,k)}+\frac{gcd(j,k)}{gcd(i,j)gcd(i,k)}) i=1nj=1mk=1pgcd(ij,ik,jk)×gcd(i,j,k)×(gcd(i,k)gcd(j,k)gcd(i,j)+gcd(i,j)gcd(j,k)gcd(i,k)+gcd(i,j)gcd(i,k)gcd(j,k))


分析

∵ g c d ( i j , i k , j k ) = g c d ( i , j ) g c d ( i , k ) g c d ( j , k ) g c d ( i , j , k ) \because gcd(ij,ik,jk)=\frac{gcd(i,j)gcd(i,k)gcd(j,k)}{gcd(i,j,k)} gcd(ij,ik,jk)=gcd(i,j,k)gcd(i,j)gcd(i,k)gcd(j,k)
再加上后面的通分,这个式子可以火速化简成
∑ i = 1 n ∑ j = 1 m ∑ k = 1 p g c d ( i , j ) 2 + g c d ( i , k ) 2 + g c d ( j , k ) 2 \sum_{i=1}^n\sum_{j=1}^m\sum_{k=1}^pgcd(i,j)^2+gcd(i,k)^2+gcd(j,k)^2 i=1nj=1mk=1pgcd(i,j)2+gcd(i,k)2+gcd(j,k)2
= p × ∑ i = 1 n ∑ j = 1 m g c d ( i , j ) 2 + m × ∑ i = 1 n ∑ k = 1 p g c d ( i , k ) 2 + n × ∑ j = 1 m ∑ k = 1 p g c d ( j , k ) 2 =p\times\sum_{i=1}^n\sum_{j=1}^mgcd(i,j)^2+m\times\sum_{i=1}^n\sum_{k=1}^pgcd(i,k)^2+n\times\sum_{j=1}^m\sum_{k=1}^pgcd(j,k)^2 =p×i=1nj=1mgcd(i,j)2+m×i=1nk=1pgcd(i,k)2+n×j=1mk=1pgcd(j,k)2
于是题目就变成了这个洛谷 4449
对于单独的 ∑ i = 1 n ∑ j = 1 m g c d ( i , j ) 2 \sum_{i=1}^n\sum_{j=1}^mgcd(i,j)^2 i=1nj=1mgcd(i,j)2
最后变成 ∑ i = 1 n ⌊ n i ⌋ ⌊ m i ⌋ ∑ d ∣ i d 2 μ ( i d ) \sum_{i=1}^n\lfloor\frac{n}{i}\rfloor\lfloor\frac{m}{i}\rfloor\sum_{d|i}d^2\mu(\frac{i}{d}) i=1ninimdid2μ(di)
至于后面怎么推就理解吧


代码

#include 
#include 
#define rr register
using namespace std;
const int N=20000011,mod=1000000007;
int dp[N],prime[N],cnt,T,k; bool v[N];
inline signed iut(){
	rr int ans=0; rr char c=getchar();
	while (!isdigit(c)) c=getchar();
	while (isdigit(c)) ans=(ans<<3)+(ans<<1)+(c^48),c=getchar();
	return ans;
}
inline void print(int ans){
	if (ans>9) print(ans/10);
	putchar(ans%10+48);
}
inline signed min(int a,int b){return a<b?a:b;}
inline signed mo(int x,int y){return x+y>=mod?x+y-mod:x+y;}
inline void init(){
	dp[1]=1;
	for (rr int i=2;i<N;++i){
		if (!v[i]) prime[++cnt]=i,dp[i]=1ll*(i+1)*(i-1)%mod;
		for (rr int j=1;prime[j]*i<N&&j<=cnt;++j){
			v[i*prime[j]]=1;
			if (i%prime[j]==0){
				dp[i*prime[j]]=1ll*dp[i]*(dp[prime[j]]+1)%mod;
				break;
			}
			dp[i*prime[j]]=1ll*dp[i]*dp[prime[j]]%mod;
		}
	}
	for (rr int i=2;i<N;++i) dp[i]=mo(dp[i],dp[i-1]);
}
inline signed f(int n,int m){
	rr int t=n<m?n:m,ans=0;
	for (rr int l=1,r;l<=t;l=r+1){
		r=min(n/(n/l),m/(m/l));
		ans=mo(ans,1ll*mo(dp[r]-dp[l-1],mod)*(n/l)%mod*(m/l)%mod);
	}
	return ans;	
}
signed main(){
	T=iut(); init();
	while (T--){
		rr int n=iut(),m=iut(),p=iut(),ans=0;
		ans=mo(mo(1ll*n*f(m,p)%mod,1ll*m*f(n,p)%mod),1ll*p*f(n,m)%mod);
		print(ans),putchar(10);
	}
	return 0;
}

你可能感兴趣的:(反演,洛谷,5176,公约数)