51Nod 1046 A^B Mod C Label:快速幂

时间:2023-03-10 06:27:04
51Nod 1046 A^B Mod C Label:快速幂
给出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
输出计算结果
Input示例
3 5 8
Output示例
3

代码

 #include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#define ll long long
using namespace std; ll pow(ll x,ll n,ll m){
ll ans=;
while(n){
if(n&)ans=ans*x%m;
x=(x*x)%m;
n>>=;
}
return ans;
} int main(){
ll a,b,c;
scanf("%lld%lld%lld",&a,&b,&c);
printf("%lld\n",pow(a,b,c));
return ;
}