Welcome

首页 / 软件开发 / 数据结构与算法 / UVa 374 Big Mod:快速幂取模

UVa 374 Big Mod:快速幂取模2014-07-24

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
", ans);}return 0;}
作者署名:csdn博客 synapse7