咱們用$\phi(n)$表示歐拉函數html
定義:$\phi(n)$表示對於整數$n$,小於等於$n$中與$n$互質的數的個數ios
1.$\phi(n)$爲積性函數c++
證實:函數
此處證實須要用到下面計算方法1中的內容,建議先看後面再回過頭來看這裏學習
假設存在$p,q$,且$p*q=n$ui
將$n,p,q$進行質因數分解spa
$n=a_1^{p_1}*a_2^{p_2}...*a_k^{p_k}$.net
$p=a_1^{p_1}*a_2^{p_2}...*a_m^{p_m}$code
$q=a_{m+1}^{p_{m+1}}*a_{m+2}^{m+2}...*a_k^{p_k}$htm
那麼
$\varphi \left( n\right) =n\prod ^{k}_{i=1}\left( 1-\dfrac {1}{p_{i}}\right)$
$\varphi \left( a\right) =a\prod ^{m}_{i=1}\left( 1-\dfrac {1}{p_{i}}\right)$
$\varphi \left( b\right) =b\prod ^{k}_{i=m+1}\left( 1-\dfrac {1}{p_{i}}\right)$
由於$n=a*b$
顯然
$\varphi \left( n\right) =\varphi \left( a\right) \varphi \left( b\right)$
這種方法也是常見的證實一個函數是積性函數的方法
2.$\sum_{d|n}\phi(d)=n$
3.$1$到$n$中與$n$互質的數的和爲$n*\dfrac{\phi(n)}{2}(n>1)$
證實:若$gcd(n, i) = 1$,那麼$gcd(n, n - i) = 1$
所以與$n$互質的數都是成對出現的。且每一對的和都爲$n$
這樣最終答案爲$n * \frac{\phi(n)}{2}$
4. $a^{\phi(n)} \equiv 1 \pmod n$
:
假設咱們須要計算$\phi(n)$
分狀況討論
很明顯,答案爲$1$
根據素數的定義,答案爲$n-1$
(僅有$n$與$n$不互質)
咱們已經知道了$n$爲素數的狀況
不妨對$n$進行質因數分解
設$n=a_1^{p_1}*a_2^{p_2}...*a_k^{p_k}$
假設$k=1$
那麼$\phi(p^k)=p^k-p^{k-1}$
證實:
考慮容斥,與一個數互素的數的個數就是這個數減去與它不互素的數的個數
由於$p$是素數,因此在$p^k$中與其不互素的數爲$1*p$,$2*p$....$p^{k-1}*p$,有$p^{k-1}$個
得證
當$k\neq 1$時
$$\phi(n)$$
$$=\varphi \left( a^{p_{1}}_{1}a^{p_{2}\ldots }_{2}a^{Pk}_{k}\right)$$
$$=\prod ^{k}_{i=1}a^{P_i}-a^{P_{i}-1}_{i}$$
$$=\prod ^{k}_{i=1}a^{Pi}_{i}(1-\dfrac {1}{p_{i}})$$
$$=n*\prod ^{k}_{i=1}(1-\dfrac {1}{p_{i}})$$
#include<iostream> #include<cstdio> #include<cstring> #include<algorithm> using namespace std; const int MAXN = 1e7 + 10; int p, ans = 1, N; void GetPhi() { for(int i = 2; i * i <= p; i++) { if(p % i == 0) { int now = i - 1; p /= i; while(p % i == 0) now = now * i, p /= i; ans = ans * now; } } if(p != 1) ans *= (p - 1); } int main() { cin >> p; N = p; GetPhi(); cout << ans; return 0; }
由於歐拉函數是積性函數
所以能夠使用線性篩法
若$p$爲素數,則$\varphi \left( p\right) =p-1$
證實:
在$1-p$中,只有$(p,p)\neq1$
若$i\ mod\ p \neq 0$,且$p$爲素數
則$\varphi \left( i*p\right) =\varphi \left( i\right) *\varphi \left( p\right)$
$=\varphi \left( i\ast p\right) =\varphi \left( i\right) \ast \left( p-1\right)$
這一步同時利用了性質1和歐拉函數的積性
若$i\ mod \ p = 0$,且$p$爲素數,
則$\varphi \left( i\ast p\right) =\varphi \left( i\right) \ast p$
證實:
沒怎麼看懂,丟一個連接
http://blog.csdn.net/Lytning/article/details/24432651
#include<bits/stdc++.h>
using namespace std; const int MAXN = 3e5 + 10; void GetPhi(int N) { static int phi[MAXN], vis[MAXN], prime[MAXN], tot = 0; for(int i = 2; i <= N; i++) { if(!vis[i]) prime[++tot] = i, phi[i] = i - 1; for(int j = 1; j <= tot && i * prime[j] <= N; j++) { vis[i * prime[j]] = 1; if(!(i % prime[j])) {phi[i * prime[j]] = phi[i] * prime[j]; break;} else phi[i * prime[j]] = phi[i] * (prime[j] - 1); } } while(cin >> N) cout << phi[N] << endl; } int main() { GetPhi(100); return 0; }
放幾道水題
http://poj.org/problem?id=2407
http://poj.org/problem?id=2478
https://www.luogu.org/problemnew/show/P2158