【BZOJ2694】Lcm(莫比乌斯反演)

传送门

注意到其实可以把 ∀ n > 1 , n 2 ∤ g c d ( i , j ) \forall n>1,n^2\not |gcd(i,j) n>1,n2gcd(i,j)
写作 ∣ μ ( g c d ( i , j ) ) ∣ |\mu(gcd(i,j))| μ(gcd(i,j))
然后把 l c m ( i , j ) lcm(i,j) lcm(i,j)写作 i j / g c d ( i , j ) ij/gcd(i,j) ij/gcd(i,j)

然后就可以大力莫反了

最后推出来就是
∑ T = 1 n S ( n T ) S ( m T ) f ( T ) \sum_{T=1}^{n}S(\frac n T)S(\frac m T)f(T) T=1nS(Tn)S(Tm)f(T)
其中 S ( n ) = ∑ i = 1 n i , f ( T ) = ∑ d ∣ T d ∣ μ ( d ) ∣ μ ( T d ) ( T d ) 2 S(n)=\sum_{i=1}^ni,f(T)=\sum_{d|T}d|\mu(d)|\mu(\frac Td)(\frac T d)^2 S(n)=i=1ni,f(T)=dTdμ(d)μ(dT)(dT)2

然后发现 f f f可以线筛(其实 n l o g n nlogn nlogn应该也可以)
f ( 1 ) = 1 , f ( p ) = p − p 2 , f ( p 2 ) = − p 3 , f ( p k ) = 0 ( k > 2 ) f(1)=1,f(p)=p-p^2,f(p^2)=-p^3,f(p^k)=0(k>2) f(1)=1,f(p)=pp2,f(p2)=p3,f(pk)=0(k>2)

然后就完了

#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;}
inline int ksm(int a,int b,int res=1){
     
	for(;b;b>>=1,a=a*a)if(b&1)res=res*a;return res;
}
cs int N=4000005;
int pr[N],tot;
int 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]=i-i*i;
		for(int p,j=1;j<=tot&&i*pr[j]<=len;j++){
     
			p=i*pr[j],vis[p]=1;
			if(i%pr[j]==0){
     
				if((i/pr[j])%pr[j]==0)f[p]=0;
				else f[p]=-f[i/pr[j]]*pr[j]*pr[j]*pr[j];
				break;
			}
			f[p]=f[i]*f[pr[j]];
		}
	}
	for(int i=2;i<=len;i++)f[i]+=f[i-1];
}
int n,m;
inline int s(int x){
     return 1ll*x*(x+1)/2;}
inline void solve(){
     
	n=read(),m=read();
	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));
		ret=ret+s(n/i)*s(m/i)*(f[j]-f[i-1]);
	}
	cout<<((ret%(1<<30)+(1<<30))%(1<<30))<<'\n';
}
int main(){
     
	#ifdef Stargazer
	freopen("lx.in","r",stdin);
	#endif
	
	int T=read();init();
	while(T--)solve();
}

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