1046 A^B Mod C

来源:互联网 发布:360软件粉碎 编辑:程序博客网 时间:2024/05/17 07:55

1046 A^B Mod C

给出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<algorithm>#include<cstdio>#include<cstring>#include<cmath>#define ll long longusing namespace std;ll power(ll a,ll b,ll m){    ll ans=1;    while(b){        if(b&1){            ans=(ans*a)%m;            b--;        }        b/=2;        a=a*a%m;    }    return ans;}int main(){    int m,a,b;    while(~scanf("%d%d%d",&a,&b,&m)){        ll ans=power(a,b,m);        printf("%lld\n",ans);    }    return 0;}