fzu Problem 1650 A^B mod C

题目:1650 AB mod C 

 

#include <cstdio>
#include <cstring>
#include <iostream>
#include <cmath>
#include <algorithm>
using namespace std;
unsigned long long multi(unsigned long long a,unsigned long long b,unsigned long long mod)
{
    unsigned long long ans=0;
    while(b)
    {
        if(b&1)
        {
            b--;
            ans=(ans+a)%mod;
        }
        else
        {
            b/=2;
            a=(a+a)%mod;
        }
    }
    return ans;
}
unsigned long long Pow(unsigned long long a,unsigned long long b,unsigned long long mod)
{
    unsigned long long ans=1;
    while(b)
    {
        if(b&1)
        {
            b--;
            ans=multi(ans,a,mod);
        }
        else
        {
            b/=2;
            a=multi(a,a,mod);
        }
    }
    return ans;
}
int main()
{
    unsigned long long a,b,n;
    while(cin>>a>>b>>n)
    {
        a%=n;
        cout<<Pow(a,b,n)<<endl;
    }
    return 0;
}
View Code

 

posted @ 2013-07-15 23:44  over_flow  阅读(181)  评论(0编辑  收藏  举报