快速幂
思想是用指数的乘法性质,用二分思想执行操作
1 #include<bits/stdc++.h> 2 using namespace std; 3 int b,p,k,ans; 4 void power(int a,int n) 5 { 6 ans=1; 7 while(n) 8 { 9 if(n%2==1)ans=(long long)ans*a%k; 10 a=(long long)a*a%k; 11 n/=2; 12 } 13 } 14 int main() 15 { 16 cin>>b>>p>>k; 17 power(b,p); 18 cout<<b<<"^"<<p<<" mod "<<k<<"="<<ans%k; 19 return 0; 20 }