【洛谷 P5176】公约数(莫比乌斯反演)

传送门

考虑对于一个质数pp
i,j,ki,j,k中分别为pa,pb,pcp^a,p^b,p^c
a<b<ca<b<c
那么这个式子的值就是
pa+b+a2p2a+p2bpa+a+bp^{a+b+a}*\frac{2p^{2a}+p^{2b}}{p^{a+a+b}}

=2p2a+p2b=2p^{2a}+p^{2b}

那么对于整个式子
ijkgcd(i,j)2+gcd(j,k)2+gcd(i,k)2\sum_{i}\sum_j\sum_kgcd(i,j)^2+gcd(j,k)^2+gcd(i,k)^2
=njkgcd(j,k)2+mikgcd(i,k)2+pijgcd(i,j)2=n*\sum_j\sum_kgcd(j,k)^2+m*\sum_i\sum_kgcd(i,k)^2+p*\sum_i\sum_jgcd(i,j)^2

只考虑求injmgcd(i,j)2\sum_i^n\sum_j^mgcd(i,j)^2
简单莫反出来就是T=1nnTnTdTd2μ(Td)\sum_{T=1}^n\frac n T\frac n T\sum_{d|T}d^2\mu(\frac T d)

这个可以线筛,然后又做完了

#include<bits/stdc++.h>
using namespace std;
#define cs const
#define re register
#define pb push_back
#define pii pair<int,int>
#define ll long long
#define fi first
#define se second
#define bg begin
cs int RLEN=1<<20|1;
inline char gc(){
    static char ibuf[RLEN],*ib,*ob;
    (ib==ob)&&(ob=(ib=ibuf)+fread(ibuf,1,RLEN,stdin));
    return (ib==ob)?EOF:*ib++;
}
inline int read(){
    char ch=gc();
    int res=0;bool f=1;
    while(!isdigit(ch))f^=ch=='-',ch=gc();
    while(isdigit(ch))res=(res+(res<<2)<<1)+(ch^48),ch=gc();
    return f?res:-res;
}
template<class tp>inline void chemx(tp &a,tp b){a<b?a=b:0;}
template<class tp>inline void chemn(tp &a,tp b){a>b?a=b:0;}
cs int mod=1e9+7;
inline int add(int a,int b){return (a+=b)>=mod?(a-mod):a;}
inline int dec(int a,int b){a-=b;return a+(a>>31&mod);}
inline int mul(int a,int b){static ll r;r=1ll*a*b;return (r>=mod)?(r%mod):r;}
inline void Add(int &a,int b){(a+=b)>=mod?(a-=mod):0;}
inline void Dec(int &a,int b){a-=b,a+=a>>31&mod;}
inline void Mul(int &a,int b){static ll r;r=1ll*a*b;a=(r>=mod)?(r%mod):r;}
inline int ksm(int a,int b,int res=1){for(;b;b>>=1,Mul(a,a))(b&1)&&(Mul(res,a),1);return res;}
inline int Inv(int x){return ksm(x,mod-2);}
cs int N=20000007;
int pr[N/15+1],tot,f[N];
bitset<N>vis;
inline void init(cs int len=N-5){
	f[1]=1;
	for(int i=2;i<=len;i++){
		if(!vis[i])pr[++tot]=i,f[i]=mul(i,i)-1;
		for(int p,j=1,l=len/i;j<=tot&&pr[j]<=l;j++){
			p=i*pr[j],vis[p]=1;
			if(i%pr[j]==0){
				f[p]=mul(f[i],mul(pr[j],pr[j]));break;
			}
			f[p]=mul(f[i],f[pr[j]]);
		}
	}
	for(int i=1;i<=len;i++)Add(f[i],f[i-1]);
}
inline int calc(int n,int m){
	if(n>m)swap(n,m);
	int ret=0;
	for(int i=1,j;i<=n;i=j+1){
		j=min(n/(n/i),m/(m/i));
		Add(ret,mul(mul(n/i,dec(f[j],f[i-1])),m/i));
	}
	return ret;
}
int n,m,p;
inline void solve(){
	n=read(),m=read(),p=read();
	int ret=0;
	Add(ret,mul(calc(n,m),p));
	Add(ret,mul(calc(n,p),m));
	Add(ret,mul(calc(m,p),n));
	cout<<ret<<'\n';
}
int main(){
	#ifdef Stargazer
	freopen("lx.in","r",stdin);
	#endif
	init();
	int T=read();
	while(T--)solve();
}
posted @ 2020-01-21 18:51  Stargazer_cykoi  阅读(136)  评论(0编辑  收藏  举报