给出3个正整数A B C,求A^B Mod C。
例如,3 5 8,3^5 Mod 8 = 3。
Input
3个正整数A B C,中间用空格分隔。(1 <= A,B,C <= 10^9)
Output
输出计算结果
Sample Input
3 5 8
Sample Output
3
这道题需要用到快速幂除法,以优化计算速度
#include<stdio.h>
typedef long long ll;
int JS(ll a,ll b,ll c)
{
ll ans = 1;
a=a%c;
while(b>0)
{
if(b & 1)
ans=ans*a %c;
b >>= 1;
a=a*a%c;
}
return ans;
}
int main()
{
ll a,b,c;
scanf("%lld%lld%lld",&a,&b,&c);
a = JS(a,b,c);
printf("%lld",a);
return 0;
}