#include<iostream>
using namespace std;
int f[3000];
int main()
{
unsigned long long a,b,n,i,j,t,ans,m;
cin>>t;
for(j=0;j<t;j++){
cin>>a>>b>>n;
if(n==1||a==0) cout<<"0"<<endl;
else if(a==1||b==0) cout<<"1"<<endl;
else {
f[0]=0;
f[1]=1;
i=2;
for(i=2;i<3000;i++){
f[i]=(f[i-1]+f[i-2])%n;
if(f[i-1]==1&&f[i]==0)
break;
}
m=i;
ans=1;
a%=m;
while(b>0){
if(b%2==1) ans=(ans*a)%m;
a=(a*a)%m;
b>>=1;
}
cout<<f[ans]<<endl;
}
}
return 0;
}
时间: 2024-10-14 19:39:37