【算法】求欧拉函数(包括完整的证明以及代码模板,建议收藏)

文章目录

    • 求欧拉函数

求欧拉函数

前置知识

互质:互质是公约数只有1的两个整数,叫做互质整数。

欧拉函数定义

1 ∼ N − 1 1∼N-1 1N1中与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)=Np1p11p2p21...pmpm1

欧拉函数推导

首先我们要知道 1 , 2 , 3... N − 1 , N 1,2,3...N-1,N 1,2,3...N1,N N N N互质的个数是 1 ∼ N 1∼N 1N数列去除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} nnp1np2n...pkn

②.由容斥原理, p i ⋅ p j p_i \cdot p_j pipj的倍数个数被①减了两次,所以加上所有 p i ⋅ p j p_i\cdot p_j pipj的倍数的个数(其中 p i , p j p_i,p_j pi,pj p 1 ∼ p k p_1∼p_k p1pk的组合),即

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} nn+p1p2n+p1p3n+...+pk1pkn

③.减去所有 p i ⋅ p j ⋅ p k p_i\cdot p_j \cdot p_k pipjpk的倍数个数,即

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} nnp1p2p3np1p2p4n...pk2pk1pkn

④.同理,加上所有 p i ⋅ p j ⋅ p k ⋅ p l p_i\cdot p_j \cdot p_k \cdot p_l pipjpkpl的倍数个数,即

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}} nn+p1p2p3p4n+p1p2p3p5n+...+pk3pk2pk1pkn
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)=np1np2n...pkn+p1p2n+p1p3n+...+pk1pknp1p2p3np1p2p4n...pk2pk1pkn+p1p2p3p4n+p1p2p3p5n+...+pk3pk2pk1pkn...
也就是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(1p11)(1p21)...(1pk1)

证必。

代码模板

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;
}

你可能感兴趣的:(算法,算法)