0
点赞
收藏
分享

微信扫一扫

HDU 5451 Best Solver(数学)


思路:显然无理数是不能取模的,考虑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);
   }
}





Description



The so-called best problem solver can easily solve this problem, with his/her childhood sweetheart. 


It is known that 




For a given integer 


 and a given prime number 


, print 


. (


 means the integer part of 





Input


, indicating there are 


 test cases. 

Following are 


 lines, each containing two integers 


 and 


, as introduced above.

Output


 lines. 

Each line contains an integer representing 

.

Sample Input


70 463371 463373 46337 1 46337 21 46337 321 46337 4321 46337


Sample Output


Case #1: 97Case #2: 969Case #3: 16537Case #4: 969 Case #5: 40453 Case #6: 10211 Case #7: 17947





举报

相关推荐

0 条评论