快速幂取模

#include<iostream>
#include<cstdio>
#include<cmath>
#include<cstring>
#include<algorithm>
using namespace std;
typedef long long ll;
ll ksm(ll a, ll b, ll c)
{
    ll ans=1;
    a=a%c;
    while(b>0)
    {
        if(b%2==1)
            ans=(ans*a)%c;
        b=b/2;
        a=(a*a)%c;
    }
    return ans;
}
int main()
{
    ll a,b,c;
    scanf("%lld %lld %lld",&a,&b,&c);
    printf("%lld\n",ksm(a,b,c));
    return 0;
    
}

 

posted @ 2018-08-10 12:21  小学弟-  阅读(99)  评论(0编辑  收藏  举报