取余运算

取余运算

Time Limit : 3000/1000ms (Java/Other)   Memory Limit : 65535/32768K (Java/Other)
Total Submission(s) : 6   Accepted Submission(s) : 5

Font: Times New Roman | Verdana | Georgia

Font Size:  

Problem Description

输入b,p,k的值,求b^p mod k的值。

Input

输入有多组数据,每组数据为一行三个数b,p,k,其中b,p,k*k为长整型数。

Output

对于每组数据输出b^p mod k的值。

Sample Input

2 10 9 

Sample Output

2^10 mod 9=7

#include <iostream>
#include <string.h>
#include <stdio.h>
using namespace std;
long long ans;
int  powmod(long long a,long long b,long long c)
{
    ans=1;
    while(b)
    {
        if(b&1)
            ans=a*ans%c;
        a=(a%c)*(a%c)%c;
        b/=2;

    }
    return ans;
}
int main ()
{
    long long b,p,k;
    while(cin>>b>>p>>k)
    {
        powmod(b,p,k);
        cout<<b<<'^'<<p<<" "<<"mod"<<" "<<k<<"="<<ans<<endl;
    }
}


猜你喜欢

转载自blog.csdn.net/u010760034/article/details/17125359