「BZOJ 2154」Crash的数字表格「莫比乌斯反演」

题目传送门

题意

ni=1mj=1lcm(i,j) ∑ i = 1 n ∑ j = 1 m l c m ( i , j ) ,对 20101009 20101009 取模

题解

不妨设nm.

ni=1mj=1lcm(i,j)

=ni=1mj=1ijgcd(i,j)

=\sum_{d=1}^{n}\sum_{i=1}^{n} \sum_{j=1}^{m} \frac{ij}{d} [gcd(i,j)=d]

=\sum_{d=1}^{n}\sum_{i=1}^{\lfloor\frac{n}{d}\rfloor} \sum_{j=1}^{\lfloor\frac{m}{d}\rfloor} ijd [gcd(i,j)=1]

=\sum_{d=1}^{n}d\sum_{i=1}^{\lfloor\frac{n}{d}\rfloor} \sum_{j=1}^{\lfloor\frac{m}{d}\rfloor} ij[gcd(i,j)=1]

=\sum_{d=1}^{n}d\sum_{i=1}^{\lfloor\frac{n}{d}\rfloor} \sum_{j=1}^{\lfloor\frac{m}{d}\rfloor} ije(gcd(i,j))

=\sum_{d=1}^{n}d\sum_{i=1}^{\lfloor\frac{n}{d}\rfloor} \sum_{j=1}^{\lfloor\frac{m}{d}\rfloor} ij \sum_{k|gcd(i,j)} \mu(k)

=\sum_{d=1}^{n}d \sum_{k=1}^{\lfloor\frac{n}{d}\rfloor} \mu(k) \sum_{i=1}^{\lfloor\frac{n}{dk}\rfloor} \sum_{j=1}^{\lfloor\frac{m}{dk}\rfloor} ijk^2

=\sum_{d=1}^{n}d \sum_{k=1}^{\lfloor\frac{n}{d}\rfloor} k^2\mu(k) \sum_{i=1}^{\lfloor\frac{n}{dk}\rfloor} i\sum_{j=1}^{\lfloor\frac{m}{dk}\rfloor} j

sum(k)表示\sum_{i=1}^{k}i,即\frac{k(k+1)}{2}

=\sum_{d=1}^{n}d \sum_{k=1}^{\lfloor\frac{n}{d}\rfloor} k^2\mu(k) sum(\lfloor\frac{n}{dk}\rfloor)sum(\lfloor\frac{m}{dk}\rfloor)

枚举T=dk

=\sum_{T=1}^{n}sum(\lfloor\frac{n}{T}\rfloor)sum(\lfloor\frac{m}{T}\rfloor)\sum_{d|T}d (\frac{T}{d})^2\mu(\frac{T}{d})

考虑到枚举d相当于枚举T/d

=\sum_{T=1}^{n}sum(\lfloor\frac{n}{T}\rfloor)sum(\lfloor\frac{m}{T}\rfloor)\sum_{d|T}\frac{T}{d} (d)^2\mu(d)

=\sum_{T=1}^{n}sum(\lfloor\frac{n}{T}\rfloor)sum(\lfloor\frac{m}{T}\rfloor)\sum_{d|T} Td\mu(d)

=\sum_{T=1}^{n}sum(\lfloor\frac{n}{T}\rfloor)sum(\lfloor\frac{m}{T}\rfloor)T\sum_{d|T} d\mu(d)

f(T)=\sum_{d|T} d\mu(d),这可以线性筛

如果pi的一个质因子,那么f(ip)=f(i)(因为p加入不会产生新的有贡献的约数d)。否则这个p乘上之前的每一个约数d,使得\mu(pd)=-\mu(d),加入答案里,即f(ip)=f(i)-pf(i)=(1-p)f(i),注意到f(p)=1-p(根据定义),即f(ip)=f(i)f(p).

于是就可以愉快的数论分块+线性筛了

#include 
#include 
using namespace std;

const int MOD = 20101009;
const int MAXN = 1e7 + 10;

int n, m;
int pr[MAXN], tot;
int f[MAXN], sum[MAXN];
bool tag[MAXN];

void init(int n) {
    tag[1] = true;
    f[1] = sum[1] = 1;
    for(int i = 2; i <= n; i ++) {
        if(!tag[i]) {
            pr[++ tot] = i;
            f[i] = ((1 - i) % MOD + MOD) % MOD;
        }
        for(int j = 1; j <= tot && i * 1ll * pr[j] <= n; j ++) {
            tag[i * pr[j]] = true;
            if(i % pr[j] == 0) {
                f[i * pr[j]] = f[i];
                break ;
            }
            f[i * pr[j]] = f[i] * 1ll * f[pr[j]] % MOD;
        }
    }
    for(int i = 2; i <= n; i ++) {
        f[i] = (f[i - 1] + 1ll * f[i] * i % MOD) % MOD;
        sum[i] = (sum[i - 1] + 1ll * i) % MOD;
    }
}

int main() {
    scanf("%d%d", &n, &m);
    if(n > m) swap(n, m);
    init(m);
    int ans = 0;
    for(int i = 1, j; i <= n; i = j + 1) {
        j = min(n / (n / i), m / (m / i));
        ans = (ans + ((( (f[j] - f[i-1]) % MOD + MOD ) % MOD) * 1ll \
        * sum[n / i] % MOD * 1ll * sum[m / i] % MOD)) % MOD;
    }
    printf("%d\n", ans);
    return 0;
}

你可能感兴趣的:(数学,-,莫比乌斯反演)