# 欧拉函数详解

## 性质

1.$\phi(n)$为积性函数

$n=a_1^{p_1}*a_2^{p_2}...*a_k^{p_k}$

$p=a_1^{p_1}*a_2^{p_2}...*a_m^{p_m}$

$q=a_{m+1}^{p_{m+1}}*a_{m+2}^{m+2}...*a_k^{p_k}$

$\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)$

$\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)$

4. $a^{\phi(n)} \equiv 1 \pmod n$

## 计算方法

### $\sqrt(n)$计算单值欧拉函数

#### 2.当$n$为质数时

(仅有$n$与$n$不互质)

#### 3.当$n$为合数时

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

### 线性筛

#### 性质2

$=\varphi \left( i\ast p\right) =\varphi \left( i\right) \ast \left( p-1\right)$

#### 性质3

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

## 参考资料

posted @ 2018-01-27 10:35  自为风月马前卒  阅读(...)  评论(...编辑  收藏

……