斐波那契数列的通项:

\[\frac{1}{\sqrt{5}}((\frac{1+\sqrt{5}}{2})-(\frac{1-\sqrt{5}}{2}))
\]

设T=\(\sqrt{5}*N\),\(y=\frac{\sqrt{5}+1}{2}\)

原式可化为\(y^n-(-\frac{1}{y}^n) \equiv T(mod\ p)\)

我们设\(t=y^n\)

原式可继续化为\(t-T*t \equiv (-1)^n(mod\ p)\)

然后我们对n进行奇偶讨论。

即分别求出\(t-T*t\equiv 1(mod\ p)\)和\(t-T*t\equiv -1(mod\ p)\)的t的解,这个用求根公式+二次剩余求出。

最后离散对数求出n。

(我写的时候求根公式背错了调了半个小时。。)

#include<iostream>
#include<cstring>
#include<cstdio>
#include<cmath>
#include<algorithm>
#include<cstdlib>
#include<ctime>
using namespace std;
#define int long long
const int p=1e9+9;
const int N=201000;
int head[N],cnt;
int num,ans[10],w;
struct edge{
int w,id,nxt;
}e[N];
void add(int u,int w,int id){
for(int i=head[u];i;i=e[i].nxt)
if(e[i].w==w&&e[i].id<id)swap(e[i].id,id);
cnt++;
e[cnt].nxt=head[u];
e[cnt].w=w;
e[cnt].id=id;
head[u]=cnt;
}
struct comple{
int x,y;
comple (int xx=0,int yy=0){
x=xx;y=yy;
}
};
comple operator *(comple a,comple b){
return comple(((a.x*b.x%p+a.y*b.y%p*w%p)%p+p)%p,((a.x*b.y%p+a.y*b.x%p)%p+p)%p);
}
int random(int x){
return rand()*rand()%p;
}
int ksm(int x,int b){
int tmp=1;
while(b){
if(b&1)tmp=tmp*x%p;
x=x*x%p;
b>>=1;
}
return tmp;
}
comple ksm(comple x,int b){
comple tmp(1,0);
while(b){
if(b&1)tmp=tmp*x;
x=x*x;
b>>=1;
}
return tmp;
}
int Sqrt(int x){
if(p==2)return x;
if(ksm(x,(p-1)/2)+1==p)return -1;
int a;
while(233){
a=random(p);
w=((a*a%p-x)%p+p)%p;
if(ksm(w,(p-1)/2)+1==p)break;
}
comple res(a,1);
comple ans(0,0);
ans=ksm(res,(p+1)/2);
return ans.x;
}
int BSGS(int a,int b){
int block=sqrt(p)+1;
int tmp=b;
for(int i=0;i<block;i++,tmp=tmp*a%p)add(tmp%200000+1,tmp,i);
a=ksm(a,block);
if(a==0)return b==0?1:-1;
tmp=1;
for(int i=0;i<=block;i++,tmp=tmp*a%p){
for(int j=head[tmp%200000+1];j;j=e[j].nxt)
if(e[j].w==tmp&&i*block-e[j].id>=0)return i*block-e[j].id;
}
return -1;
}
int read(){
int sum=0,f=1;char ch=getchar();
while(ch<'0'||ch>'9'){if(ch=='-')f=-1;ch=getchar();}
while(ch>='0'&&ch<='9'){sum=sum*10+ch-'0';ch=getchar();}
return sum*f;
}
signed main(){
srand(time(NULL));
int n=read()%p;
int a=Sqrt(5);
int T=a*n%p;
int y=(1+a)*ksm(2,p-2)%p;
int x1=Sqrt(T*T%p+4ll);
if(x1!=-1){
int t1=(T+x1)%p*ksm(2,p-2)%p;
int t2=((T-x1)%p+p)%p*ksm(2,p-2)%p;
int ans1=BSGS(y,t1);
cnt=0;memset(head,0,sizeof(head));
int ans2=BSGS(y,t2);
if(ans1!=-1)ans[++num]=ans1;
if(ans2!=-1)ans[++num]=ans2;
}
int x2=Sqrt(T*T%p-4);
if(x2!=-1){
int t1=(T+x2)%p*ksm(2,p-2)%p;
int t2=((T-x2)%p+p)%p*ksm(2,p-2)%p;
cnt=0;memset(head,0,sizeof(head));
int ans1=BSGS(y,t1);
cnt=0;memset(head,0,sizeof(head));
int ans2=BSGS(y,t2);
if(ans1!=-1)ans[++num]=ans1;
if(ans2!=-1)ans[++num]=ans2;
}
if(num==0)printf("-1");
else {
sort(ans+1,ans+1+num);
printf("%lld",ans[1]);
}
return 0;
}

