DCP-23: Another Bigmod Problem
Topic: BigMod
solution:
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/// https://www.devskill.com/CodingProblems/ViewProblem/23 | |
#include<bits/stdc++.h> | |
#define ll long long | |
using namespace std; | |
inline ll modMul(ll a, ll b, ll MOD) | |
{ | |
ll r = 0; | |
a %= MOD; | |
while(b) | |
{ | |
if(b%2) r = (r +a)%MOD; | |
a = (a+a)%MOD; | |
b/=2; | |
} | |
return r; | |
} | |
inline ll modPow(ll b, ll p, ll MOD) | |
{ | |
ll r = 1; | |
while(p) | |
{ | |
if(p%2) r = modMul(r, b, MOD); | |
b = modMul(b, b, MOD); | |
p/=2; | |
} | |
return r; | |
} | |
int main() | |
{ | |
ios_base::sync_with_stdio(false); | |
cin.tie(NULL); | |
ll a,b,c; | |
int t,i=0; | |
cin >> t; | |
while(t--) | |
{ | |
i++; | |
cin>>a>>b>>c; | |
cout<<"Case "<<i<<": "; | |
cout<<modPow(a,b,c)<<endl; | |
} | |
return 0; | |
} | |
No comments:
Post a Comment