快速幂!~

---恢复内容开始---

基本代码:

#include <iostream>
#include <algorithm>

using namespace std;
int PowerMod(long long a, long long b, int n)
{
int ans=1;
a=a%n;
while(b>0)
{
if(b%2==1)
ans=(ans*a)%n;
b/=2;
a=(a*a)%n;
};
return ans;
}
int main()
{
long long a,b;
int ans,n;
cin>>a>>b>>n;
ans=PowerMod(a,b,n);
cout<<ans<<endl;
return 0;
}

这里有一个重要的公式

个人认为 是这道题的关键所在

(a * b) mod n=(a mod n * b mod n) mod n

   利用这个公式   可以每乘一步就取一次模  为了避免ll爆掉

猜你喜欢

转载自www.cnblogs.com/darlingroot/p/10048586.html