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

传送门

考虑对于一个质数 p p p
i , j , k i,j,k i,j,k中分别为 p a , p b , p c p^a,p^b,p^c pa,pb,pc
a < b < c aa<b<c
那么这个式子的值就是
p a + b + a ∗ 2 p 2 a + p 2 b p a + a + b p^{a+b+a}*\frac{2p^{2a}+p^{2b}}{p^{a+a+b}} pa+b+apa+a+b2p2a+p2b

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

那么对于整个式子
∑ i ∑ j ∑ k g c d ( i , j ) 2 + g c d ( j , k ) 2 + g c d ( i , k ) 2 \sum_{i}\sum_j\sum_kgcd(i,j)^2+gcd(j,k)^2+gcd(i,k)^2 ijkgcd(i,j)2+gcd(j,k)2+gcd(i,k)2
= n ∗ ∑ j ∑ k g c d ( j , k ) 2 + m ∗ ∑ i ∑ k g c d ( i , k ) 2 + p ∗ ∑ i ∑ j g c d ( 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 =njkgcd(j,k)2+mikgcd(i,k)2+pijgcd(i,j)2

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

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

#include
using namespace std;
#define cs const
#define re register
#define pb push_back
#define pii pair
#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();
}

你可能感兴趣的:(莫比乌斯反演)