常见积性函数
- μ \mu μ
- φ φ φ
- d ( n ) d(n) d(n): n n n的约数个数
- σ ( n ) σ(n) σ(n): n n n的约数和
- ϵ ( n ) ϵ(n) ϵ(n):单位元函数, e ( n ) = [ n = 1 ] e(n)=[n=1] e(n)=[n=1] (完全积性)
- I ( n ) I(n) I(n): I ( n ) = 1 I(n)=1 I(n)=1(完全积性)
- i d ( n ) id(n) id(n): i d ( n ) = n id(n)=n id(n)=n (完全积性)
∑ d ∣ n φ ( d ) = n \sum_{d|n}φ(d)=n d∣n∑φ(d)=n ∑ d ∣ n μ ( d ) = [ n = 1 ] \sum_{d|n}\mu(d)=[n=1] d∣n∑μ(d)=[n=1] ∑ d ∣ n φ ( d ) × d = n × φ ( n ) + [ n = 1 ] 2 \sum_{d|n}φ(d)\times d=\frac{n\times φ(n)+[n=1]}{2} d∣n∑φ(d)×d=2n×φ(n)+[n=1]
公式推导
求 ∑ i = 1 n f ( i ) \sum_{i=1}^nf(i) i=1∑nf(i)
狄利克雷卷积
定义两个数论函数
f
,
g
f,g
f,g的狄利克雷卷积为
h
h
h
(
f
∗
g
)
(
n
)
=
h
(
n
)
⇔
h
(
n
)
=
∑
d
∣
n
f
(
d
)
×
g
(
n
d
)
=
∑
d
∣
n
g
(
d
)
×
f
(
n
d
)
(f*g)(n)=h(n) \Leftrightarrow\ h(n)=\sum_{d|n}f(d)\times g(\frac{n}{d})=\sum_{d|n}g(d)\times f(\frac{n}{d})
(f∗g)(n)=h(n)⇔ h(n)=d∣n∑f(d)×g(dn)=d∣n∑g(d)×f(dn)
如果 f , g f,g f,g 是积性函数则 h h h 函数也是一个积性函数。
证明:令 n = x ∗ y ∧ ( x , y ) = 1 n=x*y\wedge(x,y)=1 n=x∗y∧(x,y)=1。
h ( n ) = ∑ d ∣ n f ( d ) g ( n d ) ⇔ h ( x y ) = ∑ d 1 ∣ x , d 2 ∣ y f ( d 1 d 2 ) g ( x d 1 y d 2 ) = ∑ d 1 ∣ x , d 2 ∣ y f ( d 1 ) f ( d 2 ) g ( x d 1 ) g ( y d 2 ) h(n)=\sum_{d|n}f(d)g(\frac n d)\Leftrightarrow h(xy)=\sum_{d_1|x,d_2|y}f(d_1d_2)g(\frac x {d_1}\frac y {d_2})=\sum_{d_1|x,d_2|y}f(d_1)f(d_2)g(\frac x{d_1})g(\frac y{d_2}) h(n)=d∣n∑f(d)g(dn)⇔h(xy)=d1∣x,d2∣y∑f(d1d2)g(d1xd2y)=d1∣x,d2∣y∑f(d1)f(d2)g(d1x)g(d2y) = ∑ d 1 ∣ x f ( d 1 ) g ( x d 1 ) ∑ d 2 ∣ y f ( d 2 ) g ( y d 2 ) = h ( x ) h ( y ) =\sum_{d_1|x}f(d_1)g(\frac x{d_1})\sum_{d_2|y}f(d_2)g(\frac{y}{d_2})=h(x)h(y) =d1∣x∑f(d1)g(d1x)d2∣y∑f(d2)g(d2y)=h(x)h(y)
卷积满足乘法交换/结合律/分配律 (这不是重点😓)
f ∗ g = g ∗ f f*g=g*f f∗g=g∗f
( f ∗ g ) ∗ h = f ∗ ( g ∗ h ) (f*g)*h=f*(g*h) (f∗g)∗h=f∗(g∗h)
( f + g ) ∗ h = f ∗ h + g ∗ h (f+g)*h=f*h+g*h (f+g)∗h=f∗h+g∗h
杜教筛
杜教筛是一种以低于线性筛时间复杂度筛积性函数前缀和的筛法。
将所求问题定义成函数,令
S
(
n
)
=
∑
i
=
1
n
f
(
i
)
S(n)=\sum_{i=1}^nf(i)
S(n)=∑i=1nf(i)
先推
∑
i
=
1
n
h
(
i
)
\sum_{i=1}^nh(i)
∑i=1nh(i)
∑
i
=
1
n
h
(
i
)
\sum_{i=1}^nh(i)
i=1∑nh(i)
=
∑
i
=
1
n
∑
d
∣
i
g
(
d
)
×
f
(
i
d
)
=\sum_{i=1}^n\sum_{d|i}g(d)\times f(\frac{i}{d})
=i=1∑nd∣i∑g(d)×f(di)
=
∑
d
=
1
n
g
(
d
)
∑
i
=
1
⌊
n
d
⌋
f
(
i
)
=\sum_{d=1}^ng(d) \sum_{i=1}^{\lfloor\frac{n}{d}\rfloor}f(i)
=d=1∑ng(d)i=1∑⌊dn⌋f(i)
=
∑
d
=
1
n
g
(
d
)
S
(
⌊
n
d
⌋
)
=\sum_{d=1}^ng(d)S(\lfloor\frac{n}{d}\rfloor)
=d=1∑ng(d)S(⌊dn⌋)
将
d
=
1
d=1
d=1的一项单独提出来
⇓
\Downarrow
⇓
=
g
(
1
)
×
S
(
n
)
+
∑
d
=
2
n
g
(
d
)
S
(
⌊
n
d
⌋
)
=g(1)\times S(n)+\sum_{d=2}^ng(d)S(\lfloor\frac{n}{d}\rfloor)
=g(1)×S(n)+d=2∑ng(d)S(⌊dn⌋)
g
(
1
)
×
S
(
n
)
=
∑
i
=
1
n
h
(
i
)
−
∑
d
=
2
n
g
(
d
)
S
(
⌊
n
d
⌋
)
g(1)\times S(n)=\sum_{i=1}^nh(i)-\sum_{d=2}^ng(d)S(\lfloor\frac{n}{d}\rfloor)
g(1)×S(n)=i=1∑nh(i)−d=2∑ng(d)S(⌊dn⌋)
一般套路情况下
g
(
1
)
=
1
g(1)=1
g(1)=1
时间复杂度是 O ( n 2 3 ) O(n^\frac{2}{3}) O(n32),大概就是 S S S 函数可以记忆化递归,另外设定一个阀值 M M M 预处理 S ( 1 ∼ M ) S(1\sim M) S(1∼M) 内的信息。
当 M M M 大约取 n 2 3 n^\frac{2}{3} n32 最优,让预处理和后面记忆化的时间复杂度差不多。
实现
因为
g
,
h
g,h
g,h都是我们自己选择构造出来的,通过上面化出的最后式子来看
我们希望
h
(
i
)
h(i)
h(i)的前缀和是好求的,换言之就是很快就能求出来的
然后按
S
(
⌊
n
d
⌋
)
S(\lfloor\frac{n}{d}\rfloor)
S(⌊dn⌋)可以对后面式子进行分块处理
至于怎么选择
g
g
g和
h
h
h,说实话只能观察式子,或者结合以前的做题经验套路
常见卷积
Ⅰ
S
(
n
)
=
∑
i
=
1
n
μ
(
i
)
S(n)=\sum_{i=1}^n\mu(i)
S(n)=∑i=1nμ(i)
因为知道在莫比乌斯反演有个关于莫比乌斯函数的公式
∑
d
∣
n
μ
(
d
)
=
[
n
=
1
]
\sum_{d|n}\mu(d)=[n=1]
d∣n∑μ(d)=[n=1]
很容易想到
ϵ
=
μ
∗
I
ϵ=\mu*I
ϵ=μ∗I
ϵ
(
n
)
=
∑
d
∣
n
μ
(
d
)
×
I
(
n
d
)
=
∑
d
∣
n
μ
(
d
)
=
[
n
=
1
]
ϵ(n)=\sum_{d|n}\mu(d)\times I(\frac{n}{d})=\sum_{d|n}\mu(d)=[n=1]
ϵ(n)=d∣n∑μ(d)×I(dn)=d∣n∑μ(d)=[n=1]
Ⅱ
s
(
n
)
=
∑
i
=
1
n
φ
(
i
)
s(n)=\sum_{i=1}^nφ(i)
s(n)=∑i=1nφ(i)
上述提到过一个公式
n
=
∑
d
∣
n
φ
(
d
)
n=\sum_{d|n}φ(d)
n=d∣n∑φ(d)
所以我们想到
i
d
=
φ
∗
I
id=φ*I
id=φ∗I
i
d
(
n
)
=
∑
d
∣
n
φ
(
d
)
×
I
(
n
d
)
=
∑
d
∣
n
φ
(
d
)
=
n
id(n)=\sum_{d|n}φ(d)\times I(\frac{n}{d})=\sum_{d|n}φ(d)=n
id(n)=d∣n∑φ(d)×I(dn)=d∣n∑φ(d)=n
Ⅲ
S
(
n
)
=
∑
i
=
1
n
φ
(
i
)
×
i
S(n)=\sum_{i=1}^nφ(i)\times i
S(n)=∑i=1nφ(i)×i
乍一看似乎配不出什么鸟玩意儿
尝试从狄利克雷卷积入手
h
=
f
∗
g
=
∑
d
∣
n
f
(
d
)
×
g
(
n
d
)
=
∑
d
∣
n
φ
(
d
)
×
d
×
g
(
n
d
)
h=f*g=\sum_{d|n}f(d)\times g(\frac{n}{d})=\sum_{d|n}φ(d)\times d\times g(\frac{n}{d})
h=f∗g=d∣n∑f(d)×g(dn)=d∣n∑φ(d)×d×g(dn)
我们想着如果能把
f
f
f顺带的多余的
d
d
d除掉,尝试给
g
g
g配成
i
d
id
id
h
=
∑
d
∣
n
φ
(
d
)
×
d
×
n
d
=
∑
d
∣
n
{
φ
(
d
)
×
n
}
=
n
∑
d
∣
n
φ
(
d
)
=
n
2
h=\sum_{d|n}φ(d)\times d\times \frac{n}{d}=\sum_{d|n}\{φ(d)\times n\}=n\sum_{d|n}φ(d)=n^2
h=d∣n∑φ(d)×d×dn=d∣n∑{φ(d)×n}=nd∣n∑φ(d)=n2
i
2
i^2
i2的前缀和,欸好巧哦,由公式可以快速算出,所以成功配出来了
g
,
h
g,h
g,h
∑ i = 1 n i 2 = n × ( n + 1 ) × ( 2 n + 1 ) 6 \sum_{i=1}^ni^2=\frac{n\times (n+1)\times (2n+1)}{6} i=1∑ni2=6n×(n+1)×(2n+1)
∑ i = 1 n i 3 = ( ∑ i = 1 n i ) 2 = [ n × ( n + 1 ) 2 ] 2 = n 2 ( n + 1 ) 2 4 \sum_{i=1}^ni^3=(\sum_{i=1}^ni)^2=[\frac{n\times (n+1)}{2}]^2=\frac{n^2(n+1)^2}{4} i=1∑ni3=(i=1∑ni)2=[2n×(n+1)]2=4n2(n+1)2
习题集
Sum
- solution
前面提过了 i d = φ ∗ I , ϵ = μ ∗ I id=φ*I,ϵ=\mu*I id=φ∗I,ϵ=μ∗I,直接带公式即可 i d , ϵ id,ϵ id,ϵ就是 h h h, φ , μ φ,\mu φ,μ就是 f f f, I I I是 g g g
g ( 1 ) × S ( n ) = ∑ i = 1 n h ( i ) − ∑ d = 2 n g ( d ) S ( ⌊ n d ⌋ ) g(1)\times S(n)=\sum_{i=1}^nh(i)-\sum_{d=2}^ng(d)S(\lfloor\frac{n}{d}\rfloor) g(1)×S(n)=i=1∑nh(i)−d=2∑ng(d)S(⌊dn⌋) - code
#include <cstdio>
#include <map>
using namespace std;
#define maxn 2000000
#define int long long
map < int, int > mp1, mp2;
int cnt;
bool vis[maxn + 5];
int phi[maxn + 5], mu[maxn + 5], prime[maxn];
int sum1[maxn + 5], sum2[maxn + 5];
void init() {
mu[1] = phi[1] = 1;
for( int i = 2;i <= maxn;i ++ ) {
if( ! vis[i] ) prime[++ cnt] = i, mu[i] = -1, phi[i] = i - 1;
for( int j = 1;j <= cnt && 1ll * i * prime[j] <= maxn;j ++ ) {
vis[i * prime[j]] = 1;
if( i % prime[j] == 0 ) {
mu[i * prime[j]] = 0;
phi[i * prime[j]] = phi[i] * prime[j];
break;
}
mu[i * prime[j]] = -mu[i];
phi[i * prime[j]] = phi[i] * ( prime[j] - 1 );
}
}
for( int i = 1;i <= maxn;i ++ )
sum1[i] = sum1[i - 1] + phi[i], sum2[i] = sum2[i - 1] + mu[i];
}
int find_phi( int n ) {
if( n <= maxn ) return sum1[n];
if( mp1[n] ) return mp1[n];
int ans = n * ( n + 1 ) / 2;
int r;
for( int i = 2;i <= n;i = r + 1 ) {
r = n / ( n / i );
ans -= ( r - i + 1 ) * find_phi( n / i );
}
return mp1[n] = ans;
}
int find_mu( int n ) {
if( n <= maxn ) return sum2[n];
if( mp2[n] ) return mp2[n];
int ans = 1;
int r;
for( int i = 2;i <= n;i = r + 1 ) {
r = n / ( n / i );
ans -= ( r - i + 1 ) * find_mu( n / i );
}
return mp2[n] = ans;
}
signed main() {
init();
int T, n;
scanf( "%lld", &T );
while( T -- ) {
scanf( "%lld", &n );
printf( "%lld %lld\n", find_phi( n ), find_mu( n ) );
}
return 0;
}
神犇和蒟蒻
- solution
A A A很简单, i 2 i^2 i2的 μ \mu μ一定是 0 0 0,所以 A A A一定是 1 1 1
考虑 B B B,这里要用到欧拉函数的一个性质
如果 i % j = 0 i\% j=0 i%j=0,则 φ ( i × j ) = φ ( i ) × j φ(i\times j)=φ(i)\times j φ(i×j)=φ(i)×j
所以 φ ( i 2 ) = φ ( i ) × i φ(i^2)=φ(i)\times i φ(i2)=φ(i)×i,就神奇的转化为了上面的应用公式三 - code
#include <cstdio>
#include <map>
using namespace std;
#define int long long
#define mod 1000000007
#define maxn 1000000
map < int, int > mp;
int inv6, inv2, cnt;
bool vis[maxn + 5];
int prime[maxn], sum[maxn + 5], phi[maxn + 5];
void init() {
phi[1] = 1;
for( int i = 2;i <= maxn;i ++ ) {
if( ! vis[i] ) prime[++ cnt] = i, phi[i] = i - 1;
for( int j = 1;j <= cnt && i * prime[j] <= maxn;j ++ ) {
vis[i * prime[j]] = 1;
if( i % prime[j] == 0 ) {
phi[i * prime[j]] = phi[i] * prime[j];
break;
}
phi[i * prime[j]] = phi[i] * ( prime[j] - 1 );
}
}
for( int i = 1;i <= maxn;i ++ )
sum[i] = ( sum[i - 1] + i * phi[i] ) % mod;
}
int find_phi( int n ) {
if( n <= maxn ) return sum[n];
if( mp[n] ) return mp[n];
int ans = n * ( n + 1 ) % mod * ( 2 * n + 1 ) % mod * inv6 % mod;
for( int l = 2, r;l <= n;l = r + 1 ) {
r = n / ( n / l );
ans -= ( l + r ) * ( r - l + 1 ) % mod * find_phi( n / l ) % mod * inv2 % mod;
if( ans < 0 ) ans += mod;
}
return mp[n] = ans;
}
int qkpow( int x, int y ) {
int ans = 1;
while( y ) {
if( y & 1 ) ans = ans * x % mod;
x = x * x % mod;
y >>= 1;
}
return ans;
}
signed main() {
init();
int n;
inv2 = qkpow( 2, mod - 2 );
inv6 = qkpow( 6, mod - 2 );
scanf( "%lld", &n );
printf( "1\n%lld\n", find_phi( n ) );
return 0;
}
简单的数学题
-
solution
取模的操作代码里加入就可以了,下面式子中就省略不写了
∑ i = 1 n ∑ j = 1 n i × j × g c d ( i , j ) = ∑ i = 1 n ∑ j = 1 n i × j ∑ d ∣ i , d ∣ j φ ( d ) = ∑ d = 1 n φ ( d ) ∑ d ∣ i ∑ d ∣ j i j \sum_{i=1}^n\sum_{j=1}^ni\times j\times gcd(i,j)=\sum_{i=1}^n\sum_{j=1}^ni\times j\sum_{d|i,d|j}φ(d)=\sum_{d=1}^nφ(d)\sum_{d|i}\sum_{d|j}ij i=1∑nj=1∑ni×j×gcd(i,j)=i=1∑nj=1∑ni×jd∣i,d∣j∑φ(d)=d=1∑nφ(d)d∣i∑d∣j∑ij
= ∑ d = 1 n φ ( d ) × d 2 ∑ i = 1 ⌊ n d ⌋ i ∑ j = 1 ⌊ n d ⌋ j = ∑ d = 1 n φ ( d ) × d 2 ( ∑ i = 1 ⌊ n d ⌋ i ) 2 =\sum_{d=1}^nφ(d)\times d^2\sum_{i=1}^{\lfloor\frac{n}{d}\rfloor}i\sum_{j=1}^{\lfloor\frac{n}{d}\rfloor}j=\sum_{d=1}^nφ(d)\times d^2(\sum_{i=1}^{\lfloor\frac{n}{d}\rfloor}i)^2 =d=1∑nφ(d)×d2i=1∑⌊dn⌋ij=1∑⌊dn⌋j=d=1∑nφ(d)×d2(i=1∑⌊dn⌋i)2
前面杜教筛,后面数论分块
-
code
#include <cstdio>
#include <map>
using namespace std;
#define int long long
#define maxn 5000000
map < int, int > mp;
int mod, cnt, inv;
bool vis[maxn + 5];
int prime[maxn], sum[maxn + 5], phi[maxn + 5];
void init() {
phi[1] = 1;
for( int i = 2;i <= maxn;i ++ ) {
if( ! vis[i] ) prime[++ cnt] = i, phi[i] = i - 1;
for( int j = 1;j <= cnt && i * prime[j] <= maxn;j ++ ) {
vis[i * prime[j]] = 1;
if( i % prime[j] == 0 ) {
phi[i * prime[j]] = phi[i] * prime[j] % mod;
break;
}
else
phi[i * prime[j]] = phi[i] * ( prime[j] - 1 ) % mod;
}
}
for( int i = 1;i <= maxn;i ++ )
sum[i] = ( sum[i - 1] + phi[i] * i % mod * i % mod ) % mod;
}
int calc( int n ) {
n %= mod;
return n * ( n + 1 ) / 2 % mod;
}
int js( int n ) {
n %= mod;
return n * ( n + 1 ) % mod * ( 2 * n + 1 ) % mod * inv % mod;
}
int solve( int n ) {
if( n <= maxn ) return sum[n];
if( mp[n] ) return mp[n];
int ans = calc( n ) * calc( n ) % mod, r;
for( int i = 2;i <= n;i = r + 1 ) {
r = n / ( n / i );
ans = ( ans - ( js( r ) - js( i - 1 ) ) * solve( n / i ) ) % mod;
}
return mp[n] = ans;
}
int qkpow( int x, int y ) {
int ans = 1;
while( y ) {
if( y & 1 ) ans = ans * x % mod;
x = x * x % mod;
y >>= 1;
}
return ans;
}
signed main() {
int n;
scanf( "%lld %lld", &mod, &n );
init();
inv = qkpow( 6, mod - 2 );
int r, ans = 0;
for( int l = 1;l <= n;l = r + 1 ) {
r = n / ( n / l );
ans = ( ans + calc( n / l ) * calc( n / l ) % mod * ( solve( r ) - solve( l - 1 ) ) + mod ) % mod;
}
printf( "%lld", ( ans + mod ) % mod );
return 0;
}