HDU -- 5451 Best Solver(广义斐波那契数列)

题目vj链接

题面:在这里插入图片描述

题意:
y = ( 5 + 2 6 ) 1 + 2 x , x ( 0 ≤ x < 2 32 ) y=(5+2\sqrt6)^{1+2^x},x(0\le x<2^{32}) y=(5+26 )1+2x,x(0x<232)
求:
[ y ] % M , 其 中 [ y ] 指 y 的 整 数 部 分 , M 为 ≤ 46337 的 质 数 [y]\%M,其中[y]指y的整数部分,M 为 \le46337的质数 [y]%M,[y]yM46337

题解:
我们设 A n = ( 5 + 2 6 ) n , B n = ( 5 − 2 6 ) n , S n = A n + B n A_n=(5+2\sqrt6)^n,B_n=(5-2\sqrt6)^n,S_n=A_n+B_n An=(5+26 )n,Bn=(526 )n,Sn=An+Bn

我们可得 S n 是 整 数 , B n ≤ 1 , A n 形 式 与 y 一 致 , 那 么 [ y ] = S n − 1 S_n是整数,B_n\le 1,A_n形式与y一致,那么[y]=S_n-1 SnBn1,Any[y]=Sn1

观察:
S n = ( 5 + 2 6 ) n + ( 5 − 2 6 ) n S_n=(5+2\sqrt6)^n+(5-2\sqrt6)^n Sn=(5+26 )n+(526 )n 类似于斐波那契数列通项公式,猜测其与斐波那契数列具有类似的形式:具有递推式,对一个质数取模有循环节等等。

求解递推式:

合理猜测大法:
S 0 = 2 , S 1 = 10 , S 2 = 98 S_0=2,S_1=10,S_2=98 S0=2,S1=10,S2=98,然后我们类比斐波那契数列的递推式, S n 只 与 S n − 1 和 S n − 2 S_n只与S_{n-1}和S_{n-2} SnSn1Sn2有关,得到 S n = 10 ∗ S n − 1 − S n − 2 S_n=10*S_{n-1}-S_{n-2} Sn=10Sn1Sn2

数学论证:
S n = ( 5 + 2 6 ) n + ( 5 − 2 6 ) n S_n=(5+2\sqrt6)^n+(5-2\sqrt6)^n Sn=(5+26 )n+(526 )n

S n = ( ( 5 + 2 6 ) n − 1 + ( 5 − 2 6 ) n − 1 ) ∗ ( ( 5 + 2 6 ) + ( 5 − 2 6 ) ) − ( ( 5 + 2 6 ) n − 2 + ( 5 − 2 6 ) n − 2 ) S_n=((5+2\sqrt6)^{n-1}+(5-2\sqrt6)^{n-1})*((5+2\sqrt6)+(5-2\sqrt6))-((5+2\sqrt6)^{n-2}+(5-2\sqrt6)^{n-2}) Sn=((5+26 )n1+(526 )n1)((5+26 )+(526 ))((5+26 )n2+(526 )n2)

S n = 10 S n − 1 − S n − 2 S_n=10S_{n-1}-S_{n-2} Sn=10Sn1Sn2

求解:
既然已经求的递推式,那么可以有一下方法求解此问题。
(1)线性地推,杜教BM。
(2)矩阵快速幂。
(3)M较小,暴力寻找循环节。

代码:暴力找循环节

#include<iostream>
#include<cstdio>
#include<cstdlib>
#include<algorithm>
#include<cstring>
#include<cmath>
#include<string>
#include<queue>
#include<bitset>
#include<map>
#include<unordered_map>
#include<set>
#define ui unsigned int
#define ll long long
#define llu unsigned ll
#define ld long double
#define pr make_pair
#define pb push_back
#define lc (cnt<<1)
#define rc (cnt<<1|1)
#define len(x)  (t[(x)].r-t[(x)].l+1)
#define tmid ((l+r)>>1)
#define fhead(x) for(int i=head[(x)];i;i=nt[i])
#define max(x,y) ((x)>(y)?(x):(y))
#define min(x,y) ((x)>(y)?(y):(x))
using namespace std;

const int inf=0x3f3f3f3f;
const ll lnf=0x3f3f3f3f3f3f3f3f;
const double dnf=1e18;
const int mod=998244353;
const double eps=1e-8;
const double pi=acos(-1.0);
const int hp=13331;
const int maxn=200100;
const int maxm=100100;
const int maxp=100100;
const int up=100100;

int a[maxn];
int mypow(int a,int b,int mod)
{
    int ans=1;
    while(b)
    {
        if(b&1) ans=ans*a%mod;
        a=a*a%mod;
        b>>=1;
    }
    return ans;
}

int main(void)
{
    int t;
    scanf("%d",&t);
    for(int tt=1;tt<=t;tt++)
    {
        int x,mod;
        scanf("%d%d",&x,&mod);
        a[0]=2%mod,a[1]=10%mod;
        int tot=0;
        for(int i=2;i<maxn;i++)
        {
            a[i]=10*a[i-1]-a[i-2]+mod;
            a[i]%=mod;
            if(a[i]==a[1]&&a[i-1]==a[0])
            {
                tot=i-1;
                break;
            }
        }
        ll pp=(1+mypow(2,x,tot)%tot)%tot;
        printf("Case #%d: %d\n",tt,(a[pp]-1+mod)%mod);
    }
    return 0;
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值