Modular Basics + Number Theory + Permutations

  • Benefit - instead of multiplying b times => multiply log2(b) times
  • If a is large → take (a % M)
  • If M is itself very large (like 1e18) => instead of a*a → DO a+a+a....(a times)
    • even if a*a = 1e18 → will not get reduced
    • log²(n) bcz log(n) for binary_Multiply & log(n) for expo
      • Same code as expo => Only change * to + everywhere
If b is large in ab

 → abc ≠ a(bc % M) (modM) → cannot take mod in power
  • Use Euler Theorem → abc
    a(bc % ɸ(m)) (modM) → ɸ is euler totient function
    • For safety, add extra ɸ in power bcz above fails if gcd(a, m) ≠ 1

    • abc
      a(ɸ(m) + (bc % ɸ(m)))(modM
       → updated

GCD + prime  properties
  1. a divides b + b divides a ---> a = ± b
  2. gcd is always +ve --> bcz let's say -d is gcd
    => d is also gcd and since gcd is greatest divisor --> gcd = d
  3. To make (a,b) co-prime --> divide both by their gcd
  4. a divides c + b divides c + gcd(a,b)=1 --> a*b divides c
  5. a divides b*c + gcd(a,b)=1 --> a divides c
  6. gcd(a,bc)=1 ⇔ gcd(a,b)=1 and gcd(a,c)=1
  7. For gcd problems, take gcd to be d --> and then think
  8. gcd(0,x)= x                    lcm(a,b)= 0 (if any of a or b = 0}
Modular  properties
  1. a≡ (mod n) c≡ d (mod n) --> a+c≡ b+d  or ac≡ bd (mod n)
    • Converse NOT true -> 4+2≡ 5+1 (mod 6) or 3*2≡ 6*1 (mod 6)
  2. cacb (mod n) --> a≡ b (mod n/d) where d = gcd(n,c)
    • Similar to GCD point 3
  3. If gcd(a,n)= d then ax≡ b (mod n) has d solutions if d divides b
    • ax - ny = b <=> ax ≡ b (mod n) bcz n divides (ax-b)
    • If gcd(a,n)=1 => x ≡ a⁻¹b(mod n) to get unique x [use GCD point 3]
  4. Fermat's theorem - p is prime gcd(p,a)= 1 --> p1≡ 1(mod p)
  5. p is prime a is any number --> p ≡ a(mod p)
  6. Wilson's theorem - If p is prime --> (p-1)! ≡  -1(mod p)
  7. Modular inverse ax ≡ 1(mod m) + gcd(a,m)=1
    => unique solution exists(from 3)and x is the modular inverse
    • a*(a p−2) ≡ 1(mod p) [from fermat's theorem] --> p−2 is inverse
  8. a≡ b (mod n) m is divisor of n --> a≡ b (mod m)

Divisor  Results
   where p1,p2,,pk are distinct prime numbers
τ(N)=(a1+1)(a2+1)(a3+1)(ak+1 ---> number of divisors
 
σ(N)=(p11p1a1+11)(p21p2a2+11)(p31p3a3+11)(pk1pkak+11)    --> sum of all divisors

Idea - (a1+1) choices for divisor in τ(N) + all choices are independent of others

Chinese Remainder theorem

for pairwise coprime integers m₁, m₂,...,mₖ and corresponding remainders a₁, a₂,...,aₖ, there exists a unique solution modulo M = m₁×m₂×...×mₖ to the system:

{xa1(modm1)xa2(modm2)xak(modmk)above system of equations corresponds to "xa
(mod M
)
Euler Totient function
  1. ɸ(n) is number of integers not more than n and relatively prime to n
    1. ɸ(p) = p-1  [when p is prime] bcz all 1 to p-1 are co-prime with p
    2.  ɸ(pk)=pkpk1  [when p is prime] bcz total value = pkvalues divisible by p are {p, 2p, ....,  pk1p} --> total = pk1 (subtract it)
  2. ɸ(n) is always even for n > 2
  3. ɸ(n) is multiplicative function => ɸ(m*n) = ɸ(m) * ɸ(n)    {if gcd(m, n) = 1}
  4.    where p1,p2,,pk are distinct prime numbers
    • ɸ(n) = ( pkak-1 )
  5. Euler's Theorem -   aϕ(n)1 (modn)  if gcd(a, n) = 1
Permutation and Combinations
  • Problem Type - Find number of integer solutions to equation x1 + x2 + .... + xk = n where ( 0) → ways to arrange n identical items into k distinct bin → C(n+k-1, k-1)
    • need (k-1) bars → same as choosing (k-1) positions out of {n + (k-1)} positions
    • C(n + 1, k - 1)❌ bcz ignores case where two or more bars are next to each other
    • NOTE - If ( 1) → subtract 1 from each to convert into ( 0) → C(n - 1, k-1)
  • Problem Type - Find number of integer solutions to equation x1 + x2 + .... + xk ≤ n where ( 0) → ways to arrange n identical items into k+1 distinct bin → C(n+k, k)
    • bcz add one extra slack bin to create equality (like Optimisation Techniques)

Comments

Popular posts from this blog

Templates (for CP)

DSA Trees + Heap