原题链接:https://vjudge.net/problem/UVA-11582
分类:斐波那契
备注:模算数
特别注意一下n=1,还有long long最大才1<<63-1,要开unsigned long long
#include<bits/stdc++.h>
using namespace std;
typedef unsigned long long ull;
const int maxn=1e3+5;
int t,n,pos[maxn],f[maxn*maxn];
ull a,b,m;
ull getPos(){
a=a%m;
ull res=1;
while(b){
if(b&1)res=res*a%m;
b>>=1;
a=a*a%m;
}
return res%m;
}
int main(void){
// freopen("in.txt","r",stdin);
f[0]=0, f[1]=1;
scanf("%d",&t);
while(t--){
scanf("%llu%llu%d",&a,&b,&n);
if(n==1){
printf("0\n"); continue;
}
memset(pos,-1,sizeof(pos));
for(int i=2;i<=n*n;i++){
f[i]=(f[i-1]+f[i-2])%n;
if(f[i]==1&&f[i-1]==0){
m=i-1; break;
}
}
printf("%d\n",f[getPos()]);
}
return 0;
}