[国家集训队]Crash的数字表格 / JZPTAB
题意
求(sumlimits_{i=1}^nsumlimits_{j=1}^mlcm(i,j)),(n,mle 10^7)
鉴于我式子没推出来,所以再推一遍。
[sumlimits_{i=1}^nsumlimits_{j=1}^mlcm(i,j)
]
[=sumlimits_{i=1}^nsumlimits_{j=1}^mfrac{ij}{gcd(i,j)}
]
[=sumlimits_{i=1}^nsumlimits_{j=1}^m ijsum_{k=1}^{min(i,j)}frac{1}{k}[gcd(i,j)=k]
]
[=sum_{k=1}^{min(n,m)}frac{1}{k}sum_{i=1}^nsum_{j=1}^mij[gcd(i,j)=k]
]
[=sum_{k=1}^{min(n,m)}ksum_{i=1}^{lfloorfrac{n}{k}
floor}sum_{j=1}^{lfloorfrac{m}{k}
floor}ij[gcd(i,j)=1]
]
[=sum_{k=1}^{min(n,m)}ksum_{i=1}^{lfloorfrac{n}{k}
floor}sum_{j=1}^{lfloorfrac{m}{k}
floor}ijsum_{d=1}^{min(i,j)}mu(d)[gcd(i,j)=d]
]
[=sum_{k=1}^{min(n,m)}ksum_{d=1}^{min(lfloorfrac{n}{k}
floor,lfloorfrac{m}{k}
floor)}mu(d)d^2sum_{i=1}^{lfloorfrac{n}{kd}
floor}sum_{j=1}^{lfloorfrac{m}{kd}
floor}ij
]
[=sum_{k=1}^{min(n,m)}ksum_{d=1}^{min(lfloorfrac{n}{k}
floor,lfloorfrac{m}{k}
floor)}mu(d)d^2g(lfloorfrac{n}{kd}
floor)g(lfloorfrac{m}{kd}
floor)
]
[=sum_{T=1}^{min(n,m)}g(lfloorfrac{n}{T}
floor)g(lfloorfrac{m}{T}
floor)sum_{kd=T}kd^2mu(d)
]
[=sum_{T=1}^{min(n,m)}g(lfloorfrac{n}{T}
floor)g(lfloorfrac{m}{T}
floor)Tsum_{d|T}dmu(d)
]
设(f(n)=sum_{d|n}dmu(d))
研究一下( t{Ta})的性质,设(p)代表一个质数。
有(f(p)=1-p,f(p^n)=f(p)),(f(n))是一个积性函数。
所以我们可以在线筛的时候把这个函数(O(n))筛出来。
前面整除分块一下就可以了。
总复杂度(O(sqrt n+n))
Code:
#include <cstdio>
#define ll long long
const ll mod=20101009;
const int N=1e7;
int pri[N+10],ispri[N+10],cnt;
ll g[N+10];
#define f(x) (((x)+1)*(x)/2%mod)
void init()
{
g[1]=1;
for(int i=2;i<=N;i++)
{
if(!ispri[i])
{
g[i]=1-i;
pri[++cnt]=i;
}
for(int j=1;j<=cnt&&pri[j]*i<=N;j++)
{
ispri[pri[j]*i]=1;
if(i%pri[j]==0){g[pri[j]*i]=g[i];break;}
else g[pri[j]*i]=g[i]*g[pri[j]]%mod;
}
}
for(int i=1;i<=N;i++) g[i]*=i%=mod,(g[i]+=g[i-1])%mod;
}
ll min(ll a,ll b){return a<b?a:b;}
int main()
{
init();
ll ans=0,n,m;
scanf("%lld%lld",&n,&m);
for(ll l=1,r;l<=min(n,m);l=r+1)
{
r=min(n/(n/l),m/(m/l));
(ans+=f(n/l)*f(m/l)%mod*(g[r]-g[l-1]))%=mod;
}
printf("%lld
",(ans+mod)%mod);
return 0;
}
2018.10.26