Description
给定A,B,C,计算AB%C,这里AB代表A的B次方。
Input
输入数据有多组,每组数据一行,有3个正整数分别为A,B和C,1<=A,B,C<=1000000000
Output
输出A^B%C的值
Sample Input
2 3 5
8 2 10
Sample Output
3
4
代码如下:
#include <stdio.h>
#include <stdlib.h>
long long f(long long a,long long b,long long c)
{
long long ans=1;
a=a%c;
while(b)
{
if(b%2==1)
{
ans=(ans*a)%c;
}
a=(a*a)%c;
b=b/2;
}
return ans;
}
int main()
{
long long f(long long a,long long b,long long c);
long long a,b,c;
while(scanf("%lld%lld%lld",&a,&b,&c)!=-1)
{
long long m;
m=f(a,b,c);
printf("%lld\n",m);
}
return 0;
}