取余运算(codevs 1497)

题目描述 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

//快速幂
#include<cstdio>
#include<iostream>
#define LL long long
using namespace std;
LL b,p,k;
LL poww(int a,int b)
{
    LL r=1,base=a;
    while(b)
    {
        if(b&1)r*=base;
        base*=base;
        base%=k;
        b/=2;
        r%=k;
    }
    return r%=k;
}
int main()
{
    cin>>b>>p>>k;
    int tb=b%k;
    cout<<b<<"^"<<p<<" mod "<<k<<"="<<poww(tb,p);
    return 0;
} 
View Code
原文地址:https://www.cnblogs.com/harden/p/5642912.html