Big Mod
Big Mod |
Calculate
for large values of B, P, and M using an efficient algorithm. (That's right, this problem has a time dependency !!!.)
Input
Three integer values (in the order B, P, M) will be read one number per line. B and P are integers in the range 0 to 2147483647 inclusive. M is an integer in the range 1 to 46340 inclusive.
Output
The result of the computation. A single integer.
Sample Input
3 18132 17 17 1765 3 2374859 3029382 36123
Sample Output
13 2 13195
#include <iostream> #include <cstdio> using namespace std; #define ll long long ll B , P , M; void computing(){ int r = B%M , ans = 1; while(P != 0){ if(P%2 == 1){ ans = (ans*r)%M; } r = (r*r)%M; P = P/2; } printf("%d\n" , ans); } int main(){ while(cin >> B >> P >> M){ computing(); } return 0; }