daily pastebin goal
16%
SHARE
TWEET

Exponentiation by squaring

a guest May 23rd, 2012 32 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. long long power(long long x, long long n)
  2. {
  3.         if(n == 0)
  4.                 return 1;
  5.         if(n % 2) // n is odd
  6.                 return (x * power(x, n - 1)) % Q;
  7.         else // n is even
  8.         {
  9.                 int a = power(x, n / 2);
  10.                 return (a * a) % Q;
  11.         }
  12. }
RAW Paste Data
Pastebin PRO WINTER Special!
Get 40% OFF Pastebin PRO accounts!
Top