您的位置:首页 > 其它

51nod 1046 A^B Mod C (快速幂)

2015-10-12 16:18 302 查看
给出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>
using namespace std;
typedef long long ll;

ll mod_pow(ll x,ll n,ll mod)
{
ll res=1;
while(n>0) {
if(n&1) res=res*x%mod;
x=x*x%mod;
n>>=1;
}
return res;
}

int main()
{
int a,n,mod;
cin>>a>>n>>mod;
cout<<mod_pow(a,n,mod)<<endl;
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: