快速幂

快速幂算法依赖于以下明显的公式:

 1 #include <iostream>
 2 #include <algorithm>
 3 using namespace std;
 4 int main()
 5 {
 6     long long x,n,m,ans=1;
 7     long long i,j;
 8     cin>>x>>n>>m;
 9     x=x%m;
10     while(n>0)
11     {
12         if(n%2==1)
13             ans=(ans*x)%m;
14         n=n/2;
15         x=(x*x)%m;
16     }
17     cout<<ans<<endl;
18 }
原文地址:https://www.cnblogs.com/a1225234/p/4756816.html