1. 程式人生 > >哇咔咔的升級之路~

哇咔咔的升級之路~

取餘運算

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;
    }
}