【洛谷 P5176】公约数(莫比乌斯反演)
考虑对于一个质数
在中分别为
设
那么这个式子的值就是
那么对于整个式子
只考虑求
简单莫反出来就是
这个可以线筛,然后又做完了
#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();
}