题目
P1829 [国家集训队]Crash的数字表格 / JZPTAB
这题解法较多,都有值得学习的部分
解法一
(Ans=sum_{i=1}^{n}sum_{j=1}^{m}frac{ij}{gcd(i,j)})
思考把(sum_{d|n}mu(d)=[n=1])带进去
(Ans=sum_{d=1}^{min(n,m)}sum_{i=1}^{n}sum_{j=1}^{m}[gcd(i,j)=d]frac{ij}{d})
(Ans=sum_{d=1}^{min(n,m)}dsum_{i=1}^{lfloorfrac{n}{d} floor}sum_{j=1}^{lfloorfrac{m}{d} floor}[gcd(i,j)=1]ij)
至此,我们把式变成:
(Ans=sum_{d=1}^{min(n,m)}dsum_{i=1}^{lfloorfrac{n}{d} floor}sum_{j=1}^{lfloorfrac{m}{d} floor}sum_{x|gcd(i,j)}mu(x)ij)
我们来枚举(x)消掉(sum_{x|gcd(i,j)}):
(Ans=sum_{d=1}^{min(n,m)}dsum_{x=1}^{min(lfloorfrac{n}{d} floor,lfloorfrac{m}{d} floor)}mu(x)sum_{i=1}^{lfloorfrac{n}{d} floor}sum_{j=1}^{lfloorfrac{m}{d} floor}ij[x|gcd(i,j)])
对于判断([x|gcd(i,j)])也要消掉:
(Ans=sum_{d=1}^{min(n,m)}dsum_{x=1}^{min(lfloorfrac{n}{d} floor,lfloorfrac{m}{d} floor)}mu(x)sum_{xu=1}^{lfloorfrac{n}{d} floor}sum_{xv=1}^{lfloorfrac{m}{d} floor}x^2uv)
最后要处理的式子:
(Ans=sum_{d=1}^{min(n,m)}dsum_{x=1}^{min(lfloorfrac{n}{d} floor,lfloorfrac{m}{d} floor)}x^2mu(x)(sum_{u=1}^{lfloorfrac{n}{dx} floor}u)(sum_{v=1}^{lfloorfrac{m}{dx} floor}v))
直接分块,另((sum_{u=1}^{lfloorfrac{n}{dx} floor}u)(sum_{v=1}^{lfloorfrac{m}{dx} floor}v))相同
解法二
(egin{aligned} Ans &= sum_{d=1}^nsum_{i=1}^{n}sum_{j=1}^{m}frac{ij}{d}[gcd(i,j)=d] \ &= sum_{d=1}^ndsum_{i=1}^{lfloor frac{n}{d} floor }sum_{j=1}^{lfloor frac{m}{d} floor }ij[gcd(i,j)==1] \ & = sum_{d=1}^ndsum_{i=1}^{lfloor frac{n}{d} floor }sum_{j=1}^{lfloor frac{m}{d} floor }ijsum_{t|gcd(i,j)}mu(t) \ & = sum_{d=1}^ndsum_{t=1}^{lfloor frac{n}{d} floor}mu(t)sum_{i=1}^{frac{n}{dt}}itsum_{j=1}^{frac{m}{dt}}jt \ & = sum_{d=1}^ndsum_{t=1}^{lfloor frac{n}{d} floor}t^2mu(t)sum_{i=1}^{frac{n}{dt}}isum_{j=1}^{frac{m}{dt}}j \ end{aligned})
由于(sum_{i=1}^{frac{n}{dt}}isum_{j=1}^{frac{m}{dt}}j)可以分块,所以枚举(dt)
(sum_{T=1}^n sum_{i=1}^{frac{n}{T}}isum_{j=1}^{frac{m}{T}} floor)Tsum_{t|T}tmu(t))
#include<cstdio>
#include<cstring>
#include<iostream>
#include<algorithm>
using namespace std;
typedef long long LL;
const LL p=20101009;
const int maxn=1e7+10;
inline int Read(){
LL x=0,f=1; char c=getchar();
while(c<'0'||c>'9'){
if(c=='-') f=-1; c=getchar();
}
while(c>='0'&&c<='9'){
x=(x<<3)+(x<<1)+c-'0',c=getchar();
}
return x*f;
}
int n,m;
LL ans;
int mu[maxn],prime[maxn];
LL sum[maxn];
bool visit[maxn];
inline void F_phi(LL max_n){
mu[1]=1;
int tot=0;
for(int i=2;i<=max_n;++i){
if(!visit[i]){
prime[++tot]=i,
mu[i]=-1;
}
for(int j=1;j<=tot&&i*prime[j]<=max_n;++j){
visit[i*prime[j]]=true;
if(i%prime[j]==0)
break;
else
mu[i*prime[j]]=-mu[i];
}
}
for(int i=1;i<=max_n;++i)
sum[i]=(sum[i-1]+(LL)mu[i]*i%p*i%p)%p;
}
int main(){
scanf("%d%d",&n,&m);
int N=min(n,m);
F_phi(N);
for(int d=1;d<=N;++d){
int maxx=n/d,maxy=m/d;
int x=min(maxx,maxy);
LL num=0;
for(int l=1,r;l<=x;l=r+1){
r=min(maxx/(maxx/l),maxy/(maxy/l));
num=(num+
((sum[r]-sum[l-1]+p)%p)*
((((1ll+maxx/l)%p)*1ll*(maxx/l)/2%p)%p)%p*
((((1ll+maxy/l)%p)*1ll*(maxy/l)/2%p)%p)%p)%p;
}
ans=(ans+(num*1ll*d)%p)%p;
}
printf("%lld",ans);
return 0;
}/*
1000000 1000000
9002207
*/