二元组有循环节,找出循环,在快速幂求出第几个,输出即可, (LL 大法好)
#include<iostream>
#include<cstdio>
using namespace std;
typedef unsigned long long LL;
const int maxn = 1000 + 5;
int M[maxn];
int F[maxn][maxn<<2];
int pow_mod(LL a,LL b,int mod) {
LL ret = 1;
LL temp = a;
while(b) {
if(b & 1) ret = ret * temp % mod;
temp = temp * temp % mod;
b >>= 1;
}
return ret;
}
void Int() {
for(int n = 2; n <= 1000; ++n) {
F[n][0] = 0;
F[n][1] = 1;
for(int j = 2; ; ++j) {
F[n][j] = (F[n][j-1] % n+ F[n][j-2] % n) %n;
if(F[n][j] == 1 && F[n][j-1] == 0)
{ M[n] = j-1; break; }
}
}
}
int main() {
Int();
int t;
cin >> t;
while(t --) {
LL a, b ,n;
cin >> a >> b >> n;
if(a == 0 || n == 1) cout << "0" <<endl;
else {
cout << F[n][pow_mod(a % M[n],b,M[n])] << endl;
}
}
return 0;
}