# 莫比乌斯反演

## 莫比乌斯函数

$\mu(x)= \cases{ 1~~~~~~~~~~~~~~~~~n=1\\ (-1)^k ~~~~~~~~~{n=p_1p_2\cdots p_k}\\ 0 ~ ~ ~~~~~~~~~~~~~~~ other }$

## 莫比乌斯反演

$$g(n)$$$$f(n)$$是定义在非负整数集合上的两个函数，并且满足$$g(n)=\sum_{d|n}f(d)$$，那么

$f(n)=\sum_{d|n}\mu(d)g(\frac{n}{d})$

\begin{aligned} &\sum_{d|n}\mu(d)g(\frac{n}{d})\\ =& \sum_{d|n}\mu(d)\sum_{k|\frac{n}{d}}f(k)\\ =& \sum_{d|n}\sum_{k|\frac{n}{d}}f(k)\mu(d)\\ =& \sum_{k|n}\sum_{d|\frac{n}{k}}f(k)\mu(d)\\ =&\sum_{k|n}f(k)\sum_{d|\frac{n}{k}}\mu(d)\\ =& f(n) \end{aligned}

$g(n)=\sum_{n|d}\mu(\frac{d}{n})f(d)$

$g(n)=\cases{1 &n=1\\0 &n>1}\\ f(n)=\mu(n)\\$

$g(n)=n\\ f(n)=\phi(n)$

$\phi(n)=\sum_{d|n}\mu(d)\frac{n}{d}$

$$\mu(n)$$是个积性函数，可以用线性筛预处理出来。

## 例题：

1.求$$[1,n]$$$$[1,m]$$上互质的数的个数

\begin{aligned} &\sum_{i=1}^{n}\sum_{j=1}^{m}[gcd(i,j)==1]\\ =&\sum_{i=1}^{n}\sum_{j=1}^{m}\sum_{d|gcd(i,j)}\mu(d)\\ =&\sum_{i=1}^{n}\sum_{j=1}^{m}\sum_{d|i \&d|j}\mu(d)\\ =&\sum_{d=1}^{min(n,m)}\mu(d)\sum_{i=1}^{\lfloor\frac{n}{d}\rfloor}\sum_{j=1}^{\lfloor\frac{m}{d}\rfloor}1\\ =&\sum_{d=1}^{min(n,m)}\mu(d)\times\lfloor\frac{n}{d}\rfloor\times\lfloor\frac{m}{d}\rfloor \end{aligned}

2.Crash的数字表格

\begin{aligned} ans=&\sum_{i=1}^n\sum_{j=1}^m\frac{ij}{gcd(i,j)}\\ =&\sum_{i=1}^{n}\sum_{j=1}^m\sum_{g|i\&g|j}\frac{ij}{g}\times[gcd(i/g,j/g)=1]\\ =&\sum_{g=1}^{min(n,m)}\sum_{i=1}^{\lfloor\frac{n}{g}\rfloor}\sum_{j=1}^{\lfloor\frac{m}{g}\rfloor}ijg\times[gcd(i,j)=1]\\ =&\sum_{g=1}^{min(n,m)}\sum_{i=1}^{\lfloor\frac{n}{g}\rfloor}\sum_{j=1}^{\lfloor\frac{m}{g}\rfloor}ijg\sum_{d|i\&d|j}\mu(d)\\ =&\sum_{g=1}^{min(n,m)}g\sum_{d=1}^{min(\lfloor\frac{m}{g}\rfloor,\lfloor\frac{n}{g}\rfloor)}\mu(d)d^2\sum_{i=1}^{\lfloor\frac{n}{gd}\rfloor}i\sum_{j=1}^{\lfloor\frac{m}{gd}\rfloor}j\\ =&\sum_{g=1}^{min(n,m)}g\sum_{d=1}^{min(\lfloor\frac{m}{g}\rfloor,\lfloor\frac{n}{g}\rfloor)}\mu(d)d^2\frac{(\lfloor\frac{m}{gd}\rfloor+1)\lfloor\frac{m}{gd}\rfloor(\lfloor\frac{n}{gd}\rfloor+1)\lfloor\frac{n}{gd}\rfloor}{4} \end{aligned}

// Problem: P1829 [国家集训队]Crash的数字表格 / JZPTAB
// Contest: Luogu
// URL: https://www.luogu.com.cn/problem/P1829
// Memory Limit: 500 MB
// Time Limit: 2000 ms
//

#include<bits/stdc++.h>
using namespace std;
const int maxn = 1e7 + 7;
#define ll long long
const ll md = 20101009;
int rd() {
int s = 0, f = 1; char c = getchar();
while (c < '0' || c > '9') {if (c == '-') f = -1; c = getchar();}
while (c >= '0' && c <= '9') {s = s * 10 + c - '0'; c = getchar();}
return s * f;
}
int n, m, tot;
bool isp[maxn];
ll sumd[maxn], pri[maxn], mu[maxn];

void sieve() {
memset(isp, true, sizeof(isp));
isp[1] = false; mu[1] = 1;
for (int i = 2; i <= 10000000; i++) {
if (isp[i]) {
pri[++tot] = i;
mu[i] = -1;
}
for (int j = 1; j <= tot && i * pri[j] <= 10000000; j++) {
isp[i*pri[j]] = false;
if (i % pri[j] == 0) {
mu[i*pri[j]] = 0;
break;
}
mu[i*pri[j]] = -mu[i];
}
}
for (int i = 1; i <= 10000000; i++)
sumd[i] = (sumd[i-1] + 1ll * i * (mu[i] + md) % md * i % md) % md;
}
ll ksm(ll a, int b) {
ll res = 1;
while (b) {
if (b & 1) res = res * a % md;
a = a * a % md;
b >>= 1;
}
return res;
}
ll ans = 0, inv4 = ksm(4, md - 2), inv2 = ksm(2, md-2);
ll f(ll x, ll y) {
return (x * (x+1) % md * y % md * (y+1) % md * inv4 % md);
}
ll g(ll x, ll y) {
int t = min(x, y);
ll res = 0;
for (int l = 1, r; l <= t; l = r + 1) {
r = min(x / (x / l), y / (y / l));
res = (res + (sumd[r] - sumd[l-1]) * f(x/l, y/l) % md + md) % md;
}
return res;
}
ll h(ll x, ll y) {
ll res = 0;
int t = min(x, y);
for (int l = 1, r; l <= t; l = r + 1) {
r = min(x / (x / l), y / (y / l));
res = (res + g(x / l, y / l) * (l + r) % md * (r - l + 1) % md * inv2 % md + md) % md;
}
return res;
}

int main() {
n = rd(), m = rd();
sieve();
printf("%lld\n", h(n, m));
}

posted @ 2021-08-19 13:24  yjmstr  阅读(90)  评论(0编辑  收藏  举报