正题
P1829
题目大意
给出n,m,求∑i=1n∑j=1mlcm(i,j)\sum_{i=1}^n\sum_{j=1}^mlcm(i,j)i=1∑nj=1∑mlcm(i,j)
解题思路
∑i=1n∑j=1mlcm(i,j)\sum_{i=1}^n\sum_{j=1}^mlcm(i,j)i=1∑nj=1∑mlcm(i,j)
∑d=1nd∑i=1n/d∑j=1m/di∗j[gcd(i,j)==1]\sum_{d=1}^nd\sum_{i=1}^{n/d}\sum_{j=1}^{m/d}i*j[gcd(i,j)==1]d=1∑ndi=1∑n/dj=1∑m/di∗j[gcd(i,j)==1]
∑d=1nd∑i=1n/d∑j=1m/di∗j∑c∣i,c∣jμ(c)\sum_{d=1}^nd\sum_{i=1}^{n/d}\sum_{j=1}^{m/d}i*j\sum_{c|i,c|j}\mu(c)d=1∑ndi=1∑n/dj=1∑m/di∗jc∣i,c∣j∑μ(c)
∑d=1nd∑c=1n/dμ(c)×c2×∑i=1n/cdi×∑j=1m/cdj\sum_{d=1}^nd\sum_{c=1}^{n/d}\mu(c)\times c^2\times \sum_{i=1}^{n/cd}i\times \sum_{j=1}^{m/cd}jd=1∑ndc=1∑n/dμ(c)×c2×i=1∑n/cdi×j=1∑m/cdj
考虑枚举cd,设k=cd,
∑k=1n(∑i=1n/ki×∑j=1m/kj×∑d∣kdμ(kd)(kd)2)\sum_{k=1}^n\left(\sum_{i=1}^{n/k}i\times \sum_{j=1}^{m/k}j\times \sum_{d|k}d\mu(\frac{k}{d})(\frac{k}{d})^2\right )k=1∑n⎝⎛i=1∑n/ki×j=1∑m/kj×d∣k∑dμ(dk)(dk)2⎠⎞
∑k=1n(k×nk(nk+1)2×mk(mk+1)2×∑d∣kdμ(d))\sum_{k=1}^n\left(k\times \frac{\frac{n}{k}(\frac{n}{k}+1)}{2}\times \frac{\frac{m}{k}(\frac{m}{k}+1)}{2}\times \sum_{d|k}d\mu(d)\right )k=1∑n⎝⎛k×2kn(kn+1)×2km(km+1)×d∣k∑dμ(d)⎠⎞
后面的 ∑d∣kdμ(d)\sum_{d|k}d\mu(d)∑d∣kdμ(d) 可以用狄利克雷前缀和计算,也可以用积性函数计算
时间复杂度 O(n)O(n)O(n)
code
#include<cstdio>
#include<cstring>
#include<iostream>
#include<algorithm>
#define ll long long
#define N 10000100
#define mod 20101009
using namespace std;
ll n,m,w,ans,p[N],g[N],mu[N],prime[N];
const ll MX=1e7;
void work()
{mu[1]=1;for(ll i=2;i<=MX;++i){if(!p[i]){mu[i]=-1;prime[++w]=i;}for(ll j=1;j<=w&&i*prime[j]<=MX;++j){p[i*prime[j]]=1;if(i%prime[j]==0)break;else mu[i*prime[j]]=-mu[i];}}
// for(ll i=1;i<=MX;++i)
// for(ll j=1;i*j<=MX;++j)
// g[i*j]+=i*mu[i];for(ll i=1;i<=MX;++i)g[i]=(i*mu[i]+mod)%mod;for(ll i=1;i<=w;++i)for(ll j=1;j*prime[i]<=MX;++j)(g[j*prime[i]]+=g[j])%=mod;return;
}
ll get(ll x)
{return (x*(x+1)/2)%mod;
}
int main()
{scanf("%lld%lld",&n,&m);work();for(ll i=1;i<=min(n,m);++i)(ans+=i*get(n/i)%mod*get(m/i)%mod*g[i]%mod)%=mod;printf("%lld",ans);return 0;
}