Professional Documents
Culture Documents
Elementary Number Theory
Elementary Number Theory
• 2, 3, 5, 7, ...
Primality Testing
• Obvious 𝑂( 𝑛) loop
• Wilson’s Theorem
𝑛 − 1 ! ≡ −1 (mod 𝑛) if and only if 𝑛 isprime
Multiplicative Functions
• 𝑓: 𝑁 → 𝑁 such that 𝑓 𝑥𝑦 = 𝑓 𝑥 𝑓(𝑦) for all
relatively prime numbers 𝑥 and 𝑦 (i.e.
gcd 𝑥, 𝑦 = 1)
• Obvious example: 𝑓 𝑥 = 𝑥
Sigma, Tau, Phi
• 𝜎(𝑥) denotes the sum of positive divisors of 𝑥
• 𝜏(𝑥) denotes the number of positive divisors
of 𝑥
• 𝜙 𝑥 (also called Euler totient function)
denotes the number of 𝑖 such that 𝑖 ≤ 𝑥 and
gcd 𝑖, 𝑥 = 1
• 𝜏 𝑥 = ς𝑘𝑖=1 𝑒𝑖 + 1
1
• 𝜙 𝑥 = 𝑥 ς𝑘𝑖=1 1−
𝑝𝑖
Euler’s Theorem
• 𝑎𝜙(𝑛) ≡ 1 mod 𝑛 if gcd 𝑎, 𝑛 = 1
Euclidean Algorithm
• gcd 𝑎, 𝑏 = gcd 𝑏, 𝑎 − 𝑏
Extended Euclidean Algorithm
• Find 𝑥 and 𝑦 such that 𝑎𝑥 + 𝑏𝑦 = gcd(𝑎, 𝑏)
• Computes sequence of remainders and
quotients:
– 𝑟0 = 𝑎
– 𝑟1 = 𝑏
–…
– 𝑟𝑖+1 = 𝑟𝑖−1 − 𝑞𝑖 𝑟𝑖 and 0 ≤ 𝑟𝑖+1 < |𝑟𝑖 |
Extended Euclidean Algorithm
• Also maintain two other sequences 𝑠𝑖 and 𝑡𝑖
𝑠0 = 1 𝑠1 = 0
𝑡0 = 0 𝑡1 = 1
…
𝑠𝑖+1 = 𝑠𝑖−1 − 𝑞𝑖 𝑠𝑖
𝑡𝑖+1 = 𝑡𝑖−1 − 𝑞𝑖 𝑡𝑖
• Extended Euclid
Find 𝑥 and 𝑦 such that 𝑥𝑝 + 𝑦𝑏 = 1
?= 𝑦
Linear Diophantine Equation
• Find 𝑥 and 𝑦 satisfying 𝑎𝑥 + 𝑏𝑦 = 𝑐
• If 𝑥0 , 𝑦0 is a solution, then
𝑦 𝑥
𝑥0 + 𝑘 gcd 𝑥,𝑦 , 𝑦0 − 𝑘 gcd 𝑥,𝑦 is
Problem: Benefit (UVa11889)
• Given 𝑎 and 𝑐, find the smallest positive
integer 𝑏 such that 𝑙𝑐𝑚 𝑎, 𝑏 = 𝑐
𝑎, 𝑐 ≤ 107
Problem: Permasalahan DP (P1 2014)
• If 𝑓(𝑎, 𝑏) is known, then so is 𝑓(𝑎 × 𝑘, 𝑏 × 𝑘)
• How many values of 𝑓 should be calculated to
find all 𝑓 𝑎, 𝑏 , 1 ≤ 𝑎, 𝑏 ≤ 𝑛?
SPOJ GCDEX
G=0;
for(i = 1 ; i < N ; i++)
for(j = i+1 ; j <= N ; j++)
G+=gcd(i,j);