bdfz_2024_summer/day6/binaryExponentiation/binaryExponentiation.cpp
2024-08-08 14:12:15 +08:00

33 lines
487 B
C++

#include<bits/stdc++.h>
using namespace std;
int binExp(int b,int e,int m){
int r = 1;
while(e>0){
if(e%2==1){
r = (r*b)%m;
}
b = b*b %m;
e = e>>1;
}
return r;
}
#ifndef ISTEST
signed main(){
cin.sync_with_stdio(false);
cin.tie(0);
int a,b,p;
cin>>a>>b>>p;
int r= binExp(a,b,p);
cout<<a<<"^"<<b<<" mod "<<p<<"="<<r<<endl;
}
#endif
#ifdef ISTEST
int main(){
assert(binExp(2, 10, 9)==7);
}
#endif