题目描述
X的n次普通幂对p驱魔的结果
算法1
C++ 代码
#include <bits/stdc++.h>
using namespace std;
int x;
string p;
int _pow(int x, int n, int p){ // 求a^n普通幂,对p求模
int res = 1;
for(int i = 1; i <= n; ++ i){
res = res * x % p;
}
return res;
}
int main()
{
ios::sync_with_stdio(0), cin.tie(0), cout.tie(0);//-字符串快速幂
int t; cin >> t;
while(t --){
cin >> x >> p;
p = "~" + p;//1.因为幂本质是乘法,所以在任何地方都可以取模
int res = 1;
for(int i = 1; i <= p.length() - 1; ++ i){ ///把次方的每一位取出来,对x循环乘,x自身对每一位相应求^10
res = res * _pow(x, p[i] - '0', 10) % 10;
if(i != p.length() - 1)res = _pow(res, 10, 10);
}
cout << res << '\n';
}
return 0;
}