莫欺少年穷,修仙之旅在这开始—>算法基础课题解
快速幂Example: a=5,b=5=(101)2,ab=5(101)2=520+22=520×522
时间复杂度: O(logb)
#include<bits/stdc++.h>
using namespace std;
typedef long long LL;
int qmi(int a,int b,int p)
{
int res=1;
while(b)
{
if(b&1) res=(LL)res*a%p;
a=(LL)a*a%p;
b>>=1;
}
return res;
}
int main()
{
int n;
cin>>n;
while(n--)
{
int a,b,p;
cin>>a>>b>>p;
cout<<qmi(a,b,p)<<endl;
}
return 0;
}