Guest User

Untitled

a guest
Sep 18th, 2018
68
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.35 KB | None | 0 0
  1. Linear Recurrence for very large n
  2. 0 <= a, b, n <= 10^100
  3. 1 <= M <= 100000
  4. F(0)=1
  5.  
  6. // Requires n >= 0 and M > 0.
  7. int modularPower(int a, int n, int M) {
  8. if (n == 0)
  9. return 1;
  10. int result = modularPower(a, n / 2, M);
  11. result = (result * result) % M;
  12. if (n % 2 != 0)
  13. result = (result * a) % M;
  14. return result;
  15. }
Add Comment
Please, Sign In to add comment