培訓- 5 算數
算數
Angrybird11 c2251393 momohuang October 30, 2013
1 數
1.1 公因數與最大公因數與互質
1.1.1
Ifd|a and d|b, then d is the common divisor of a and b.
Greatest Common Divisor of a, b is written as gcd(a, b).
a and b are relatively prime iff gcd(a, b) = 1.
1.1.2 性質
1. d|aand d|b ⇒ d|(ax+by) 2. gcd(a,0) = a
3. gcd(a, b) = gcd(b, a) 4. gcd(a, b) = gcd(a, b−a) 5. gcd(a, b) = gcd(a, a−b) 6. Anonymous Theorem:
If a and b are integers, not both 0, then gcd(a, b)is the smallest pos- itive element of the set {ax+by|x, y ∈Z}
Proof: Let s be the smallest positive, s.t. s = ax + by. Let g = gcd(a, b), it is obvious that g | s. Now we'll show that s is a common
I
1.1 公因數與最大公因數與互質 算數
divisor of a and b, thus g = s. a mod s = a - qs = a(1-qx)+b(-qy).
And 0 ≤ a mod s < s, so a mod s = 0.
7. d|aand d|b ⇒ d| gcd(a, b). (Corollary from 6.)
8. if n | ab and gcd(a, n) = 1, then n | b. (Corollary from 6.)
1.1.3 歐
gcd(a, b) =gcd(a−qb, b) =gcd(b, a mod b) gcd(a,0) = a 性質 lg(max(a, b))的
Algorithm 1Euclid Algorithm
1: function Euclid(a, b)
2: if b==0 then
3: return a
4: else
5: return Euclid(b, a mod b)
6: end if
7: end function
1.1.4 歐
Target: 組 ax+by=gcd(a, b) 的 數
的 同 歐
ax+by=g 的 組 bx'+(a mod b)y'=g 的 bx'+
(a-⌊ab⌋*b)y'=g 因 ay'+b(x'-⌊ab⌋*by')=g 因
b==0 x=1, y=0
Algorithm 2Extended Euclid Algorithm
1: function Extended-Euclid(a, b)
2: if b==0 then
3: return (a, 1, 0)
4: else
5: (d', x', y') = Extended-Euclid(b, a mod b)
6: return (d', y', x' -⌊ab⌋y')
7: end if
8: end function
的 的 組 組 ax+by=0 的 ax
+by=gcd(a, b)的 ax+by=0的 gcd(a, b)=1 ax = -by y̸=0 b | ax b | x x = bn, y = -an
II
1.2 歐拉的ϕ 函數 算數
1.2 歐拉的 ϕ 函數
1.2.1
ϕ(n) [1, n] 與 n互質的數
1.2.2 性質
1. if p is prime,ϕ(p) = p-1
2. if p is prime,ϕ(pk) =(p−1)pk−1 3. if gcd(n, m) = 1, ϕ(nm) =ϕ(n)ϕ(m)
1.3 質數
1.3.1
因數 的 數 質數
π(N) 代 1∼N 質數
1.3.2 性質
1. if p is prime, and (a,p) = 1, then ap−1 ≡1 (mod p)
2. 餘: if p is an odd prime, then x2 ≡1(mod p) holds, only for x≡ 1 or x ≡ p-1(mod p).
3. 質數 When N → ∞, π(N) = logNN
1.3.3 質數
合數N ≤√
N 的因數 的數
N 質數 O(√
N)
合 的 質數性質 Miller-Rabin
數 n 1∼n-1的數 a Witness(a, n)
的 a witness n 合數
的for 的 餘的 性質 最
的 for 的 數 最 O(lg n)
的 au mod n O(lg n) 的 的 題
函數 n 合數 n 合數的
III
1.3 質數 算數
Algorithm 3Miller-Rabin Algorithm
1: function witness(a, n)
2: let t and u be t≥ 1, u is odd, and n-1=2tu
3: x0 =au mod n
4: for i = 1 to t do
5: xi =x2i−1 mod n
6: if xi == 1 andxi−1 ̸= 1 andxi−1 ̸=n−1 then
7: return TRUE
8: end if
9: end for
10: if xt̸= 1 then
11: return TRUE
12: end if
13: return FALSE
14: end function
的 1∼n-1 的數 witness n 合數 n 合數
a witness 20 FALSE 合數的
1
1048576 質數
1.3.4 質數
Target: 2∼N 質數
1. 2 ∼ N 的 True
2 的 數 2 False 3 的 數 3
False 4 的 數 4 False...
O(N2 +N3 +...+NN−1 +NN) = O(N ∗(12 +13 +...+N1))=O(NlogN) 質數的 數 質數數 logNN
線性的 數 3
2. 線性 2∼ N 的 True 質
數的陣列 合數 False
合數 i∗x x 合數的最 質因數
1.3.5 質因數
Target: 數的質因數
1. 大數 1 ∼ √
N 數 N 質因數 的
O(√ N)
IV
1.4 同餘 算數
Algorithm 4Linear Sieve
1: empty prime(vector)
2: set isprime[2∼N] = True
3: for i = 2 to Ndo
4: if isprime[i] is True then
5: prime.pushback(i)
6: end if
7: for x in prime do
8: if i * x > N then
9: break
10: end if
11: isprime[i * x] = False
12: if i mod x = 0 then
13: break
14: end if
15: end for
16: end for
2. 數 的 質數 的 數
數 質數 2∼N 的數 的質因數
O(NlogN)
1.4 同餘
a b 同餘 m 代 m | a-b a ≡b(mod m)
1.4.1 性質
1. a≡b(mod m) and α≡β(mod m) ⇒a+α ≡ b+β(mod m) 2. a≡b(mod m) and α≡β(mod m) ⇒aα ≡ bβ(mod m) 3. ac≡ bc(mod m) ⇒a ≡b(mod m)? No.
1.4.2
同餘的 性質 c−1 c−1 數 x, s.t. c∗x ≡1(mod m) 的 0∼m-1 c−1 的
x' 合 c∗x′ ≡ 1(mod m) c−1 ∗c∗x′ ≡ c−1(mod m) x≡c−1(mod m)
V
1.5 習題 算數
1.4.3
cx≡ 1(mod m) cx - 1 = mk ⇒ cx - mk = 1 的 ext-gcd
ext-gcd 的 gcd(c, m) = 1 的
餘 gcd(c, m) = 1
1.4.4
Target: ab(mod m)
: 同 的 2 | b ab mod m = (ab2mod m *ab2mod m) mod m ab mod m = (((ab−12 mod m *ab−12 mod m) mod m) * a) mod m
a0 mod m = 1
Algorithm 5Fast power
1: function power(a, b, m)
2: if b = 0 then
3: return 1 mod m
4: end if
5: if 2 | b then
6: set x = power(a, b2, m)
7: return (x * x) mod m
8: else
9: set x = power(a, b−21, m)
10: return (((x * x) mod m) * a) mod m
11: end if
12: end function
1.5 習題
1. x mod a1 =b1, ..., x mod an = bn, x 的
2. 的 線性 ϕ(1∼N)
3. [L, R] 的質數(L, R ≤2147483647, R - L ≤2∗105)
4. (Tioj 1067) N 列 數 0∼9 最
列 的數 M 互質 (N≤103, M≤104) 5. (Sgu 119) N,(A0, B0) 的 (A, B) 0 ≤ A, B < N
的 X, Y N|A0X+B0Y N|AX+BY (N, A0, B0 ≤104)
VI
2.線性代數 算數
2 線性代數
2.1
數 的 合
的 大公 函數 的
2.2 線性組合
與 數 v1, ..., vn的線性組合a1v1+...+anvn
的 的 (1, 0) (0, 1)
(1, 1) (2, 3) (1, 7) (9, 7) (2, 6)
2.3 線性
合 的線性組合
a1v1 +...+anvn = 0 a1 = ... = an = 0 合 線性
的 合 線性 的 (1, 0)
(0, 1) 的 的數
的 數 的 性質
的 的線性組合
2.4 線性
函數 T V W 合 T(x
+y)=T(x)+T(y) T(ax)=aT(x) 性質 N M
的 線性 的 合 與 M*N 矩陣的 合 的
2.5 矩陣
2.5.1
m×n 矩陣 陣列 {Aij} 1≤ i≤ m, 1≤j ≤n
VII
2.5 矩陣 算數
2.5.2 性質
1. A, B N*M 的矩陣
∀i∈[1, N], j ∈[1, M], (A+B)ij =Aij +Bij
2. A, B N*M 的矩陣
∀i∈[1, N], j ∈[1, M], (αA)ij =α(A)ij
3. A N*P的矩陣 B P*M 的矩陣
∀i∈[1, N], j ∈[1, M],(AB)ij =
∑P
k=1
AikBkj
4. 合 函數
5.
6. ? No.
7. 矩陣 IN N*N 的矩陣 Aij =δij δij = bool(i == j)
8. M N M*N 的矩陣 N*1 的矩陣
= M*1的矩陣
2.5.3
N*M 矩陣 列
N*N 的 矩陣 Eij 矩陣 Aij 1 餘 0
1. i, j列互 IN −Eii−Ejj +Eij +Eji 2. i列 數 α IN −Eii+αEii
3. i列 數 α j列 IN +αEji
列 的 列的
同的 的
VIII
2.6 習題 算數
2.5.4 矩陣
N*M 矩 陣 A 的 矩 陣 A−1 s.t. AA−1 =
IN, A−1A =IM A−1 A 的 矩陣
1. A 的 A 的線性 1-1 onto
2. A 的 N=M
3. N=M AB=IN BA=IN
4. 的 矩陣 的
5. A B AB
6. A IN A
的 矩陣 的矩陣
A−1 習題
2.5.5 矩陣的
矩陣的AK 同 的
2.6 習題
1. A 的 A−1
2. N M 最 線性 (N, M≤200)
3. N M 組 (N, M ≤ 200)
4. f(0) = a, f(1) = b, f(n) = f(n-1) + f(n-2), f(N) mod 109 (a, b, N
≤1017) 5. N
的 K 的 (N≤ 100, K≤1017)
6. V E S K T mod 109
(V ≤100, K ≤1017)
3 組合
組合 數 合特 的 的數
的
IX
3.1 排列組合 算數
3.1 排列組合
1. A, B 互 |A|+|B|
2. A, B |A| · |B|
3. n的 n! = 1×2×. . .×n−1×n n 的排列
數
4. 排列數 Pmn = n!
(n−m)! n m 的 排列 數
5. 組合數 Cmn = (n
m )
= n!
(n−m)!m! n m 的 數
6. (x+y)n=
∑n
k=0
Cknxkyn−k 特 x=y= 1 2n =
∑n
k=0
Ckn
7. 卡 Cmn =Cmn−1+Cmn−−11
3.2
3.2.1
數列的 的函數
數列的 的
3.2.2 1.
的 的
2. Dynamic Programming
3. 矩陣 ( 線性 )
線性 的函數 (矩陣) 因 矩陣的
K 的 O(N3logK)(N 矩陣的
的 數)
X
3.3 卡特蘭數 算數
3.3 卡特蘭數
Definition n×n 的 (0,0)
(n, n) 線( )
? 數 卡特蘭數
Cn={1,1,2,5,14,42,132, , ,}
卡特蘭數
線 (i, i)
1. (0,0) (i, i) 線: (1,0) (i, i−1) (
) 線的 數 Ci−1
2. (i, i) (n, n) 線: Cn−i
互 卡特蘭數 n 的 :
Cn=
∑n
1
Ci−1Cn−i
O(N2) n 卡特蘭數的 的 !
2 卡特蘭數 = 數 - 線的 數
數 Cn2n 線的 數 ? 線的 ( 線
的線) 的 線 " 線 的線"
(n−1, n+ 1) 線的 數 (0,0) (n−1, n+ 1) 的 數 Cn+12n 合 :
Cn=Cn2n−Cn+12n
O(N) N 卡特蘭數的
的組合數 卡特蘭數
1. n 的 數
2. n 的 數
3. n 的合 數
4. blahblahblah
XI
3.4 Exercises 算數
3.4 Exercises
1. 數列
數 K 數的
2. <TIOJ 1607> 線
T 2N 的 線的 數 mod 1000000007 的
3. <STEP5 0093>數
m 排 因
的 n 的 排
m & 排 排
(1≤n ≤15,1≤m≤90)
4. <TIOJ 1086> 的 <94 >
N 的 的 8 數O(N);
8 數O(1)
5. <TIOJ 1677> <98 (prob 1)>
( )的 ( ) ( ) ( )
( ) N K
6. <STEP5 0053> 的
N 的 k 的 數
(N ≤50, k <231)
XII