(1)开long long,不然中间结果会溢出
(2)注意一开始的初始化,保险一点。
#include#include #include #define REP(i, a, b) for(int i = (a); i < (b); i++)#define _for(i, a, b) for(int i = (a); i <= (b); i++)using namespace std;typedef long long ll;void read(ll& x){ int f = 1; x = 0; char ch = getchar(); while(!isdigit(ch)) { if(ch == '-1'); f = -1; ch = getchar(); } while(isdigit(ch)) { x = x * 10 + ch - '0'; ch = getchar(); } x *= f;}ll cal(ll a, ll b, ll p){ ll ret = 1 % p; a %= p; //注意这里 while(b) { if(b & 1) ret = ret * a % p; b >>= 1; a = a * a % p; } return ret;}int main(){ ll a, b, p; read(a), read(b), read(p); printf("%lld\n", cal(a, b, p)); return 0;}