BZOJ 5104 Fib数列(二次剩余+BSGS)的更多相关文章

  1. @bzoj - 5104@ Fib数列

    目录 @description@ @solution@ @accepted code@ @details@ @description@ Fib数列为1,1,2,3,5,8... 求在Mod10^9+9 ...

  2. 【BZOJ5104】Fib数列(BSGS,二次剩余)

    [BZOJ5104]Fib数列(BSGS,二次剩余) 题面 BZOJ 题解 首先求出斐波那契数列的通项: 令\(A=\frac{1+\sqrt 5}{2},B=\frac{1-\sqrt 5}{2}\ ...

  3. BZOJ5104 Fib数列 二次剩余、BSGS

    传送门 发现只有通项公式可以解决考虑通项公式 \(F_n = \frac{1}{\sqrt{5}}((\frac{1+\sqrt{5}}{2})^n - (\frac{1-\sqrt{5}}{2})^ ...

  4. bzoj5104 Fib数列(BSGS+二次剩余)

    快AFO了才第一次写二次剩余的题…… 显然应该将Fn写成通项公式(具体是什么写起来不方便而且大家也都知道),设t=((1+√5)/2)n,T=√5N,然后可以得到t-(-1)t/t=√5N,两边同时乘 ...

  5. FIB数列

    斐波那契级数除以N会出现循环,此周期称为皮萨诺周期. 下面给出证明 必然会出现循环 这是基于下面事实: 1. R(n+2)=F(n+2) mod P=(F(n+1)+F(n)) mod P=(F(n+ ...

  6. bzoj5104: Fib数列

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

  7. 动态规划之Fib数列类问题应用

    一,问题描述 有个小孩上楼梯,共有N阶楼梯,小孩一次可以上1阶,2阶或者3阶.走到N阶楼梯,一共有多少种走法? 二,问题分析 DP之自顶向下分析方式: 爬到第N阶楼梯,一共只有三种情况(全划分,加法原 ...

  8. UVaLive 3357 Pinary (Fib数列+递归)

    题意:求第 k 个不含前导 0 和连续 1 的二进制串. 析:1,10,100,101,1000,...很容易发现长度为 i 的二进制串的个数正好就是Fib数列的第 i 个数,因为第 i 个也有子问题 ...

  9. 【bzoj5118】Fib数列2 费马小定理+矩阵乘法

    题目描述 Fib定义为Fib(0)=0,Fib(1)=1,对于n≥2,Fib(n)=Fib(n-1)+Fib(n-2) 现给出N,求Fib(2^n). 输入 本题有多组数据.第一行一个整数T,表示数据 ...

随机推荐

  1. ZBrush2018中文版全球同步发售,终身授权

    ZBrush 2018于2018.3.28发布了!这个我们期待已久的2018新版本等了几年,它终于来了! 不负众望,ZBrush 2018的这一次更新,简直炸裂,新功能真是太好用了!2018版是ZBr ...

  2. servlet缺省路径

    servlet缺省路径 servlet的缺省路径(<url-pattern>/</url-pattern>)是在tomcat服务器内置的一个路径.该路径对应的是一个Defaul ...

  3. elk集群配置配置文件中节点数配多少

    配置elk集群时,遇到,elasticsearch配置文件中的一个配置discovery.zen.minimum_master_nodes: 2.这里是三配的2 看到某一位的解释是这样:为了避免脑裂, ...

  4. BZOJ 2260 商店购物(最小树形图)

    不会最小树形图的出门左转 其实如果确定每种商品第一件的购买顺序,那么剩下的商品肯定是以最优惠价格购买的. 如何确定各种商品第一件购买时的最小价值呢? 考虑如果购买了\(a_i\)这种商品,那么就能以\ ...

  5. mac上用VMWare虚拟机装Linux-Ubuntu

    1.下载vmware fusion for Mac 网上有很多,大家自己找吧 2.安装Vmware 3.序列号可以在网上找,或者淘宝几块钱买一个(记得买对应版本的) 4.之后就下一步,完成就好 安装U ...

  6. Jquery Map遍历

    var map = { 地名: ["北京","天津","上海"], 民族: ["汉族","藏族",& ...

  7. 【Python 学习】通过while循环和for循环猜测年龄

    Python中可以通过while和for循环来限制猜测年龄的次数 1. 在猜测年龄的小程序中,不加循环的代码是这样的: age_of_yu = 23 guess_age = int(input(&qu ...

  8. Windows桌面美化

    [工具链接]链接: https://pan.baidu.com/s/12aUGsu91F8WfaW5HU5ps3g 提取码: dnan [样例] [美化步骤] 1.解压下载文件,安装两个软件: Sta ...

  9. 引入拦截器及swagger支持及解决redis无法初始化问题

    Springboot引入拦截器 自定义的拦截器类 Interceptor package cn.zytao.taosir.auth.config; import javax.annotation.Re ...

  10. Shiro:授权的相关实现

    Shiro:授权的相关实现 一.使用Shiro过滤器实现授权 设置好授权拦截跳转的请求地址 /** * 创建ShiroFilterFactoryBean */ @Bean public ShiroFi ...