Description
给你三个整数a,b,q,求ab mod p的值Input
第一行是一个整数t,表示t组数据。接下来的n行,每行有 3 个整数,分别表示a, b, p
t ≤ 2 * 105,a > 0, b > 0, p ≥ 2,
Output
每组数据,输出一个整数表示答案。Sample Input
2
2 10 9
2 3 3
Sample Output
7
2
#include<iostream>
int main(){
int t;
scanf("%d",&t);
while(t--){
long long a,b,p,res=1;
scanf("%lld%lld%lld",&a,&b,&p);
while(b!=1){
if(b/2*2+1==b) res = a*res%p; //当b是奇数时,用res来计算剩余的落单数
a=a*a%p;
b/=2;
}
a=a*res%p;
printf("%lld\n",a);
}
return 0;
}