【学习笔记】莫比乌斯反演
其实引理用得更多,人麻了
引理:
f ( n ) = ∑ d ∣ n μ ( d ) ⇒ f ( n ) = { 1 , n = 1 0 , e l s e \begin{aligned} f (n) &= \sum_{d \mid n} \mu (d) \\ \Rightarrow f (n) &= \begin{cases} 1, n =1 \\ 0, else \end{cases} \end{aligned} f(n)⇒f(n)=d∣n∑μ(d)={1,n=10,else
容斥定理易证
原式:
f ( n ) = ∑ d ∣ n F ( d ) ⇒ F ( n ) = ∑ d ∣ n μ ( n d ) f ( d ) \begin{aligned} f (n) &= \sum_{d \mid n} F (d) \\ \Rightarrow F (n) &= \sum_{d \mid n} \mu (\frac{n}{d}) f (d) \end{aligned} f(n)⇒F(n)=d∣n∑F(d)=d∣n∑μ(dn)f(d)
证明:
F ( n ) = ∑ d ∣ n μ ( n d ) ∑ p ∣ d F ( p ) 令 t = d p = ∑ p ∑ t ∣ n p μ ( n t p ) F ( p ) = ∑ p F ( p ) ∑ t ∣ n p μ ( n p t ) = F ( n ) \begin{aligned} F (n) &= \sum_{d \mid n} \mu (\frac{n}{d}) \sum_{p \mid d} F (p) \\ 令 t = &\frac{d}{p} \\ &= \sum_{p} \sum_{t \mid \frac{n}{p}} \mu (\frac{n}{tp}) F (p) \\ &= \sum_{p} F (p) \sum_{t \mid \frac{n}{p}} \mu (\frac{\frac{n}{p}}{t}) \\ &= F (n) \end{aligned} F(n)令t==d∣n∑μ(dn)p∣d∑F(p)pd=p∑t∣pn∑μ(tpn)F(p)=p∑F(p)t∣pn∑μ(tpn)=F(n)
推论:
f ( n ) = ∑ n ∣ d F ( d ) ⇒ F ( n ) = ∑ n ∣ d μ ( d n ) f ( d ) \begin{aligned} f (n) &= \sum_{n \mid d} F (d) \\ \Rightarrow F (n) &= \sum_{n \mid d} \mu (\frac{d}{n}) f (d) \end{aligned} f(n)⇒F(n)=n∣d∑F(d)=n∣d∑μ(nd)f(d)
证明:
类似于原式。
例题
一、 Y Y YY YY 的 G C D GCD GCD
∑ p r i m e ∑ x ∈ [ 1 , n ] ∑ y ∈ [ 1 , m ] [ ( x , y ) = = p r i m e ] \begin{aligned} & \sum_{prime} \sum_{x \in [1, n]}\sum_{y \in [1, m]} [(x, y) == prime] \end{aligned} prime∑x∈[1,n]∑y∈[1,m]∑[(x,y)==prime]
令 f ( i ) f(i) f(i) 表示 g c d = i gcd = i gcd=i 的数的对数, F ( i ) F(i) F(i) 表示 i ∣ g c d i \mid gcd i∣gcd 的数的对数
F ( i ) = ∑ i ∣ j f ( j ) f ( i ) = ∑ i ∣ j μ ( j i ) F ( j ) a n s w e r = ∑ p r i m e f ( p r i m e ) = ∑ p r i m e ∑ p r i m e ∣ j μ ( j p r i m e ) ⌊ n j ⌋ ⌊ m j ⌋ = ∑ j ⌊ n j ⌋ ⌊ m j ⌋ ∑ p r i m e ∣ j μ ( j p r i m e ) 令 h ( x ) = ∑ p r i m e ∣ x μ ( x p r i m e ) = ∑ j ⌊ n j ⌋ ⌊ m j ⌋ h ( j ) \begin{aligned} F (i) &= \sum_{i \mid j} f (j) \\ f (i) &= \sum_{i \mid j} \mu (\frac{j}{i}) F (j) \\ answer &= \sum_{prime} f (prime) \\ &= \sum_{prime} \sum_{prime \mid j} \mu(\frac{j}{prime}) \lfloor \frac{n}{j} \rfloor \lfloor \frac{m}{j} \rfloor \\ &= \sum_{j} \lfloor \frac{n}{j} \rfloor \lfloor \frac{m}{j} \rfloor \sum_{prime \mid j} \mu (\frac{j}{prime}) \\ 令 h (x) = &\sum_{prime \mid x} \mu (\frac{x}{prime}) \\ &= \sum_{j} \lfloor \frac{n}{j} \rfloor \lfloor \frac{m}{j} \rfloor h (j) \end{aligned} F(i)f(i)answer令h(x)==i∣j∑f(j)=i∣j∑μ(ij)F(j)=prime∑f(prime)=prime∑prime∣j∑μ(primej)⌊jn⌋⌊jm⌋=j∑⌊jn⌋⌊jm⌋prime∣j∑μ(primej)prime∣x∑μ(primex)=j∑⌊jn⌋⌊jm⌋h(j)
把 h h h 打表出来,整数分块,结束了。
//author : LH ——Who just can eat S??t
//worship WJC ——Who can f??k tourist up and down and loves 周歆恬
//worship YJX ——Who can f??k WJC up and down
#include <cmath>
#include <queue>
#include <vector>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <algorithm>
using namespace std;
#define fi first
#define se second
#define db double
#define LL long long
#define ULL unsigned long long
#define PII pair <int, int>
#define MP(x,y) make_pair (x, y)
#define rep(i,j,k) for (int i = (j); i <= (k); ++i)
#define per(i,j,k) for (int i = (j); i >= (k); --i)
template <typename T> T Max (T x, T y) { return x > y ? x : y; }
template <typename T> T Min (T x, T y) { return x < y ? x : y; }
template <typename T> T Abs (T x) { return x > 0 ? x : -x; }
template <typename T>
void read (T &x) {
x = 0; T f = 1;
char ch = getchar ();
while (ch < '0' || ch > '9') {
if (ch == '-') f = -1;
ch = getchar ();
}
while (ch >= '0' && ch <= '9') {
x = (x << 3) + (x << 1) + ch - '0';
ch = getchar ();
}
x *= f;
}
template <typename T, typename... Args>
void read (T &x, Args&... args) {
read (x); read (args...);
}
char For_Print[25];
template <typename T>
void write (T x) {
if (x == 0) { putchar ('0'); return; }
if (x < 0) { putchar ('-'); x = -x; }
int poi = 0;
while (x) {
For_Print[++poi] = x % 10 + '0';
x /= 10;
}
while (poi) putchar (For_Print[poi--]);
}
template <typename T>
void print (T x, char ch) {
write (x); putchar (ch);
}
const LL Mod = 1e9 + 7;
LL square (LL x) { return (x * x) % Mod; }
void DEL (LL &x, LL y) { ((x -= y) < 0) && (x += Mod); }
void ADD (LL &x, LL y) { ((x += y) >= Mod) && (x -= Mod); }
const int Maxn = 1e7;
int t, n, m;
int cnt, primes[Maxn + 5];
int mu[Maxn + 5], px[Maxn + 5], h[Maxn + 5];
LL pre[Maxn + 5];
bool vis[Maxn + 5];
void Euler () {
mu[1] = 1;
rep (i, 2, Maxn) {
if (!vis[i]) {
primes[++cnt] = i;
mu[i] = -1;
px[i] = i;
}
rep (j, 1, cnt) {
if (primes[j] > Maxn / i) break;
vis[i * primes[j]] = 1;
if (i % primes[j] == 0) {
mu[i * primes[j]] = 0;
px[i * primes[j]] = primes[j];
break;
}
mu[i * primes[j]] = -mu[i];
px[i * primes[j]] = primes[j];
}
}
rep (i, 1, Maxn) {
int tmp = i;
while (tmp != 1) {
int prm = px[tmp];
h[i] += mu[i / prm];
while (tmp % prm == 0)
tmp /= prm;
}
}
rep (i, 1, Maxn) {
pre[i] = pre[i - 1] + h[i];
}
}
int main () {
// freopen ("D:\\lihan\\1.in", "r", stdin);
// freopen ("D:\\lihan\\1.out", "w", stdout);
Euler ();
read (t);
while (t--) {
read (n, m);
if (n > m) swap (n, m);
int l = 1, r = n;
LL res = 0;
while (l <= n) {
r = Min (n / (n / l), m / (m / l));
res += (LL)(n / l) * (m / l) * (pre[r] - pre[l - 1]);
l = r + 1;
}
cout << res << endl;
}
return 0;
}
二、于神之怒
∑ i = 1 n ∑ j = 1 m g c d ( i , j ) k = ∑ d ∑ i ∑ j d k ∑ p ∣ g c d ( i , j ) d μ ( p ) = ∑ p μ ( p ) ∑ d d k ⌊ n d p ⌋ ⌊ m d p ⌋ = ∑ d d k ∑ p μ ( p ) ⌊ n d p ⌋ ⌊ m d p ⌋ 令 t = d p = ∑ t ⌊ n t ⌋ ⌊ m t ⌋ ∑ d ∣ t d k μ ( t d ) 令 h ( x ) = ∑ d ∣ x d k μ ( x d ) = ∑ t ⌊ n t ⌋ ⌊ m t ⌋ h ( t ) \begin{aligned} &\sum_{i = 1}^n\sum_{j = 1}^m gcd (i, j)^k \\ = &\sum_d\sum_i\sum_j d^k \sum_{p \mid \frac{gcd (i, j)}{d}} \mu (p) \\ = &\sum_p \mu (p) \sum_d d^k \lfloor \frac{n}{dp} \rfloor \lfloor \frac{m}{dp} \rfloor \\ = &\sum_dd^k\sum_p \mu (p) \lfloor \frac{n}{dp} \rfloor \lfloor \frac{m}{dp} \rfloor \\ 令 &t = dp \\ = &\sum_t \lfloor \frac{n}{t} \rfloor \lfloor \frac{m}{t} \rfloor \sum_{d \mid t} d^k \mu (\frac{t}{d}) \\ 令 &h (x) = \sum_{d \mid x} d^k \mu (\frac{x}{d}) \\ = &\sum_t \lfloor \frac{n}{t} \rfloor \lfloor \frac{m}{t} \rfloor h (t) \end{aligned} ===令=令=i=1∑nj=1∑mgcd(i,j)kd∑i∑j∑dkp∣dgcd(i,j)∑μ(p)p∑μ(p)d∑dk⌊dpn⌋⌊dpm⌋d∑dkp∑μ(p)⌊dpn⌋⌊dpm⌋t=dpt∑⌊tn⌋⌊tm⌋d∣t∑dkμ(dt)h(x)=d∣x∑dkμ(dx)t∑⌊tn⌋⌊tm⌋h(t)
//author : LH ——Who just can eat S??t
//worship WJC ——Who can f??k tourist up and down and loves 周歆恬
//worship YJX ——Who can f??k WJC up and down
#include <cmath>
#include <queue>
#include <vector>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <algorithm>
using namespace std;
#define fi first
#define se second
#define db double
#define LL long long
#define ULL unsigned long long
#define PII pair <int, int>
#define MP(x,y) make_pair (x, y)
#define rep(i,j,k) for (int i = (j); i <= (k); ++i)
#define per(i,j,k) for (int i = (j); i >= (k); --i)
template <typename T> T Max (T x, T y) { return x > y ? x : y; }
template <typename T> T Min (T x, T y) { return x < y ? x : y; }
template <typename T> T Abs (T x) { return x > 0 ? x : -x; }
template <typename T>
void read (T &x) {
x = 0; T f = 1;
char ch = getchar ();
while (ch < '0' || ch > '9') {
if (ch == '-') f = -1;
ch = getchar ();
}
while (ch >= '0' && ch <= '9') {
x = (x << 3) + (x << 1) + ch - '0';
ch = getchar ();
}
x *= f;
}
template <typename T, typename... Args>
void read (T &x, Args&... args) {
read (x); read (args...);
}
char For_Print[25];
template <typename T>
void write (T x) {
if (x == 0) { putchar ('0'); return; }
if (x < 0) { putchar ('-'); x = -x; }
int poi = 0;
while (x) {
For_Print[++poi] = x % 10 + '0';
x /= 10;
}
while (poi) putchar (For_Print[poi--]);
}
template <typename T>
void print (T x, char ch) {
write (x); putchar (ch);
}
const LL Mod = 1e9 + 7;
LL square (LL x) { return (x * x) % Mod; }
void DEL (LL &x, LL y) { ((x -= y) < 0) && (x += Mod); }
void ADD (LL &x, LL y) { ((x += y) >= Mod) && (x -= Mod); }
const int Maxn = 5 * 1e6;
int t, k, n, m;
int cnt, primes[Maxn + 5];
LL h[Maxn + 5], val[Maxn + 5], pre[Maxn + 5];
bool vis[Maxn + 5];
LL quick_pow (LL x, LL y) {
LL res = 1;
while (y) {
if (y & 1) res = (res * x) % Mod;
x = (x * x) % Mod; y >>= 1;
}
return res;
}
LL md (LL x, LL M) {
return (x % M + M) % M;
}
void Euler () {
pre[1] = h[1] = 1;
rep (i, 2, Maxn) {
if (!vis[i]) {
primes[++cnt] = i;
val[i] = quick_pow (i, k);
h[i] = md (val[i] - 1, Mod);
}
rep (j, 1, cnt) {
if (primes[j] > Maxn / i) break;
vis[primes[j] * i] = 1;
if (i % primes[j] == 0) {
h[i * primes[j]] = h[i] * val[primes[j]] % Mod;
break;
}
h[i * primes[j]] = h[i] * (val[primes[j]] - 1) % Mod;
}
}
rep (i, 1, Maxn) pre[i] = (pre[i - 1] + h[i]) % Mod;
}
int main () {
// freopen ("D:\\lihan\\1.in", "r", stdin);
// freopen ("D:\\lihan\\1.out", "w", stdout);
read (t, k); Euler ();
while (t--) {
read (n, m);
if (n > m) swap (n, m);
int l = 1, r;
LL res = 0;
while (l <= n) {
r = Min (n / (n / l), m / (m / l));
ADD (res, (LL)(n / l) * (m / l) % Mod * md (pre[r] - pre[l - 1], Mod) % Mod);
l = r + 1;
}
print (res, '\n');
}
return 0;
}
三、Crash的数字表格
∑ i = 1 n ∑ j = 1 m i ⋅ j g c d ( i , j ) = ∑ d ∑ i ∑ j i ⋅ j d [ g c d ( i , j ) = = d ] = ∑ d ∑ i ∑ j i ⋅ j d ∑ p ∣ g c d ( i d , j d ) μ ( p ) = ∑ p μ ( p ) ∑ d ∑ d p ∣ i ∑ d p ∣ j i ⋅ j d = ∑ p μ ( p ) ∑ d 1 d ∑ d p ∣ i i ∑ d p ∣ j j 令 l m = ⌊ m d p ⌋ = ∑ p μ ( p ) ∑ d 1 d ( 1 + l m ) ∗ l m 2 ⋅ d p ∑ d p ∣ i i 令 l n = ⌊ n d p ⌋ = ∑ p μ ( p ) ∑ d ( 1 + l m ) ∗ l m 2 d ⋅ d p ⋅ ( 1 + l n ) ∗ l n 2 ⋅ d p = ∑ p μ ( p ) ∑ d ( 1 + l m ) ∗ l m ⋅ ( 1 + l n ) ∗ l n ⋅ p 2 ⋅ d 4 = ∑ p μ ( p ) ∑ d ( 1 + l m ) ∗ l m ⋅ ( 1 + l n ) ∗ l n ⋅ p 2 ⋅ d 4 令 t = d p = ∑ p μ ( p ) ⋅ p 2 ∑ p ∣ t t ⋅ ( 1 + ⌊ m t ⌋ ) ∗ ⌊ m t ⌋ ⋅ ( 1 + ⌊ n t ⌋ ) ∗ ⌊ n t ⌋ 4 p = ∑ p μ ( p ) ⋅ p ∑ p ∣ t t ⋅ ( 1 + ⌊ m t ⌋ ) ∗ ⌊ m t ⌋ ⋅ ( 1 + ⌊ n t ⌋ ) ∗ ⌊ n t ⌋ 4 = ∑ t t ⋅ ( 1 + ⌊ m t ⌋ ) ∗ ⌊ m t ⌋ ⋅ ( 1 + ⌊ n t ⌋ ) ∗ ⌊ n t ⌋ 4 ∑ p ∣ t μ ( p ) ⋅ p 令 h ( t ) = t ⋅ ∑ p ∣ t μ ( p ) ⋅ p = ∑ t ( 1 + ⌊ m t ⌋ ) ∗ ⌊ m t ⌋ ⋅ ( 1 + ⌊ n t ⌋ ) ∗ ⌊ n t ⌋ 4 h ( t ) \begin{aligned} &\sum_{i = 1}^n\sum_{j = 1} ^ {m} \frac{i \cdot j}{gcd (i, j)} \\ = &\sum_{d} \sum_{i} \sum_{j} \frac{i \cdot j}{d} [gcd (i, j) == d] \\ = &\sum_{d} \sum_{i} \sum_{j} \frac{i \cdot j}{d} \sum_{p \mid gcd (\frac{i}{d}, \frac{j}{d})} \mu (p) \\ = &\sum_{p} \mu (p) \sum_{d} \sum_{dp \mid i} \sum_{dp \mid j} \frac{i \cdot j}{d} \\ = &\sum_{p} \mu (p) \sum_{d} \frac{1}{d} \sum_{dp \mid i} i \sum_{dp \mid j} j \\ 令 &lm = \lfloor \frac{m}{dp} \rfloor \\ = &\sum_{p} \mu (p) \sum_{d} \frac{1}{d} \frac {(1 + lm) * lm}{2} \cdot dp \sum_{dp \mid i} i \\ 令 &ln = \lfloor \frac{n}{dp} \rfloor \\ = &\sum_{p} \mu (p) \sum_{d} \frac {(1 + lm) * lm}{2d} \cdot dp \cdot \frac{(1+ln) * ln}{2} \cdot dp \\ = &\sum_{p} \mu (p) \sum_{d} \frac {(1 + lm) * lm \cdot (1+ ln) * ln \cdot p ^ 2 \cdot d}{4} \\ = &\sum_{p} \mu (p) \sum_{d} \frac {(1 + lm) * lm \cdot (1+ ln) * ln \cdot p ^ 2 \cdot d}{4} \\ 令 &t = dp \\ = &\sum_{p} \mu (p) \cdot p ^ 2 \frac{\sum_{p \mid t} t \cdot (1 + \lfloor \frac{m}{t} \rfloor) * \lfloor \frac{m}{t} \rfloor \cdot (1 + \lfloor \frac{n}{t} \rfloor) * \lfloor \frac{n}{t} \rfloor}{4p} \\ = &\sum_{p} \mu (p) \cdot p \frac{\sum_{p \mid t} t \cdot (1 + \lfloor \frac{m}{t} \rfloor) * \lfloor \frac{m}{t} \rfloor \cdot (1 + \lfloor \frac{n}{t} \rfloor) * \lfloor \frac{n}{t} \rfloor}{4} \\ = &\sum_{t}\frac{t \cdot (1 + \lfloor \frac{m}{t} \rfloor) * \lfloor \frac{m}{t} \rfloor \cdot (1 + \lfloor \frac{n}{t} \rfloor) * \lfloor \frac{n}{t} \rfloor}{4} \sum_{p \mid t} \mu (p) \cdot p \\ 令 &h (t) = t \cdot \sum_{p \mid t} \mu (p) \cdot p \\ = &\sum_{t}\frac{(1 + \lfloor \frac{m}{t} \rfloor) * \lfloor \frac{m}{t} \rfloor \cdot (1 + \lfloor \frac{n}{t} \rfloor) * \lfloor \frac{n}{t} \rfloor}{4} h (t) \end{aligned} ====令=令===令===令=i=1∑nj=1∑mgcd(i,j)i⋅jd∑i∑j∑di⋅j[gcd(i,j)==d]d∑i∑j∑di⋅jp∣gcd(di,dj)∑μ(p)p∑μ(p)d∑dp∣i∑dp∣j∑di⋅jp∑μ(p)d∑d1dp∣i∑idp∣j∑jlm=⌊dpm⌋p∑μ(p)d∑d12(1+lm)∗lm⋅dpdp∣i∑iln=⌊dpn⌋p∑μ(p)d∑2d(1+lm)∗lm⋅dp⋅2(1+ln)∗ln⋅dpp∑μ(p)d∑4(1+lm)∗lm⋅(1+ln)∗ln⋅p2⋅dp∑μ(p)d∑4(1+lm)∗lm⋅(1+ln)∗ln⋅p2⋅dt=dpp∑μ(p)⋅p24p∑p∣tt⋅(1+⌊tm⌋)∗⌊tm⌋⋅(1+⌊tn⌋)∗⌊tn⌋p∑μ(p)⋅p4∑p∣tt⋅(1+⌊tm⌋)∗⌊tm⌋⋅(1+⌊tn⌋)∗⌊tn⌋t∑4t⋅(1+⌊tm⌋)∗⌊tm⌋⋅(1+⌊tn⌋)∗⌊tn⌋p∣t∑μ(p)⋅ph(t)=t⋅p∣t∑μ(p)⋅pt∑4(1+⌊tm⌋)∗⌊tm⌋⋅(1+⌊tn⌋)∗⌊tn⌋h(t)
//author : LH ——Who just can eat S??t
//worship WJC ——Who can f??k tourist up and down and loves 周歆恬
//worship YJX ——Who can f??k WJC up and down
#include <cmath>
#include <queue>
#include <vector>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <algorithm>
using namespace std;
#define fi first
#define se second
#define db double
#define LL long long
#define ULL unsigned long long
#define PII pair <int, int>
#define MP(x,y) make_pair (x, y)
#define rep(i,j,k) for (int i = (j); i <= (k); ++i)
#define per(i,j,k) for (int i = (j); i >= (k); --i)
template <typename T> T Max (T x, T y) { return x > y ? x : y; }
template <typename T> T Min (T x, T y) { return x < y ? x : y; }
template <typename T> T Abs (T x) { return x > 0 ? x : -x; }
template <typename T>
void read (T &x) {
x = 0; T f = 1;
char ch = getchar ();
while (ch < '0' || ch > '9') {
if (ch == '-') f = -1;
ch = getchar ();
}
while (ch >= '0' && ch <= '9') {
x = (x << 3) + (x << 1) + ch - '0';
ch = getchar ();
}
x *= f;
}
template <typename T, typename... Args>
void read (T &x, Args&... args) {
read (x); read (args...);
}
char For_Print[25];
template <typename T>
void write (T x) {
if (x == 0) { putchar ('0'); return; }
if (x < 0) { putchar ('-'); x = -x; }
int poi = 0;
while (x) {
For_Print[++poi] = x % 10 + '0';
x /= 10;
}
while (poi) putchar (For_Print[poi--]);
}
template <typename T>
void print (T x, char ch) {
write (x); putchar (ch);
}
const LL Mod = 20101009;
const LL inv_2 = 10050505;
LL square (LL x) { return (x * x) % Mod; }
void DEL (LL &x, LL y) { ((x -= y) < 0) && (x += Mod); }
void ADD (LL &x, LL y) { ((x += y) >= Mod) && (x -= Mod); }
const int Maxq = 1e6;
const int Maxn = 1e7;
int cnt, primes[Maxq + 5];
LL h[Maxn + 5], pre[Maxn + 5];
bool vis[Maxn + 5];
LL md (LL x, LL M) {
return (x % M + M) % M;
}
void Euler () {
h[1] = 1;
rep (i, 2, Maxn) {
if (!vis[i]) {
primes[++cnt] = i;
h[i] = 1 - i;
}
rep (j, 1, cnt) {
if (primes[j] > Maxn / i) break;
vis[primes[j] * i] = 1;
if (i % primes[j] == 0) {
h[i * primes[j]] = h[i];
break;
}
h[i * primes[j]] = h[i] * (1 - primes[j]) % Mod;
}
}
rep (i, 1, Maxn) pre[i] = md (pre[i - 1] + h[i] * i % Mod, Mod);
}
LL n, m;
int main () {
// freopen ("D:\\lihan\\1.in", "r", stdin);
// freopen ("D:\\lihan\\1.out", "w", stdout);
Euler ();
read (n, m);
// cerr << n << " " << m << endl;
if (n > m) swap (n, m);
int l = 1, r;
LL res = 0;
while (l <= n) {
LL ln = n / l, lm = m / l;
r = Min (n / ln, m / lm);
ADD (res, (1 + ln) * ln % Mod * (1 + lm) % Mod * lm % Mod * md (pre[r] - pre[l - 1], Mod) % Mod * inv_2 % Mod * inv_2 % Mod);
l = r + 1;
}
cout << res;
return 0;
}
四、 「 S D O I 2014 」 「SDOI2014」 「SDOI2014」 数表
a n s w e r = ∑ i = 1 n ∑ j = 1 m [ σ ( g c d ( i , j ) ) ≤ a ] σ ( g c d ( i , j ) ) 令 g = g c d ( i , j ) = ∑ g ∑ i ∑ j [ σ ( g ) ≤ a ] σ ( g ) = ∑ g [ σ ( g ) ≤ a ] ∑ i ∑ j ∑ d ∣ g c d ( i g , j g ) μ ( d ) σ ( g ) = ∑ g [ σ ( g ) ≤ a ] ∑ d μ ( d ) ∑ d g ∣ i ∑ d g ∣ j σ ( g ) = ∑ g [ σ ( g ) ≤ a ] ∑ d μ ( d ) ⌊ n d g ⌋ ⌊ m d g ⌋ σ ( g ) = ∑ g ∑ d [ σ ( g ) ≤ a ] ⋅ μ ( d ) ⌊ n d g ⌋ ⌊ m d g ⌋ σ ( g ) 令 t = d g = ∑ g ∣ t ∑ t [ σ ( g ) ≤ a ] ⋅ μ ( t g ) ⌊ n t ⌋ ⌊ m t ⌋ σ ( g ) = ∑ g ∣ t ∑ t [ σ ( g ) ≤ a ] ⋅ μ ( t g ) ⌊ n t ⌋ ⌊ m t ⌋ σ ( g ) = ∑ t ⌊ n t ⌋ ⌊ m t ⌋ ∑ g ∣ t [ σ ( g ) ≤ a ] ⋅ μ ( t g ) σ ( g ) 令 h ( t ) = ∑ g ∣ t [ σ ( g ) ≤ a ] ⋅ μ ( t g ) σ ( g ) = ∑ t ⌊ n t ⌋ ⌊ m t ⌋ h ( t ) \begin{aligned} answer &= \sum_{i = 1}^{n} \sum_{j = 1}^{m} [\sigma (gcd (i, j)) \leq a] \sigma (gcd (i, j)) \\ 令 g = gc&d (i, j) \\ &= \sum_{g} \sum_i\sum_j [\sigma (g) \leq a]\sigma (g) \\ &= \sum_{g} [\sigma (g) \leq a] \sum_i \sum_j \sum_{d \mid gcd (\frac{i}{g}, \frac{j}{g})} \mu (d) \sigma (g) \\ &= \sum_{g} [\sigma (g) \leq a] \sum_{d} \mu (d) \sum_{dg \mid i} \sum_{dg \mid j} \sigma (g) \\ &= \sum_{g} [\sigma (g) \leq a] \sum_{d} \mu (d) \lfloor \frac{n}{dg} \rfloor \lfloor \frac{m}{dg} \rfloor \sigma (g) \\ &= \sum_{g} \sum_{d} [\sigma (g) \leq a] \cdot \mu (d) \lfloor \frac{n}{dg} \rfloor \lfloor \frac{m}{dg} \rfloor \sigma (g) \\ 令 t = dg& \\ &= \sum_{g \mid t} \sum_{t} [\sigma (g) \leq a] \cdot \mu (\frac{t}{g}) \lfloor \frac{n}{t} \rfloor \lfloor \frac{m}{t} \rfloor \sigma (g) \\ &= \sum_{g \mid t}\sum_t [\sigma (g) \leq a] \cdot \mu (\frac{t}{g}) \lfloor \frac{n}{t} \rfloor \lfloor \frac{m}{t} \rfloor \sigma (g) \\ &= \sum_t \lfloor \frac{n}{t} \rfloor \lfloor \frac{m}{t} \rfloor \sum_{g \mid t} [\sigma (g) \leq a] \cdot \mu (\frac{t}{g}) \sigma (g) \\ 令 h(t) = &\sum_{g \mid t}[\sigma (g) \leq a] \cdot \mu (\frac{t}{g}) \sigma (g) \\ &= \sum_t \lfloor \frac{n}{t} \rfloor \lfloor \frac{m}{t} \rfloor h (t) \end{aligned} answer令g=gc令t=dg令h(t)==i=1∑nj=1∑m[σ(gcd(i,j))≤a]σ(gcd(i,j))d(i,j)=g∑i∑j∑[σ(g)≤a]σ(g)=g∑[σ(g)≤a]i∑j∑d∣gcd(gi,gj)∑μ(d)σ(g)=g∑[σ(g)≤a]d∑μ(d)dg∣i∑dg∣j∑σ(g)=g∑[σ(g)≤a]d∑μ(d)⌊dgn⌋⌊dgm⌋σ(g)=g∑d∑[σ(g)≤a]⋅μ(d)⌊dgn⌋⌊dgm⌋σ(g)=g∣t∑t∑[σ(g)≤a]⋅μ(gt)⌊tn⌋⌊tm⌋σ(g)=g∣t∑t∑[σ(g)≤a]⋅μ(gt)⌊tn⌋⌊tm⌋σ(g)=t∑⌊tn⌋⌊tm⌋g∣t∑[σ(g)≤a]⋅μ(gt)σ(g)g∣t∑[σ(g)≤a]⋅μ(gt)σ(g)=t∑⌊tn⌋⌊tm⌋h(t)
我们离线求解,按照 a a a 排序,求解完 i − 1 i - 1 i−1 后,会加入一些 g g g,满足 a i − 1 ≤ σ ( g ) ≤ a i a_{i - 1} \leq \sigma (g) \leq a_i ai−1≤σ(g)≤ai,我们把这些 g g g 用类似埃筛的方法加入 h ( k ⋅ g ) ( k ∈ Z , k ⋅ g ≤ m i n ( n , m ) ) h (k\cdot g) (k \in \mathrm{Z}, k \cdot g \leq min (n, m)) h(k⋅g)(k∈Z,k⋅g≤min(n,m)),然后用树状数组动态维护前缀和就行了。
好妙, D J DJ DJ 是神仙。
//author : LH ——Who just can eat S??t
//worship WJC ——Who can f??k tourist up and down and loves 周歆恬
//worship YJX ——Who can f??k WJC up and down
#include <cmath>
#include <queue>
#include <vector>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <algorithm>
using namespace std;
#define fi first
#define se second
#define db double
#define LL long long
#define ULL unsigned long long
#define PII pair <int, int>
#define MP(x,y) make_pair (x, y)
#define rep(i,j,k) for (int i = (j); i <= (k); ++i)
#define per(i,j,k) for (int i = (j); i >= (k); --i)
template <typename T> T Max (T x, T y) { return x > y ? x : y; }
template <typename T> T Min (T x, T y) { return x < y ? x : y; }
template <typename T> T Abs (T x) { return x > 0 ? x : -x; }
template <typename T>
void read (T &x) {
x = 0; T f = 1;
char ch = getchar ();
while (ch < '0' || ch > '9') {
if (ch == '-') f = -1;
ch = getchar ();
}
while (ch >= '0' && ch <= '9') {
x = (x << 3) + (x << 1) + ch - '0';
ch = getchar ();
}
x *= f;
}
template <typename T, typename... Args>
void read (T &x, Args&... args) {
read (x); read (args...);
}
char For_Print[25];
template <typename T>
void write (T x) {
if (x == 0) { putchar ('0'); return; }
if (x < 0) { putchar ('-'); x = -x; }
int poi = 0;
while (x) {
For_Print[++poi] = x % 10 + '0';
x /= 10;
}
while (poi) putchar (For_Print[poi--]);
}
template <typename T>
void print (T x, char ch) {
write (x); putchar (ch);
}
const LL Mod = 1ll << 31;
LL square (LL x) { return (x * x) % Mod; }
void DEL (LL &x, LL y) { ((x -= y) < 0) && (x += Mod); }
void ADD (LL &x, LL y) { ((x += y) >= Mod) && (x -= Mod); }
const int Maxn = 1e5;
int t;
LL ans[Maxn + 5];
int cnt, primes[Maxn + 5];
LL px[Maxn + 5], sigma[Maxn + 5], mu[Maxn + 5];
bool vis[Maxn + 5];
deque <PII> c;
void Euler () {
mu[1] = 1; sigma[1] = 1;
rep (i, 2, Maxn) {
if (!vis[i]) {
primes[++cnt] = i;
sigma[i] = i + 1;
mu[i] = -1;
px[i] = i;
}
rep (j, 1, cnt) {
if (primes[j] > Maxn / i) break;
vis[i * primes[j]] = 1;
if (i % primes[j] == 0) {
mu[i * primes[j]] = 0;
px[i * primes[j]] = px[i] * primes[j];
sigma[i * primes[j]] = sigma[i / px[i]] * (sigma[px[i]] + px[i] * primes[j]);
break;
}
mu[i * primes[j]] = -mu[i];
px[i * primes[j]] = primes[j];
sigma[i * primes[j]] = sigma[i] * (primes[j] + 1);
}
}
rep (i, 1, Maxn)
c.push_back (MP (sigma[i], i));
sort (c.begin (), c.end ());
}
struct qst {
int n, m, id; LL a;
}q[Maxn + 5];
bool cmp (qst x, qst y) {
return x.a < y.a;
}
LL BIT[Maxn + 5];
LL md (LL x, LL M) {
return (x % M + M) % M;
}
int lowbit (int x) { return x & -x; }
void Update (int Index, LL x) {
for (int i = Index; i <= Maxn; i += lowbit (i))
BIT[i] += x;
}
LL Sum (int Index) {
LL res = 0;
for (int i = Index; i >= 1; i -= lowbit (i))
res += BIT[i];
return res;
}
LL Query (int l, int r) {
return md (Sum (r) - Sum (l - 1), Mod);
}
void Add (int x) {
for (int i = x; i <= Maxn; i += x)
Update (i, mu[i / x] * sigma[x]);
}
int main () {
// freopen ("D:\\lihan\\1.in", "r", stdin);
// freopen ("D:\\lihan\\1.out", "w", stdout);
Euler ();
read (t);
rep (i, 1, t)
{ read (q[i].n, q[i].m, q[i].a); q[i].id = i; }
sort (q + 1, q + 1 + t, cmp);
rep (i, 1, t) {
while (c.size () && c.begin () -> fi <= q[i].a)
{ Add (c.begin () -> se); c.pop_front (); }
int n = q[i].n, m = q[i].m;
if (n > m) swap (n, m);
int l = 1, r;
LL res = 0;
while (l <= n) {
r = Min (n / (n / l), m / (m / l));
ADD (res, (n / l) * (m / l) % Mod * Query (l, r) % Mod);
l = r + 1;
}
ans[q[i].id] = res;
}
rep (i, 1, t) print (ans[i], '\n');
return 0;
}
五、约数个数和
∑ i = 1 n ∑ j = 1 m d ( i j ) = ∑ i = 1 n ∑ j = 1 m ∑ p ∣ i j 1 = ∑ p = 1 n m ∑ i = 1 n ∑ j = 1 m [ p ∣ i j ] = ∑ p = 1 n m ∑ i = 1 n ∑ j = 1 m [ p g c d ( i , p ) ∣ j ] = ∑ p = 1 n m ∑ i = 1 n ⌊ m p g c d ( i , p ) ⌋ = ∑ g = 1 n ∑ p = 1 n m ∑ i = 1 n ⌊ m ⋅ g p ⌋ [ g c d ( i , p ) = = g ] = ∑ g = 1 n ∑ p = 1 n m ∑ i = 1 n ⌊ m ⋅ g p ⌋ ∑ d ∣ g c d ( i g , p g ) μ ( d ) = ∑ g = 1 n ∑ p = 1 n m g ∑ i = 1 n g ⌊ m ⋅ g p ⋅ i ⌋ ∑ d ∣ g c d ( i , p ) μ ( d ) = ∑ g = 1 n ∑ p = 1 n m g ∑ i = 1 n g ∑ d ∣ g c d ( i , p ) ⌊ m ⋅ g p ⋅ i ⌋ μ ( d ) = ∑ g = 1 n ∑ d ∑ d ∣ p n m g ∑ d ∣ i n g ⌊ m ⋅ g p ⋅ i ⌋ μ ( d ) = ∑ g = 1 n ∑ d ∑ p = 1 n m g d ∑ i = 1 n g d ⌊ m ⋅ g p ⋅ i ⋅ d 2 ⌋ μ ( d ) = ∑ g = 1 n ∑ d ∑ p = 1 n m g d ∑ i = 1 n g d ⌊ m ⋅ g p ⋅ i ⋅ d 2 ⌋ μ ( d ) \begin{aligned} &\sum_{i = 1}^{n} \sum_{j = 1}^{m} d (ij) \\ = &\sum_{i = 1}^{n} \sum_{j = 1}^{m} \sum_{p \mid ij} 1 \\ = &\sum_{p = 1}^{nm} \sum_{i = 1}^{n} \sum_{j = 1}^{m} [p \mid ij] \\ = &\sum_{p = 1}^{nm} \sum_{i = 1}^{n} \sum_{j = 1}^{m} [\frac{p}{gcd (i, p)} \mid j] \\ = &\sum_{p = 1}^{nm} \sum_{i = 1}^{n} \lfloor \frac{m}{\frac{p}{gcd (i, p)}} \rfloor \\ = &\sum_{g = 1}^{n} \sum_{p = 1}^{nm} \sum_{i = 1}^{n} \lfloor \frac{m \cdot g}{p} \rfloor [gcd (i, p) == g] \\ = &\sum_{g = 1}^{n}\sum_{p = 1}^{nm} \sum_{i = 1}^{n} \lfloor \frac{m \cdot g}{p} \rfloor \sum_{d \mid gcd (\frac{i}{g}, \frac{p}{g})} \mu(d) \\ = &\sum_{g = 1}^{n}\sum_{p = 1}^{\frac{nm}{g}} \sum_{i = 1}^{\frac{n}{g}} \lfloor \frac{m \cdot g}{p \cdot i} \rfloor \sum_{d \mid gcd (i, p)} \mu(d) \\ = &\sum_{g = 1}^{n}\sum_{p = 1}^{\frac{nm}{g}} \sum_{i = 1}^{\frac{n}{g}} \sum_{d \mid gcd (i, p)} \lfloor \frac{m \cdot g}{p \cdot i} \rfloor \mu(d) \\ = &\sum_{g = 1}^{n}\sum_{d}\sum_{d \mid p}^{\frac{nm}{g}} \sum_{d \mid i}^{\frac{n}{g}} \lfloor \frac{m \cdot g}{p \cdot i} \rfloor \mu(d) \\ = &\sum_{g = 1}^{n}\sum_{d}\sum_{p = 1}^{\frac{nm}{gd}} \sum_{i = 1}^{\frac{n}{gd}} \lfloor \frac{m \cdot g}{p \cdot i \cdot d^2} \rfloor \mu(d) \\ = &\sum_{g = 1}^{n}\sum_{d}\sum_{p = 1}^{\frac{nm}{gd}} \sum_{i = 1}^{\frac{n}{gd}} \lfloor \frac{m \cdot g}{p \cdot i \cdot d^2} \rfloor \mu(d) \end{aligned} ===========i=1∑nj=1∑md(ij)i=1∑nj=1∑mp∣ij∑1p=1∑nmi=1∑nj=1∑m[p∣ij]p=1∑nmi=1∑nj=1∑m[gcd(i,p)p∣j]p=1∑nmi=1∑n⌊gcd(i,p)pm⌋g=1∑np=1∑nmi=1∑n⌊pm⋅g⌋[gcd(i,p)==g]g=1∑np=1∑nmi=1∑n⌊pm⋅g⌋d∣gcd(gi,gp)∑μ(d)g=1∑np=1∑gnmi=1∑gn⌊p⋅im⋅g⌋d∣gcd(i,p)∑μ(d)g=1∑np=1∑gnmi=1∑gnd∣gcd(i,p)∑⌊p⋅im⋅g⌋μ(d)g=1∑nd∑d∣p∑gnmd∣i∑gn⌊p⋅im⋅g⌋μ(d)g=1∑nd∑p=1∑gdnmi=1∑gdn⌊p⋅i⋅d2m⋅g⌋μ(d)g=1∑nd∑p=1∑gdnmi=1∑gdn⌊p⋅i⋅d2m⋅g⌋μ(d)