Bzoj2693. jzptab (莫比乌斯反演)

题目大意:

题目链接
在这里插入图片描述
(来源)

解题思路

∑ i = 1 n ∑ j = 1 m l c m ( i , j ) \sum_{i=1}^n\sum_{j=1}^mlcm(i,j) i=1nj=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=1nj=1md=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=1ni=1mj=1ndij[gcd(i,j)==d]

i , j i,j ij枚举改为枚举 d d d的倍数,即 i ′ d = i , j ′ d = j i'd=i,j'd=j id=i,jd=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=1ndi=1dnj=1dmij[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=1ndi=1dnj=1dmijti,tjμ(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=1ndt=1ndμ(t)i=1dnj=1dm[ti][tj]ij

i ′ , j ′ i',j' ij枚举改为枚举 t t t的倍数,即 i ′ ′ t = i ′ , j ′ ′ t = j ′ i''t=i',j''t=j' it=i,jt=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=1ndt=1ndμ(t)t2i=1dtnj=1dtmij

= ∑ 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=1nt=1ndμ(t)dt2i=1dtnj=1dtmij

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=1nt=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=1nTS(Tn)S(Tm)kTkμ(k)

F ( T ) = ∑ k ∣ T k ∗ μ ( k ) F(T)=\sum_{k|T}k*\mu(k) F(T)=kTkμ(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)TF(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)/2m(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));
    }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值