NantipatSoftEn / Art.Of.Programing

:+1: Algorithm | Problem Solve :smile_cat: :smile_cat:

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Bigmod

find very big integer formular :

Serveral Algorithm

(A*B*C) mod N = ((A mod N) * (B mod N) * (C mod N)) mod N

Example

7*11*23 mod 5 is 1
(7 mod 5) * (11 mod 5) * (23 mod 5) = (6 mod 5) = 1

Fermat Little Test for primality testing:

R=B^P mod M (B^P very big number)

calulation R Quickly (fast) using Divide & Conquer

long bigmod(long b,long p,long m)
{
  if(p == 0 )
    return 1;
  else if(p % 2 == 0)
    return  (long)sqrt(bigmod(b,p/2,m)) % m; //sqrt(x)= x*x
  else
    return (b % m) * bigmod(b,p-1,m) % m;
}

Alt text

Solve UVa

  • 343
  • 353
  • 389

About

:+1: Algorithm | Problem Solve :smile_cat: :smile_cat:


Languages

Language:C++ 100.0%