#include<iostream>
#include<stdio.h>
using namespace std;
#define ll long long
int qmi(int a,int k,int p)
{
ll res=1;
while(k)
{
if(k&1)res=(ll)res*a%p;
k=k>>1;
a=(ll)a*a%p;
}
return res;
}
int main()
{
int n;
cin>>n;
while(n--)
{
ll a,k,p;
cin>>a>>k>>p;
cout<<qmi(a,k,p)<<endl;
}
return 0;
}
#include<iostream>
#include<stdio.h>
using namespace std;
#define ll long long
int main()
{
int n;
cin>>n;
while(n--)
{
ll a,k,p;
cin>>a>>k>>p;
ll res=1;
//将幂转化为二进制,对每一位都进行判定
while(k>0)
{
if(k&1)res=(ll)res*a%p;
k=k>>1;
a=(ll)a*a%p;
}
cout<<res<<endl;
}
return 0;
}
原文:https://www.cnblogs.com/BeautifulWater/p/14729169.html