codevs 1497 取余运算

时间限制: 1 s
 空间限制: 128000 KB
 题目等级 : 钻石 Diamond
题目描述 Description

输入b,p,k的值,编程计算bp mod k的值。其中的b,p,k*k为长整型数(2^31范围内)。

输入描述 Input Description

b p k 

输出描述 Output Description

输出b^p mod k=?

=左右没有空格

样例输入 Sample Input

2  10  9

样例输出 Sample Output

2^10 mod 9=7

数据范围及提示 Data Size & Hint
#include <iostream>
#include <cstring>
#include <cstdio>
#define LL long long

using namespace std;

LL b,p,k,i,j;
LL powe(LL m,LL n,LL c)
{
    LL r=1,base=m;
    while(n)
    {
        if(n&1)
            r=r*base%c;
        base=base*base%c;
        n>>=1;
    }
    return r;
}
int main()
{
    ios::sync_with_stdio(false); 
    cin>>b;
    cin>>p;
    cin>>k;
    LL ans=powe(b,p,k);
    cout<<b<<"^"<<p<<" mod "<<k<<"="<<ans;
}

 

posted @ 2017-02-09 21:06  杀猪状元  阅读(134)  评论(0编辑  收藏  举报