(1)开long long,不然中间结果会溢出

(2)注意一开始的初始化,保险一点。

#include<cstdio>
#include<cctype>
#include<algorithm>
#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;
}

 

相关文章:

  • 2022-12-23
  • 2021-08-30
  • 2022-02-06
  • 2021-12-05
  • 2022-12-23
  • 2020-06-23
  • 2021-12-03
  • 2021-06-14
猜你喜欢
  • 2022-12-23
  • 2022-12-23
  • 2022-02-14
  • 2022-12-23
  • 2022-01-04
  • 2021-12-17
  • 2022-12-23
相关资源
相似解决方案