斐波那契数列的通项:

\[\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. 洛谷T44252 线索_分治线段树_思维题

    分治线段树,其实就是将标记永久化,到最后再统一下传所有标记. 至于先后顺序,可以给每个节点开一个时间戳. 一般地,分治线段树用于离线,只查询一次答案的题目. 本题中,标记要被下传 222 次. Cod ...

  2. Pyhton学习——Day24

    # #面向对象设计:# def dog(name,gender,type):# def jiao(dog):# print('One Dog[%s],wfwfwf'%dog['name'])# def ...

  3. vue父组件引用子组件方法显示undefined问题原因及解决方法

    关于vue父组件引用子组件问题 1.首先导入子组件并且在components中定义子组件 2.引用子组件,并定义ref,ref定义的名称用于 this.$refs所调用的名称 3.调用子组件的方法 ( ...

  4. 简单实现双向数据绑定mvvm。

  5. 【【henuacm2016级暑期训练】动态规划专题 O】Robot Rapping Results Report

    [链接] 我是链接,点我呀:) [题意] 让你确定一个最小的k 使得1..k这些比赛的结果能够推导出所有人之间的实力大小 [题解] 如果关系越多.那么就越能确定所有人之间的大小关系. (多一点也能唯一 ...

  6. JDBC-连接数据库代码

    package com.zxc.connection; import java.sql.Connection; import java.sql.DriverManager; public class ...

  7. Java知识点解析

    JAVA 1:简述Java的基本历史 java起源于SUN公司的一个GREEN的项目,其原先目的是为家用消费电子产品 发送一个信息的分布式代码系统,通过发送信息控制电视机.冰箱等. 2:简单写出Jav ...

  8. hdu Escape

    Escape 题目: 非常裸的多重匹配. 可是点数较多,所以要用到状态压缩. . .. .. 第一次写. 好厉害的赶脚. #include <iostream> #include < ...

  9. Making ViewState More Secure

    Unencrypted view state in ASP.NET 2.0 could leak sensitive information https://www.rapid7.com/db/vul ...

  10. jsp页面String path = request.getContextPath(); String basePath = request.getScheme() + "://" + request.getServerName() + ":" + request.getServerPort() + path + "/";作用!!!!!

    转自:https://blog.csdn.net/kiwangruikyo/article/details/81130311 <%String path = request.getContext ...