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 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
3181321717176532374859302938236123
Sample Output
13213195
拿來複習下這個函數。。
完整代碼:
/*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; }
作者署名:csdn部落格 synapse7
查看本欄目更多精彩內容:http://www.bianceng.cnhttp://www.bianceng.cn/Programming/sjjg/