前置知识
互质:互质是公约数只有1的两个整数,叫做互质整数。
欧拉函数定义
1 ∼ N − 1 1∼N-1 1∼N−1中与N互质的数的个数被称为欧拉函数,记为 ϕ ( N ) \phi(N) ϕ(N)。
若在算数基本定理中, N = p 1 a 1 p 2 a 2 . . . p m a m N=p_1^{a_1}p_2^{a_2}...p_m^{a_m} N=p1a1p2a2...pmam,则:
ϕ ( N ) = N ⋅ p 1 − 1 p 1 ⋅ p 2 − 1 p 2 ⋅ . . . ⋅ p m − 1 p m \phi(N)=N\cdot\frac{p_1-1}{p_1}\cdot\frac{p_2-1}{p_2}\cdot...\cdot\frac{p_m-1}{p_m} ϕ(N)=N⋅p1p1−1⋅p2p2−1⋅...⋅pmpm−1
欧拉函数推导
首先我们要知道 1 , 2 , 3... N − 1 , N 1,2,3...N-1,N 1,2,3...N−1,N与 N N N互质的个数是 1 ∼ N 1∼N 1∼N数列去除N的质因子的倍数。
例如 N = 10 , 即 1 , 2 , 3 , 4 , 5 , 6 , 7 , 8 , 9 , 10 N=10,即1,2,3,4,5,6,7,8,9,10 N=10,即1,2,3,4,5,6,7,8,9,10去除 N N N的质因子的倍数 , 则 1 , 2 , 3 , 4 , 5 , 6 , 7 , 8 , 9 , 10 . ,则1,\bcancel{2},3,\bcancel{4},\bcancel{5},\bcancel{6},7,\bcancel{8},9,\bcancel{10}. ,则1,2 ,3,4 ,5 ,6 ,7,8 ,9,10 .
显然, 1 , 3 , 7 , 9 1,3,7,9 1,3,7,9与 10 10 10互质。
由上方结论使用容斥原理进行数学推导如下:
∵ N = p 1 a 1 p 2 a 2 . . . p m a m \because N=p_1^{a_1}p_2^{a_2}...p_m^{a_m} ∵N=p1a1p2a2...pmam
①.从1~n中去掉 p 1 , p 2 , . . . , p k p_1,p_2,...,p_k p1,p2,...,pk的所有倍数的个数,即
n ← n − n p 1 − n p 2 − . . . − n p k n←n-\frac{n}{p_1}-\frac{n}{p_2}-...-\frac{n}{p_k} n←n−p1n−p2n−...−pkn
②.由容斥原理, p i ⋅ p j p_i \cdot p_j pi⋅pj的倍数个数被①减了两次,所以加上所有 p i ⋅ p j p_i\cdot p_j pi⋅pj的倍数的个数(其中 p i , p j p_i,p_j pi,pj是 p 1 ∼ p k p_1∼p_k p1∼pk的组合),即
n ← n + n p 1 ⋅ p 2 + n p 1 ⋅ p 3 + . . . + n p k − 1 ⋅ p k n←n+\frac{n}{p_1\cdot p_2}+\frac{n}{p_1\cdot p_3}+...+\frac{n}{p_{k-1}\cdot p_k} n←n+p1⋅p2n+p1⋅p3n+...+pk−1⋅pkn
③.减去所有 p i ⋅ p j ⋅ p k p_i\cdot p_j \cdot p_k pi⋅pj⋅pk的倍数个数,即
n ← n − n p 1 ⋅ p 2 ⋅ p 3 − n p 1 ⋅ p 2 ⋅ p 4 − . . . − n p k − 2 ⋅ p k − 1 ⋅ p k n←n-\frac{n}{p_1\cdot p_2\cdot p_3}-\frac{n}{p_1\cdot p_2 \cdot p_4}-...-\frac{n}{p_{k-2}\cdot p_{k-1}\cdot p_k} n←n−p1⋅p2⋅p3n−p1⋅p2⋅p4n−...−pk−2⋅pk−1⋅pkn
④.同理,加上所有 p i ⋅ p j ⋅ p k ⋅ p l p_i\cdot p_j \cdot p_k \cdot p_l pi⋅pj⋅pk⋅pl的倍数个数,即
n ← n + n p 1 ⋅ p 2 ⋅ p 3 ⋅ p 4 + n p 1 ⋅ p 2 ⋅ p 3 ⋅ p 5 + . . . + n p k − 3 ⋅ p k − 2 ⋅ p k − 1 ⋅ p k n←n+\frac{n}{p_1\cdot p_2\cdot p_3\cdot p_4}+\frac{n}{p_1\cdot p_2 \cdot p_3\cdot p_5}+...+\frac{n}{p_{k-3}\cdot p_{k-2}\cdot p_{k-1}\cdot {p_k}} n←n+p1⋅p2⋅p3⋅p4n+p1⋅p2⋅p3⋅p5n+...+pk−3⋅pk−2⋅pk−1⋅pkn
KaTeX parse error: Can't use function '\mathord' in text mode at position 1: \̲m̲a̲t̲h̲o̲r̲d̲{\varvdots\rule…
因此,
ϕ ( n ) = n − n p 1 − n p 2 − . . . − n p k + n p 1 ⋅ p 2 + n p 1 ⋅ p 3 + . . . + n p k − 1 ⋅ p k − n p 1 ⋅ p 2 ⋅ p 3 − n p 1 ⋅ p 2 ⋅ p 4 − . . . − n p k − 2 ⋅ p k − 1 ⋅ p k + n p 1 ⋅ p 2 ⋅ p 3 ⋅ p 4 + n p 1 ⋅ p 2 ⋅ p 3 ⋅ p 5 + . . . + n p k − 3 ⋅ p k − 2 ⋅ p k − 1 ⋅ p k − . . . \phi(n)=n-\frac{n}{p_1}-\frac{n}{p_2}-...-\frac{n}{p_k}\\ +\frac{n}{p_1\cdot p_2}+\frac{n}{p_1\cdot p_3}+...+\frac{n}{p_{k-1}\cdot p_k}\\ -\frac{n}{p_1\cdot p_2\cdot p_3}-\frac{n}{p_1\cdot p_2 \cdot p_4}-...-\frac{n}{p_{k-2}\cdot p_{k-1}\cdot p_k}\\ +\frac{n}{p_1\cdot p_2\cdot p_3\cdot p_4}+\frac{n}{p_1\cdot p_2 \cdot p_3\cdot p_5}+...+\frac{n}{p_{k-3}\cdot p_{k-2}\cdot p_{k-1}\cdot {p_k}}\\ -... ϕ(n)=n−p1n−p2n−...−pkn+p1⋅p2n+p1⋅p3n+...+pk−1⋅pkn−p1⋅p2⋅p3n−p1⋅p2⋅p4n−...−pk−2⋅pk−1⋅pkn+p1⋅p2⋅p3⋅p4n+p1⋅p2⋅p3⋅p5n+...+pk−3⋅pk−2⋅pk−1⋅pkn−...
也就是n减去奇数个质因子的倍数个数,加上偶数个质因子的倍数个数,循环往复。将上式等价变形,得到
ϕ ( n ) = n ⋅ ( 1 − 1 p 1 ) ⋅ ( 1 − 1 p 2 ) . . . ⋅ ( 1 − 1 p k ) \phi(n)=n\cdot(1-\frac{1}{p_1})\cdot(1-\frac{1}{p_2})...\cdot(1-\frac{1}{p_k}) ϕ(n)=n⋅(1−p11)⋅(1−p21)...⋅(1−pk1)
证必。
代码模板
int phi(int x)
{
int res = x;
for (int i = 2; i <= x / i; i ++ )
if (x % i == 0)
{
res = res / i * (i - 1);
while (x % i == 0) x /= i;
}
if (x > 1) res = res / x * (x - 1);
return res;
}