【学习笔记】莫比乌斯反演

本文深入探讨了引理在数论和算法中的应用,结合容斥原理解决了一系列数学问题。通过举例展示了如何利用引理和容斥定理计算数对的GCD(最大公约数)的幂次、数表的性质以及约数个数和等。文章通过严谨的数学推导,将复杂问题简化为易于理解的形式,展现了数学在编程问题解决中的强大能力。
摘要由CSDN通过智能技术生成

其实引理用得更多,人麻了


引理:

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)=dnμ(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)=dnF(d)=dnμ(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==dnμ(dn)pdF(p)pd=ptpnμ(tpn)F(p)=pF(p)tpnμ(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)=ndF(d)=ndμ(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} primex[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 igcd 的数的对数

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)answerh(x)==ijf(j)=ijμ(ij)F(j)=primef(prime)=primeprimejμ(primej)jnjm=jjnjmprimejμ(primej)primexμ(primex)=jjnjmh(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=1nj=1mgcd(i,j)kdijdkpdgcd(i,j)μ(p)pμ(p)ddkdpndpmddkpμ(p)dpndpmt=dpttntmdtdkμ(dt)h(x)=dxdkμ(dx)ttntmh(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=1nj=1mgcd(i,j)ijdijdij[gcd(i,j)==d]dijdijpgcd(di,dj)μ(p)pμ(p)ddpidpjdijpμ(p)dd1dpiidpjjlm=dpmpμ(p)dd12(1+lm)lmdpdpiiln=dpnpμ(p)d2d(1+lm)lmdp2(1+ln)lndppμ(p)d4(1+lm)lm(1+ln)lnp2dpμ(p)d4(1+lm)lm(1+ln)lnp2dt=dppμ(p)p24pptt(1+tm)tm(1+tn)tnpμ(p)p4ptt(1+tm)tm(1+tn)tnt4t(1+tm)tm(1+tn)tnptμ(p)ph(t)=tptμ(p)pt4(1+tm)tm(1+tn)tnh(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} answerg=gct=dgh(t)==i=1nj=1m[σ(gcd(i,j))a]σ(gcd(i,j))d(i,j)=gij[σ(g)a]σ(g)=g[σ(g)a]ijdgcd(gi,gj)μ(d)σ(g)=g[σ(g)a]dμ(d)dgidgjσ(g)=g[σ(g)a]dμ(d)dgndgmσ(g)=gd[σ(g)a]μ(d)dgndgmσ(g)=gtt[σ(g)a]μ(gt)tntmσ(g)=gtt[σ(g)a]μ(gt)tntmσ(g)=ttntmgt[σ(g)a]μ(gt)σ(g)gt[σ(g)a]μ(gt)σ(g)=ttntmh(t)

我们离线求解,按照 a a a 排序,求解完 i − 1 i - 1 i1 后,会加入一些 g g g,满足 a i − 1 ≤ σ ( g ) ≤ a i a_{i - 1} \leq \sigma (g) \leq a_i ai1σ(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(kg)(kZ,kgmin(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=1nj=1md(ij)i=1nj=1mpij1p=1nmi=1nj=1m[pij]p=1nmi=1nj=1m[gcd(i,p)pj]p=1nmi=1ngcd(i,p)pmg=1np=1nmi=1npmg[gcd(i,p)==g]g=1np=1nmi=1npmgdgcd(gi,gp)μ(d)g=1np=1gnmi=1gnpimgdgcd(i,p)μ(d)g=1np=1gnmi=1gndgcd(i,p)pimgμ(d)g=1nddpgnmdignpimgμ(d)g=1ndp=1gdnmi=1gdnpid2mgμ(d)g=1ndp=1gdnmi=1gdnpid2mgμ(d)

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值