HDU 5451 Best Solver(数学)

本文介绍了一种针对形如(5+2√6)^n的无理数进行取模运算的方法。通过构造整数序列并利用递推公式,文章详细解释了如何找到该无理数的整数部分,并给出了具体的C++实现代码。适用于解决特定数学问题。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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

An integer , indicating there are  test cases. 
Following are  lines, each containing two integers  and , as introduced above.

Output

The output contains exactly  lines. 
Each line contains an integer representing .

Sample Input

7
0 46337
1 46337
3 46337
1 46337
21 46337
321 46337
4321 46337

Sample Output

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


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值