思路:显然无理数是不能取模的,考虑An=(5+2sqrt(6))^n+(5-2sqrt(6))^n显然是一个整数,那么因为0<5-2sqrt(6)<1,而它们两者相加又是整数,那么就说明5+2sqrt(6)和5-2sqrt(6)的小数点部分是互补的,那么(5+2sqrt(6))^n+(5-2sqrt(6))^n-1其实就是5+2sqrt(6)的整数部分,就是结果了,然后就是求An的递推式了
#include<bits/stdc++.h>
using namespace std;
#define LL long long
const int maxn = 48000;
int mod,a[maxn];
LL qpow(LL a,LL b,LL m) // 返回a^b % m
{
LL ans = 1;
while(b)
{
if(b&1)
ans=(ans*a)%m;
a = (a*a)%m;
b>>=1;
}
return ans;
}
int gao(int mod)
{
a[0]=10%mod;
a[1]=98%mod;
for(int i = 2;i<=maxn;i++)
{
a[i]=(a[i-1]*10-a[i-2]+mod)%mod;
if(a[i-1]==a[0]&&a[i]==a[1])
return i-1;
}
}
int main()
{
int T,cas=1;
scanf("%d",&T);
while(T--)
{
LL n;
scanf("%lld%d",&n,&mod);
int x = gao(mod);
int res = qpow(2,n,x);
printf("Case #%d: %d\n",cas++,(a[res]-1+mod)%mod);
}
}