bzoj 5104: Fib数列 二次剩余+BSGS

题意

Fib数列为1,1,2,3,5,8…
求在Mod10^9+9的意义下,数字N在Fib数列中出现在哪个位置
无解输出-1
N < = 10^9+9

分析

第一次做跟二次剩余有关的题目。
斐波那契数列的通项是

Fn=(1+52)n(152)n5

t=(1+52)n,T=5N
可以得到
t(1)n1t=5N

两边同乘 t 可得
t25Nt(1)n=0

枚举 n 是偶数或奇数的情况,通过求根公式可以求出t,再用BSGS求出 n <script type="math/tex" id="MathJax-Element-8">n</script>的值即可。

代码

#include<iostream>
#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<algorithm>
#include<map>
#include<cmath>
using namespace std;

typedef long long LL;

const int sq5=383008016;
const int ny2=500000005;
const int MOD=1000000009;
const int inf=0x7fffffff;

int n,w;
map<int,int> ma;

struct com
{
    int x,y;

    com operator * (const com &a) {return (com){((LL)x*a.x+(LL)y*a.y%MOD*w)%MOD,((LL)x*a.y+(LL)y*a.x)%MOD};}
};

int ksm(int x,int y)
{
    int ans=1;
    while (y)
    {
        if (y&1) ans=(LL)ans*x%MOD;
        x=(LL)x*x%MOD;y>>=1;
    }
    return ans;
}

int sq(int n)
{
    if (ksm(n,(MOD-1)/2)!=1) return 0;
    int a=0;
    while (ksm(((LL)a*a%MOD-MOD-n)%MOD,(MOD-1)/2)==1) a=rand();
    com x=(com){a,MOD-1},ans=(com){1,0};int y=(MOD+1)/2;w=((LL)a*a%MOD+MOD-n)%MOD;
    while (y)
    {
        if (y&1) ans=ans*x;
        x=x*x;y>>=1;
    }
    return ans.x;
}

int BSGS(int t,int n)
{
    int block=sqrt(MOD),ny=ksm(t,MOD-2),tmp=n;
    ma.clear();
    ma[n]=MOD;
    for (int i=1;i<block;i++) tmp=(LL)tmp*ny%MOD,ma[tmp]=!ma[tmp]?i:ma[tmp];
    if (ma[1]) return ma[1]%MOD;
    tmp=1;t=ksm(t,block);
    for (int i=1;i<=block;i++)
    {
        tmp=(LL)tmp*t%MOD;
        if (ma[tmp]) return (i*block+ma[tmp])%MOD;
    }
    return -1;
}

int main()
{
    scanf("%d",&n);
    int t=(LL)(sq5+1)*ny2%MOD,T=(LL)sq5*n%MOD;
    int ans=inf,x,y,r0=sq((LL)5*n*n%MOD+4),r1=sq((LL)5*n*n%MOD-4);

    x=(LL)(T+r0)*ny2%MOD;y=BSGS(t,x);
    if (y>-1&&!(y&1)&&r0) ans=min(ans,y);

    x=(LL)(T+MOD-r0)*ny2%MOD;y=BSGS(t,x);
    if (y>-1&&!(y&1)&&r0) ans=min(ans,y);

    x=(LL)(T+r1)*ny2%MOD;y=BSGS(t,x);
    if (y>-1&&(y&1)&&r1) ans=min(ans,y);

    x=(LL)(T+MOD-r1)*ny2%MOD;y=BSGS(t,x);
    if (y>-1&&(y&1)&&r1) ans=min(ans,y);

    if (ans==inf) puts("-1");
    else printf("%d",ans);
    return 0;
}
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值