【题意】
题意很好 理解, 求 f(a^b) %n 为多少;
通过 列出前几项可以发现, 是有规律的 (废话 都 20位了 这么大的数 没规律才怪, )
超过18位 用 unsigned long long 输入 输出 用 %llu
可以发现 当 出现 F[I-1] =F[1] F[I]=F[2] 时 是 有周期的, 周期在 n^2 内 一定能找到
但是 很容易超时,
可以提前打表预处理
【代码实现】
//#include <bits/stdc++.h>
#include <iostream>
#include <stdio.h>
#include <algorithm>
#include <cmath>
#include <math.h>
#include <cstring>
#include <string>
#include <queue>
#include <deque>
#include <stack>
#include <stdlib.h>
#include <list>
#include <map>
#include <set>
#include <bitset>
#include <vector>
#define mem(a,b) memset(a,b,sizeof(a))
#define findx(x) lower_bound(b+1,b+1+bn,x)-b
#define FIN freopen("input.txt","r",stdin)
#define FOUT freopen("output.txt","w",stdout)
#define S1(n) scanf("%d",&n)
#define SL1(n) scanf("%I64d",&n)
#define S2(n,m) scanf("%d%d",&n,&m)
#define SL2(n,m) scanf("%I64d%I64d",&n,&m)
#define Pr(n) printf("%d\n",n)
#define lson rt << 1, l, mid
#define rson rt << 1|1, mid + 1, r
using namespace std;
typedef unsigned long long ll;
const double PI=acos(-1);
const int INF=0x3f3f3f3f;
const double esp=1e-6;
const int maxn=1e5+5;
const int MAXN=50005;
const int mod=1e9+7;
int dir[5][2]={0,1,0,-1,1,0,-1,0};
ll f[1005][maxn];
ll go[maxn];
ll a,b,n;
ll qpow(ll x,ll y,ll MOD){ll res=1;for(;y;y>>=1){if(y&1)res=(res*x)%MOD;x=(x*x)%MOD;}return res;}
void init()
{
for(int i=2;i<=1000;i++)
{
f[i][0]=0,f[i][1]=1,f[i][2]=1;
for(int j=3;;j++)
{
f[i][j]=(f[i][j-1]%i+f[i][j-2]%i)%i;
if(f[i][j]==f[i][1]&&f[i][j-1]==f[i][0])
{
go[i]=j-1;
break;
}
}
}
}
int main()
{
init();
int T;
cin>>T;
while(T--)
{
scanf("%llu%llu%llu",&a,&b,&n);
if(n==1|| !a)
{
printf("0\n");
continue;
}
//printf("%llu %llu \n",a%go[n],b);
ll t=qpow(a%go[n],b,go[n]);
printf("%llu\n",f[n][t%go[n]]);
}
return 0;
}