題目鏈接 : TOJ10008
10008 - a^b mod c
Memory Limit: 65536KB
The calculation of ab mod c arises in many fields of computer science, this time we brought it to you, can you prove us your talent?
Input
Three integers, a, b, c. (1<=a,b,c<=231-1)
Output
One integer, the result of ab mod c.
Sample Input
3 7 15
Sample Output
12
Hint
- The operator mod is defined as calculating the reminder of a divides b. For example, 5 mod 1 = 0, 5 mod 2 = 1, 5 mod 3 = 2, 5 mod 4 = 1, 5 mod 5 = 0, 5 mod 6 = 5, 5 mod 7 = 5, 5 mod 8 = 5, etc.
- The operator mod has following properties:
- (a + b) mod c = ((a mod c) + b) mod c
- (a - b) mod c = ((a mod c) - b) mod c
- (a * b) mod c = ((a mod c) * b) mod c
- It is obvious that (a + b) mod c = ((a mod c) + (b mod c)) mod c, and etc.
- (a + b) mod c = ((a mod c) + b) mod c
簡單的快速冪
#include<cstdio>
int pow_mod(int a,int b,int c)
{
long long d=1,e=a%c;
while(b)
{
if(b%2) d=(d*e)%c;
b/=2;
e=(e*e)%c;
}
return d;
}
int main()
{
int a,b,c;
scanf("%d%d%d",&a,&b,&c);
printf("%d\n",pow_mod(a,b,c));
return 0;
}