Description
给你三个整数a,b,p,求a^b 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 3Sample Output
7 2
int Quick_pow(int a, int b)
{
//ans为累乘变量,初始化为1
//temp初始化为底数a,然后逐渐变为a²,a³...
int ans = 1; int temp = a;
while (b)
{
if (b % 2) ans = ans * temp;
temp = temp * temp;
b /= 2;
}
return ans;
}
//(a * b) % p = (a % p * b % p) % p
int Quick_pow_mod(int a, int b, int mod)
{
int ans = 1; int temp = a;
while (b)
{
if (b % 2) ans = (ans * temp) % mod;
//底数平方后取模,缩小底数的规模
temp = (temp * temp) % mod;
b /= 2;
}
return ans;
}
int main()
{
int t;
cin >> t;
while (t--)
{
int a, b, p;
cin >> a >> b >> p;
cout << Quick_pow_mod(a, b, p) << endl;
}
}