# BZOJ 2693: jzptab(莫比乌斯反演)

## 解题思路

$ans=\sum\limits_{i=1}^n\sum\limits_{j=1}^mlcm(i,j)$

$ans=\sum\limits_{i=1}^n\sum\limits_{j=1}^m\frac{i*j}{gcd(i,j)}$

$ans=\sum\limits_{d=1}^nd\sum\limits_{i=1}^{\frac{n}{d}}\sum\limits_{j=1}^{\frac{m}{d}}i*j*[gcd(i,j)=1]$

$ans=\sum\limits_{d=1}^nd\sum\limits_{d'=1}^{\frac{n}{d}}\mu(d')*d'^2\sum\limits_{i=1}^{\frac{n}{dd'}}\sum\limits_{j=1}^{\frac{n}{dd'}}i*j$

$ans=\sum\limits_{T=1}^nsum(n/T,m/T)\sum\limits_{d=1}^n\mu(d)*d^2*\frac{T}{d}$

## 代码

#include<iostream>
#include<cstdio>
#include<cstring>
#include<cmath>
#include<cstdlib>

using namespace std;
typedef long long LL;
const int N=1e7+5;
const int MOD=1e8+9;

inline int rd(){
int x=0,f=1;char ch=getchar();
while(!isdigit(ch)) f=ch=='-'?0:1,ch=getchar();
while(isdigit(ch)) x=(x<<1)+(x<<3)+ch-'0',ch=getchar();
return f?x:-x;
}

inline int min(int x,int y){return x<y?x:y;}

int T,n,m,prime[N/10],cnt,f[N];
bool vis[N];

inline void prework(){
f[1]=1;int lim=1e7;
for(int i=2;i<=lim;i++){
if(!vis[i]) {prime[++cnt]=i; f[i]=(i-(LL)i*i%MOD+MOD)%MOD;}
for(int j=1;j<=cnt && (LL)prime[j]*i<=lim;j++){
vis[i*prime[j]]=1;
if(!(i%prime[j])) {
f[i*prime[j]]=(LL)f[i]*prime[j]%MOD;
break;
}
f[i*prime[j]]=(LL)f[i]*f[prime[j]]%MOD;
}
}
for(int i=1;i<=lim;i++) f[i]=(f[i-1]+f[i])%MOD;
}

inline int calc(int x,int y){
return (LL)((LL)(x+1)*x/2%MOD)*((LL)(y+1)*y/2%MOD)%MOD;
}

inline void work(){
int ans=0;
n=rd(),m=rd();if(n>m) swap(n,m);
for(int l=1,r;l<=n;l=r+1){
r=min(n/(n/l),m/(m/l));
ans=(ans+(LL)calc(n/l,m/l)*(f[r]-f[l-1]+MOD)%MOD)%MOD;
}
printf("%d\n",ans);
}

int main(){
prework();T=rd();
while(T--) work();
return 0;
}

posted @ 2019-01-16 21:07  Monster_Qi  阅读(129)  评论(0编辑  收藏  举报