[ACM ICPC] [BAU Summer Training 2018] Week 6 — Number Theory

Revision en1, by Corvus, 2018-09-08 22:10:46

Hello BAU Competive Programmers,

Today we disscussed Number Theory topics

In the begining we talk about Divisors of integer number (Divisor of number N is any number divides N without remainder, ex: 2 | 10 => 10 % 2==0 , 2 is divisor for 10) a | b means a divide b or a is divisor for b (b % a == 0)

some rules: 1. if a | b and b | c then a | c 2. if a | b and a | c then a | b*c

Then we write code to find divisor for any number in time complexity O(sqrt(N))

Find Divisors

and we disscuse problem Luck Division — CF 122A


Then We dissucssed Prime Numbers which is any number have only two distinct divisors 1 and itself (1 is not prime)

and we modify the divisors code to check if given number N is prime or not in time Complexity O(sqrt(N)) but actual runtime may be faster than sqrt(n)

Prime Check

and we solved problem CF 749A — Code: solution

and problem UVa 543 — Goldbach's Conjecture — Code: solution


Then we talk about Prime Factors of any number n

any integer number n is prime or can be written as multiplication of some prime numbers (ex: 100 = 2*2*5*5 = 2^2 * 5^2)

rule: if we multiply two numbers a and b (a * b = c) then we add powers of prime factors in a and in b a = p1^x1 * p2^x2 * ... pm^xm b = p1^y1 * p2^y2 * ... pm^ym then c = p1^(x1+y1) * p2^(x2+y2) * ... pm^(xm+ym) because if we multiply a and b we actualy multiply prime factors of a and prime factors b and in multiplication d^x * d^y = d^(x+y)

  • remmeber number of distinct prime factors for any integer is small compared to number itself (for 10^9 number of distinct factors is at most 12)

The code below is find the prime factors in time complexity of O(sqrt(N)) but since number n is changed every time we find a prime factor the avearge complexity is very faster that sqrt(N)

Prime Factorization

After that we talk about faster way to generate every prime factos in given range in time complexity O(N log(log N)) — where N is maximum number in the range if we know that any number is prime then every multiplyer of this prime is not prime and we don't need to check any number after sqrt(N) since first multiplyer not calculated with any number before it is acutaly out of range

Sieve of Eratosthenes

then we solve problem T-Primes CF 230B but we write code that use sqrt(N) prime check and it passed to add seive we only need to build seive then replace prime check call in line 52

and we talk about problem Minimum Sum of Array from AmmanCPC 2018 and how we can modify seive to answer another problem (note: there is algorithm to factorize any number in O(log N) using seive)

Solution 1: modified Seive

Solution 2: bruteforce


Last topic was Greatest Common Divisor (GCD) — gcd(a,b) is maximum number that divides both a and b Lowest Common Multiple (LCM) — lcm(a,b) is the minimum number that divided by a and b

gcd(a,0) = a and gca(a,b) = gcd(b,a%b)

lcm(a,b) = (a * b) / lcm(a,b)

GCD
LCM

Good Luck and Have Fun!

History

 
 
 
 
Revisions
 
 
  Rev. Lang. By When Δ Comment
en2 English Corvus 2018-09-08 22:36:08 30
en1 English Corvus 2018-09-08 22:10:46 5517 Initial revision (published)