大数处理用phtyon或java很方便,不过正常比赛也没有这样的题
import java.math.BigDecimal;
import java.math.BigInteger;
import java.util.Scanner;
public class Main {
public static void main(String[] args){
Scanner in = new Scanner(System.in);
int T = in.nextInt();
while(T-->0){
BigInteger a = in.nextBigInteger();
BigInteger b = in.nextBigInteger();
BigInteger mod = in.nextBigInteger();
BigInteger ans = a.modPow(b,mod);
System.out.println(ans);
}
}
}
快速幂时,由于普通乘法会超限,所以需要用乘法快速幂
#include<bits/stdc++.h>
#define lc ((o) << 1)
#define rc ((o) << 1 | 1)
using namespace std;
typedef long long ll;
typedef pair<int,int>Pair;
const int N=1e5+10;
const int MOD=1e9+7;
ll mul(ll a,ll b,ll mod){//快速乘
ll ans=0,res=a;
while(b){
if(b&1) ans=(ans+res)%mod;
res=(res+res)%mod;
b>>=1;
}
return ans;
}
ll quickPower(ll a,ll b,ll mod){
ll ans=1,base=a;
while(b>0){
if(b&1)
ans=mul(ans,base,mod);
base=mul(base,base,mod);
b>>=1;
}
return ans;
}
int main()
{
ll t,a,b,p;
cin>>t;
while(t--)
{
cin>>a>>b>>p;
cout<<quickPower(a,b,p)<<endl;
}
}