374-big Mod
Time limit:3.000 seconds
Http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&category=24&page=show_problem &problem=310
Calculate
For large values of B, P, and M using a efficient algorithm. (That's right, this is problem has a time dependency!!!.)
Input
Three integer values (in the order B, P, M) would be read one number per line. B and P are integers in the range 0 to 2147483647 inclusive. M is a integer in the range 1 to 46340 inclusive.
Output
The result of the computation. A single integer.
Sample Input
3
18132
1765
3
2374859
3029382
36123
Sample Output
2
13195
Take a review of this function.
Complete code:
/*0.012s*/
#include <cstdio>
int main ()
{
int b, p, mod, ans;
while (~SCANF ("%d%d%d", &b, &p, &mod))
{
b%= mod;
ans = 1;
while (p)
{
if (P & 1)
ans = ans * b mod;
b = b * b% mod;
P >>= 1;
}
printf ("%d\n", ans);
}
return 0;
}
Author Signature: CSDN blog Synapse7
See more highlights of this column: http://www.bianceng.cnhttp://www.bianceng.cn/Programming/sjjg/