UVa 374 Big Mod (快速幂取模)

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 BP, and M using an efficient algorithm. (That's right, this problem has a time dependency !!!.)

Input

Three integer values (in the order BPM) 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

拿来复习下这个函数。。


完整代码:

/*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;
}

你可能感兴趣的:(C++,ACM,uva)