最大公约数
int gcd(int a, int b)
{return (b) ? gcd(b, a % b) : a;}
欧拉函数
φ
(
n
)
φ(n)
φ(n)表示
2
−
n
2-n
2−n中与
n
n
n互质的数的个数
当
N
=
p
1
c
1
∗
p
2
c
2
∗
.
.
.
∗
p
m
c
m
N = p_{1}^{c_1} * p_{2}^{c_2} * ... * p_{m}^{c_m}
N=p1c1∗p2c2∗...∗pmcm,则
φ
(
n
)
=
N
∗
p
1
−
1
p
1
∗
p
2
−
1
p
2
∗
.
.
.
∗
p
m
−
1
p
m
=
N
∗
∏
质
数
p
∣
N
(
1
−
1
p
)
φ(n) = N * \frac{p_1 - 1}{p_1} * \frac{p_2 - 1}{p_2} * ... * \frac{p_m - 1}{p_m} = N * \prod_{质数p|N}(1 - \frac{1}{p})
φ(n)=N∗p1p1−1∗p2p2−1∗...∗pmpm−1=N∗∏质数p∣N(1−p1)
证明:
设
p
p
p是
N
N
N的质因子,1~N中
p
p
p的倍数有
p
,
2
p
,
3
p
,
.
.
.
,
(
N
/
p
)
∗
p
p, 2p, 3p, ... , (N/p)*p
p,2p,3p,...,(N/p)∗p,共
N
/
p
N/p
N/p个
同理,若
q
q
q也是
N
N
N的质因子,
1
−
N
1-N
1−N中
q
q
q的倍数有
N
/
q
N/q
N/q个
如果把这
N
/
p
+
N
/
q
N/p + N/q
N/p+N/q去掉,那么
p
∗
q
p*q
p∗q的倍数就会排除了两次,需要加回来一次。
因此
1
−
N
1-N
1−N中不与N含有共同质因子
p
p
p或
q
q
q的数的个数为:
N
−
N
p
−
N
q
+
N
p
q
=
N
∗
(
1
−
1
p
−
1
q
+
1
p
q
)
=
N
(
1
−
1
p
)
(
1
−
1
q
)
N - \frac{N}{p} - \frac{N}{q} + \frac{N}{pq} = N * (1 - \frac{1}{p} - \frac{1}{q} + \frac{1}{pq}) = N(1 - \frac{1}{p})(1 - \frac{1}{q})
N−pN−qN+pqN=N∗(1−p1−q1+pq1)=N(1−p1)(1−q1)
int Euler(int n)
{
int ans = n;
for(int i = 2; i <= sqrt(n); ++i)
if(!(n % i))
{
ans = ans / i * (i - 1);
while(!(n % i))n /= i;
}
if(n > 1)ans = ans / n * (n - 1);
return ans;
}
S
o
m
e
t
h
i
n
g
Something
Something
1
−
n
(
1
<
n
)
1-n(1 < n)
1−n(1<n)中与n互质的数的和为
n
∗
φ
(
n
)
/
2
n * φ(n) / 2
n∗φ(n)/2
证明:
因为
g
c
d
(
n
,
x
)
=
g
c
d
(
n
,
n
−
x
)
gcd(n, x) = gcd(n, n - x)
gcd(n,x)=gcd(n,n−x),所以与n互质的数x,n-x成对出现,则两者之和为n
所以质数和为
φ
(
n
)
∗
n
/
2
φ(n) * n / 2
φ(n)∗n/2
同余
5
≡
7
(
m
o
d
2
)
5\ ≡\ 7\ (mod\ \ 2)
5 ≡ 7 (mod 2)表示
5
5
5和
7
7
7模
2
2
2的余数相同
S
o
m
e
t
h
i
n
g
Something
Something
a
≡
b
(
m
o
d
n
)
=
>
b
≡
a
(
m
o
d
n
)
a\ ≡\ b\ (mod\ \ n)\ \ =>\ \ b\ ≡\ a\ (mod\ \ n)
a ≡ b (mod n) => b ≡ a (mod n)
a
≡
b
(
m
o
d
n
)
=
>
a
+
c
≡
b
+
c
(
m
o
d
n
)
a\ ≡\ b\ (mod\ \ n)\ \ =>\ \ a+c\ ≡\ b+c\ (mod\ \ n)
a ≡ b (mod n) => a+c ≡ b+c (mod n)
a
≡
b
(
m
o
d
n
)
=
>
a
∗
c
≡
b
∗
c
(
m
o
d
n
)
a\ ≡\ b\ (mod\ \ n)\ \ =>\ \ a * c\ ≡\ b * c\ (mod\ \ n)
a ≡ b (mod n) => a∗c ≡ b∗c (mod n)
a
≡
b
(
m
o
d
n
)
且
a
≡
c
(
m
o
d
n
)
=
>
b
≡
c
(
m
o
d
n
)
a\ ≡\ b\ (mod\ \ n)\ \ 且\ a\ ≡\ c\ (mod\ \ n)\ =>\ \ b\ ≡\ c\ (mod\ \ n)
a ≡ b (mod n) 且 a ≡ c (mod n) => b ≡ c (mod n)
∵
a
≡
b
(
m
o
d
n
)
,
c
≡
d
(
m
o
d
n
)
∴
a
±
c
≡
b
±
d
(
m
o
d
n
)
∵a\ ≡\ b\ (mod\ \ n),c\ ≡\ d\ (mod\ \ n)∴a\ ±\ c\ ≡b\ ±\ d(mod\ \ n)
∵a ≡ b (mod n),c ≡ d (mod n)∴a ± c ≡b ± d(mod n)
∵
a
≡
b
(
m
o
d
n
)
,
c
≡
d
(
m
o
d
n
)
∴
a
c
≡
b
d
(
m
o
d
n
)
∵a\ ≡\ b\ (mod\ \ n),c\ ≡\ d\ (mod\ \ n)∴ac\ ≡bd(mod\ \ n)
∵a ≡ b (mod n),c ≡ d (mod n)∴ac ≡bd(mod n)
欧拉定理
当正整数
a
,
n
a, n
a,n互质,则
a
φ
(
n
)
≡
1
(
m
o
d
n
)
a^{φ(n)}≡1(mod\ n)
aφ(n)≡1(mod n)
证明:
设
S
1
=
r
1
,
r
2
,
.
.
.
,
r
φ
(
n
)
S_1 = r_1, r_2, ... ,r_{φ(n)}
S1=r1,r2,...,rφ(n),
r
i
r_i
ri为
m
o
d
n
mod\ n
mod n的即约剩余数
S
1
∗
a
=
S
2
=
a
r
1
,
a
r
2
,
.
.
.
,
a
r
φ
(
n
)
S_1 * a = S_2 = {ar_1, ar_2, ... ,ar_{φ(n)}}
S1∗a=S2=ar1,ar2,...,arφ(n),
a
r
i
ar_i
ari也是
m
o
d
n
mod\ n
mod n的即约剩余数
∴
∏
i
=
1
φ
(
n
)
r
i
≡
∏
i
=
1
φ
(
n
)
a
r
i
(
m
o
d
n
)
\prod_{i = 1}^{φ(n)}r_i ≡ \prod_{i = 1}^{φ(n)}ar_i (mod\ n)
∏i=1φ(n)ri≡∏i=1φ(n)ari(mod n)
∴
a
φ
(
n
)
∏
i
=
1
φ
(
n
)
r
i
≡
∏
i
=
1
φ
(
n
)
r
i
(
m
o
d
n
)
a^{φ(n)}\ \prod_{i = 1}^{φ(n)}r_i ≡ \prod_{i = 1}^{φ(n)}r_i (mod\ n)
aφ(n) ∏i=1φ(n)ri≡∏i=1φ(n)ri(mod n)
∴
n
∣
(
a
φ
(
n
)
∏
i
=
1
φ
(
n
)
r
i
−
∏
i
=
1
φ
(
n
)
r
i
)
n|(a^{φ(n)}\ \prod_{i = 1}^{φ(n)}r_i - \prod_{i = 1}^{φ(n)}r_i)
n∣(aφ(n) ∏i=1φ(n)ri−∏i=1φ(n)ri)
∴
n
∣
(
a
φ
(
n
)
−
1
)
∏
i
=
1
φ
(
n
)
r
i
⋅
⋅
⋅
⋅
⋅
⋅
⋅
⋅
⋅
⋅
⋅
⋅
⋅
⋅
⋅
⋅
⋅
⋅
⋅
m
∣
a
b
n|(a^{φ(n)} - 1)\ \prod_{i = 1}^{φ(n)}r_i···················m|ab
n∣(aφ(n)−1) ∏i=1φ(n)ri⋅⋅⋅⋅⋅⋅⋅⋅⋅⋅⋅⋅⋅⋅⋅⋅⋅⋅⋅m∣ab
∵
r
i
r_i
ri与
n
n
n互质
∴
∏
i
=
1
φ
(
n
)
r
i
\prod_{i = 1}^{φ(n)}r_i
∏i=1φ(n)ri也与
n
n
n互质
⋅
⋅
⋅
⋅
⋅
⋅
⋅
⋅
⋅
⋅
⋅
⋅
⋅
⋅
⋅
⋅
⋅
⋅
⋅
⋅
⋅
⋅
⋅
⋅
m
∣
a
························m|a
⋅⋅⋅⋅⋅⋅⋅⋅⋅⋅⋅⋅⋅⋅⋅⋅⋅⋅⋅⋅⋅⋅⋅⋅m∣a
∴
n
∣
(
a
φ
(
n
)
−
1
)
⋅
⋅
⋅
⋅
⋅
⋅
⋅
⋅
⋅
⋅
⋅
⋅
⋅
⋅
⋅
⋅
⋅
⋅
⋅
⋅
⋅
⋅
⋅
⋅
⋅
⋅
⋅
⋅
⋅
⋅
m
∣
b
n|(a^{φ(n)} - 1)······························m|b
n∣(aφ(n)−1)⋅⋅⋅⋅⋅⋅⋅⋅⋅⋅⋅⋅⋅⋅⋅⋅⋅⋅⋅⋅⋅⋅⋅⋅⋅⋅⋅⋅⋅⋅m∣b
∵
m
∣
(
a
−
b
)
=
>
a
≡
b
(
m
o
d
m
)
m|(a - b)\ =>\ \ a\ ≡\ b\ (mod\ m)
m∣(a−b) => a ≡ b (mod m)
∴
a
φ
(
n
)
≡
1
(
m
o
d
n
)
a^{φ(n)} ≡ 1(mod\ n)
aφ(n)≡1(mod n)
费马小定理
当
p
p
p是质数,则
a
p
≡
1
(
m
o
d
p
)
a^p ≡ 1(mod\ p)
ap≡1(mod p)
证明:
当
a
m
o
d
p
≠
0
a\ mod\ p\ ≠\ 0
a mod p = 0,也就是
a
a
a不是
p
p
p的倍数时,则
a
a
a与
p
p
p互质
由欧拉定理得
a
φ
(
p
)
≡
1
(
m
o
d
p
)
a^{φ(p)}≡1(mod\ p)
aφ(p)≡1(mod p)
∴
a
p
−
1
≡
1
(
m
o
d
p
)
⋅
⋅
⋅
⋅
⋅
⋅
⋅
⋅
⋅
⋅
⋅
⋅
⋅
⋅
⋅
⋅
⋅
⋅
⋅
⋅
⋅
⋅
⋅
⋅
⋅
⋅
⋅
⋅
⋅
φ
(
质
数
)
=
质
数
−
1
a^{p - 1} ≡ 1 (mod\ p)·····························φ(质数) = 质数 - 1
ap−1≡1(mod p)⋅⋅⋅⋅⋅⋅⋅⋅⋅⋅⋅⋅⋅⋅⋅⋅⋅⋅⋅⋅⋅⋅⋅⋅⋅⋅⋅⋅⋅φ(质数)=质数−1
∴
a
p
≡
a
(
m
o
d
p
)
⋅
⋅
⋅
⋅
⋅
⋅
⋅
⋅
⋅
⋅
⋅
⋅
⋅
⋅
⋅
⋅
⋅
⋅
⋅
⋅
⋅
⋅
⋅
⋅
⋅
⋅
⋅
⋅
⋅
⋅
⋅
两
边
同
乘
a
a^p ≡ a (mod\ p)·······························两边同乘a
ap≡a(mod p)⋅⋅⋅⋅⋅⋅⋅⋅⋅⋅⋅⋅⋅⋅⋅⋅⋅⋅⋅⋅⋅⋅⋅⋅⋅⋅⋅⋅⋅⋅⋅两边同乘a
当
a
m
o
d
p
=
0
a\ mod\ p\ =\ 0
a mod p = 0,也就是
a
a
a是
p
p
p的倍数时(
a
a
a与
p
p
p不互质)时(就不能用欧拉定理了)
a
m
o
d
p
=
a
p
m
o
d
p
=
0
a\ mod\ p\ =\ a^p\ mod\ p\ =\ 0
a mod p = ap mod p = 0
∴
a
p
≡
a
(
m
o
d
p
)
a^p\ ≡\ a\ (mod\ p)
ap ≡ a (mod p)
欧拉定理的推论
若
a
a
a,
n
n
n互质,则对于任何正整数
b
b
b,有
a
b
≡
a
b
m
o
d
φ
(
n
)
(
m
o
d
n
)
a^b\ ≡\ a^{b\ mod\ φ(n)}\ (mod\ \ n)
ab ≡ ab mod φ(n) (mod n)
证明:
设
b
=
q
∗
φ
(
n
)
+
r
b\ =\ q\ *\ φ(n)\ +\ r
b = q ∗ φ(n) + r,其中
0
≤
r
≤
φ
(
n
)
0\ \leq r \leq\ φ(n)
0 ≤r≤ φ(n),即
r
=
b
m
o
d
φ
(
n
)
r\ =\ b\ \ mod\ \ φ(n)
r = b mod φ(n)。
则
a
b
≡
a
q
∗
φ
(
n
)
+
r
≡
(
a
φ
(
n
)
)
q
∗
a
r
≡
1
q
∗
a
r
≡
a
r
≡
a
b
m
o
d
φ
(
n
)
(
m
o
d
n
)
a^b\ ≡\ a^{q\ *\ φ(n)\ +\ r}≡(a^{φ(n)})^q\ *\ a^r\ ≡\ 1^q\ *\ a^r\ ≡\ a^r\ ≡\ a^{b\ mod\ \ φ(n)}(mod\ \ n)
ab ≡ aq ∗ φ(n) + r≡(aφ(n))q ∗ ar ≡ 1q ∗ ar ≡ ar ≡ ab mod φ(n)(mod n)
扩展欧几里得算法
任意
a
,
b
a,\ b
a, b,存在一对
x
,
y
x,\ y
x, y,满足
a
x
+
b
y
=
g
c
d
(
a
,
b
)
ax\ +\ by\ =\ gcd(a,\ b)
ax + by = gcd(a, b)
证明:
若
b
≠
0
b≠0
b=0,则
g
c
d
(
a
,
b
)
=
g
c
d
(
b
,
a
m
o
d
b
)
gcd(a,\ b)\ =\ gcd(b,\ a\ mod\ b)
gcd(a, b) = gcd(b, a mod b)
并假设存在一对整数
x
,
y
x,\ y
x, y,满足
b
∗
x
+
(
a
m
o
d
b
)
∗
y
=
g
c
d
(
b
,
a
m
o
d
b
)
b*x+(a\ mod\ b)*y\ =\ gcd(b,\ a\ mod\ b)
b∗x+(a mod b)∗y = gcd(b, a mod b)
所以
b
x
+
(
a
m
o
d
b
)
y
bx+(a\ mod\ b)y
bx+(a mod b)y
=
b
x
+
(
a
−
b
⌊
a
/
b
⌋
)
y
\ =\ bx + (a\ -\ b\lfloor a/b \rfloor) y
= bx+(a − b⌊a/b⌋)y
=
b
x
+
(
a
y
−
b
y
⌊
a
/
b
⌋
)
\ =\ bx + (ay\ -\ by\lfloor a/b \rfloor)
= bx+(ay − by⌊a/b⌋)
=
a
y
+
b
(
x
−
y
⌊
a
/
b
⌋
)
\ =\ ay\ +\ b(x\ -\ y\lfloor a/b \rfloor)
= ay + b(x − y⌊a/b⌋)
所以令
x
′
=
y
,
y
′
=
x
−
y
⌊
a
/
b
⌋
x'\ =\ y,\ y'\ =\ x\ -\ y\lfloor a/b \rfloor
x′ = y, y′ = x − y⌊a/b⌋
就得到
a
x
′
+
b
y
′
=
g
c
d
(
a
,
b
)
ax'\ +\ by'\ =\ gcd(a,\ b)
ax′ + by′ = gcd(a, b)
若
b
=
0
b=0
b=0的话,则显然有
x
=
1
,
y
=
0
x\ =\ 1,\ y\ =\ 0
x = 1, y = 0
使得
a
∗
1
+
b
∗
0
=
g
c
d
(
a
,
0
)
=
g
c
d
(
b
,
b
−
a
)
=
g
c
d
(
a
,
b
)
a\ *\ 1\ +\ b\ *\ 0\ =\ gcd(a,\ 0)\ =\ gcd(b,\ b-a)\ =\ gcd(a,\ b)
a ∗ 1 + b ∗ 0 = gcd(a, 0) = gcd(b, b−a) = gcd(a, b)
int gcd(int a, int b, int &x, int &y)
{
if(b == 0)
{
x = 1, y = 0;
return a;
}
int d = gcd(b, a % b, x, y);
int z = x;
x = y, y = z - y * (a / b);
return d;
}