思路非常简洁的一个算法,这里只给出算法模板,建议读者阅读前详细了解快速幂算法。
//写在前面
//很简单的一个算法
#include <iostream>
#define LL long long
using namespace std;
LL Fast_power(LL x,LL y,LL mode)
{
LL sum=1;
while(y)
{
if(y&1)sum=((sum%mode)*(x%mode))%mode; //防溢出处理
x=(x%mode)*(x%mode);
y=y>>1;
}
return sum;
}
int main()
{
int a,b,mode;cin>>a>>b>>mode;
cout<<Fast_power(a,b,mode)<<'\n';
}