题目大意:
解题思路
∑ i = 1 n ∑ j = 1 m l c m ( i , j ) \sum_{i=1}^n\sum_{j=1}^mlcm(i,j) ∑i=1n∑j=1mlcm(i,j)
= ∑ i = 1 n ∑ j = 1 m ∑ d = 1 n i j d [ g c d ( i , j ) = = d ] =\sum_{i=1}^n\sum_{j=1}^m\sum_{d=1}^n\frac {ij}{d}[gcd(i,j)==d] =∑i=1n∑j=1m∑d=1ndij[gcd(i,j)==d]
= ∑ d = 1 n ∑ i = 1 m ∑ j = 1 n i j d [ g c d ( i , j ) = = d ] =\sum_{d=1}^n\sum_{i=1}^m\sum_{j=1}^n\frac {ij}{d}[gcd(i,j)==d] =∑d=1n∑i=1m∑j=1ndij[gcd(i,j)==d]
将 i , j i,j i,j枚举改为枚举 d d d的倍数,即 i ′ d = i , j ′ d = j i'd=i,j'd=j i′d=i,j′d=j
= ∑ d = 1 n d ∑ i ′ = 1 n d ∑ j ′ = 1 m d i ′ j ′ [ g c d ( i ′ , j ′ ) = = 1 ] =\sum_{d=1}^nd\sum_{i'=1}^{\frac nd}\sum_{j'=1}^{\frac md}i'j'[gcd(i',j')==1] =∑d=1nd∑i′=1dn∑j′=1dmi′j′[gcd(i′,j′)==1]
= ∑ d = 1 n d ∑ i ′ = 1 n d ∑ j ′ = 1 m d i ′ j ′ ∑ t ∣ i ′ , t ∣ j ′ μ ( t ) =\sum_{d=1}^nd\sum_{i'=1}^{\frac nd}\sum_{j'=1}^{\frac md}i'j'\sum_{t|i',t|j'}\mu(t) =∑d=1nd∑i′=1dn∑j′=1dmi′j′∑t∣i′,t∣j′μ(t)
枚举t
= ∑ d = 1 n d ∑ t = 1 d n μ ( t ) ∑ i ′ = 1 n d ∑ j ′ = 1 m d [ t ∣ i ′ ] [ t ∣ j ′ ] i ′ j ′ =\sum_{d=1}^nd\sum_{t=1}^{\frac dn}\mu(t)\sum_{i'=1}^{\frac nd}\sum_{j'=1}^{\frac md}[t|i'][t|j']i'j' =∑d=1nd∑t=1ndμ(t)∑i′=1dn∑j′=1dm[t∣i′][t∣j′]i′j′
将 i ′ , j ′ i',j' i′,j′枚举改为枚举 t t t的倍数,即 i ′ ′ t = i ′ , j ′ ′ t = j ′ i''t=i',j''t=j' i′′t=i′,j′′t=j′
= ∑ d = 1 n d ∑ t = 1 d n μ ( t ) ∗ t 2 ∑ i ′ ′ = 1 n d t ∑ j ′ ′ = 1 m d t i ′ ′ j ′ ′ =\sum_{d=1}^nd\sum_{t=1}^{\frac dn}\mu(t)*t^2\sum_{i''=1}^{\frac n{dt}}\sum_{j''=1}^{\frac m{dt}}i''j'' =∑d=1nd∑t=1ndμ(t)∗t2∑i′′=1dtn∑j′′=1dtmi′′j′′
= ∑ d = 1 n ∑ t = 1 d n μ ( t ) ∗ d ∗ t 2 ∑ i ′ ′ = 1 n d t ∑ j ′ ′ = 1 m d t i ′ ′ j ′ ′ =\sum_{d=1}^n\sum_{t=1}^{\frac dn}\mu(t)*d*t^2\sum_{i''=1}^{\frac n{dt}}\sum_{j''=1}^{\frac m{dt}}i''j'' =∑d=1n∑t=1ndμ(t)∗d∗t2∑i′′=1dtn∑j′′=1dtmi′′j′′
令 T = d t , S ( x ) = ∑ i = 1 x i T=dt,S(x)=\sum_{i=1}^xi T=dt,S(x)=∑i=1xi
= ∑ d = 1 n ∑ t = 1 n d T t ∗ μ ( t ) ∗ S ( n T ) ∗ S ( m t ) =\sum_{d=1}^n\sum_{t=1}^{\frac nd}Tt*\mu(t)*S(\frac nT)*S(\frac mt) =∑d=1n∑t=1dnTt∗μ(t)∗S(Tn)∗S(tm)
将枚举 T T T提前,又因为 T = d t T=dt T=dt,所以 t t t是 T T T的因子,可以直接枚举T的因子即可,设为 k k k
= ∑ T = 1 n T ∗ S ( n T ) ∗ S ( m T ) ∑ k ∣ T k ∗ μ ( k ) =\sum_{T=1}^nT*S(\frac nT)*S(\frac mT)\sum_{k|T}k*\mu(k) =∑T=1nT∗S(Tn)∗S(Tm)∑k∣Tk∗μ(k)
令 F ( T ) = ∑ k ∣ T k ∗ μ ( k ) F(T)=\sum_{k|T}k*\mu(k) F(T)=∑k∣Tk∗μ(k), F ( T ) F(T) F(T)又为积性函数,可线性筛出来(可参考 μ ( ) \mu() μ()的筛选过程)
= ∑ T = 1 n S ( n T ) S ( m T ) T ∗ F ( T ) =\sum_{T=1}^nS(\frac nT)S(\frac mT)T*F(T) =∑T=1nS(Tn)S(Tm)T∗F(T)
再预处理 D ( T ) D(T) D(T)(也为积性函数) = T F ( T ) =TF(T) =TF(T)的前缀和
而 S ( n ) S ( m ) = n ∗ ( n + 1 ) / 2 ∗ m ∗ ( m + 1 ) / 2 = g ( n , m ) S(n)S(m)=n*(n+1)/2*m*(m+1)/2=g(n,m) S(n)S(m)=n∗(n+1)/2∗m∗(m+1)/2=g(n,m)
注:这恶心的取模,1e8+9,调了我巨久…
AC代码
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const int maxn = 1e7 + 10;
const ll mod = 1e8 + 9;
ll Q, n, m, cnt, inv2;
bool vis[maxn];
ll mu[maxn], p[maxn], D[maxn];
void init() {
mu[1] = vis[1] = D[1] = 1;
for (ll i = 2; i < maxn; i++) {
if (!vis[i]) p[++cnt] = i, mu[i] = -1, D[i] = ((i - i * i % mod) % mod + mod) % mod;;
for (ll j = 1; j <= cnt && p[j] * i < maxn; j++) {
vis[i * p[j]] = true;
if (i % p[j] == 0) {
mu[i * p[j]] = 0;
D[i * p[j]] = D[i] * p[j] % mod;
break;
}
else
mu[i * p[j]] = -mu[i],
D[i * p[j]] = D[i] * D[p[j]] % mod;
}
}
for (int i = 1; i < maxn; i++) D[i] = (D[i] + D[i - 1]) % mod;
}
ll g(ll n, ll m) {
n %= mod, m %= mod;
return n * (n + 1) % mod * inv2 % mod * m % mod * (m + 1) % mod * inv2 % mod;
}
ll rp(ll n, ll m) {
ll res = 0;
for (ll i = 1, j; i <= min(n, m); i = j + 1) {
j = min(n / (n / i), m / (m / i));
ll tmp = (D[j] - D[i - 1]) % mod;
tmp = (tmp + mod) % mod;
res = (res + tmp * g((n / i), (m / i)) % mod) % mod;
res %= mod;
}
return res;
}
int main() {
init();
inv2 = (mod + 1) / 2;
scanf("%lld", &Q);
//T = 1;
while (Q--) {
if (n > m) swap(n, m);
scanf("%lld%lld", &n, &m);
printf("%lld\n", rp(n, m));
}
}