https://www.acwing.com/problem/content/91/
求 a 的 b 次方对 p取模的值。
输入格式
三个整数 a,b,p在同一行用空格隔开。
输出格式
输出一个整数,表示a^b mod p的值。
数据范围
0≤a,b,p≤109
输入样例:
3 2 7
输出样例:
2
#include <iostream>
using namespace std;
typedef unsigned long long ULL;
ULL a,b,p;
int main(){
cin >> a>> b>> p;
ULL ans=1,base=a%p;
//求a的b次方对p的求模
while(b!=0)
{
if(b&1!=0)
ans=ans*base%p;
base=base*base%p;
b>>=1;
}
cout << ans%p << endl;
return 0;
}