利用快速幂解决问题,注意,要使用unsigned long long int
#include<cstdio>
#include<cstring>
#include<iostream>
#include<algorithm>
#include<vector>
#include<stack>
#include<queue>
#include<map>
#include<set>
#include<list>
#include<string>
#include<cmath>
#include<sstream>
#include<ctime>
using namespace std;
#define _PI acos(-1.0)
#define INF 1 << 10
#define esp 1e-6
typedef long long LL;
typedef unsigned long long ULL;
typedef pair<int,int> pill;
/*===========================================
===========================================*/
#define MAXD 1000 + 10
#define MAX_SIZE 3000
int vis[MAXD][MAXD];
int mod[MAXD][MAX_SIZE];
int _T[MAXD];
int Get_Cycle(ULL m){ /*求周期*/
int fi = 0 % m, fj = 1 % m;
int T;
memset(vis,0,sizeof(vis));
for(int i = 1 ; ; i++){
if(!vis[fi][fj]){
vis[fi][fj] = 1;
mod[m][i - 1] = fi;
int t = fj;
fj = (fi + fj) % m;
fi = t;
}
else{
T = i;
break;
}
}
return T;
}
ULL quick_mod(ULL a , ULL b , ULL m){
if(b == 0)
return 1 % m;
else
if(b == 1)
return a % m;
ULL ans = quick_mod(a, b / 2 , m);
ans = ((ans % m) * (ans % m)) % m;
if(b & 1)
ans = (ans * (a % m)) % m;
return ans;
}
int main(){
int T;
int _vis[MAXD] = {0};
scanf("%d",&T);
while(T--){
ULL a , b , m;
scanf("%llu%llu%llu",&a,&b,&m);
if(!_vis[m]){
_T[m] = Get_Cycle(m); /*获取周期,以及打出周期表*/
_vis[m] = 1;
}
ULL ans = quick_mod(a,b,_T[m] - 1);
printf("%d\n",mod[m][ans]);
}
return 0;
}