BZOJ 5104 Fib数列(二次剩余+BSGS)
斐波那契数列的通项:
\]
设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)的更多相关文章
- @bzoj - 5104@ Fib数列
目录 @description@ @solution@ @accepted code@ @details@ @description@ Fib数列为1,1,2,3,5,8... 求在Mod10^9+9 ...
- 【BZOJ5104】Fib数列(BSGS,二次剩余)
[BZOJ5104]Fib数列(BSGS,二次剩余) 题面 BZOJ 题解 首先求出斐波那契数列的通项: 令\(A=\frac{1+\sqrt 5}{2},B=\frac{1-\sqrt 5}{2}\ ...
- BZOJ5104 Fib数列 二次剩余、BSGS
传送门 发现只有通项公式可以解决考虑通项公式 \(F_n = \frac{1}{\sqrt{5}}((\frac{1+\sqrt{5}}{2})^n - (\frac{1-\sqrt{5}}{2})^ ...
- bzoj5104 Fib数列(BSGS+二次剩余)
快AFO了才第一次写二次剩余的题…… 显然应该将Fn写成通项公式(具体是什么写起来不方便而且大家也都知道),设t=((1+√5)/2)n,T=√5N,然后可以得到t-(-1)t/t=√5N,两边同时乘 ...
- FIB数列
斐波那契级数除以N会出现循环,此周期称为皮萨诺周期. 下面给出证明 必然会出现循环 这是基于下面事实: 1. R(n+2)=F(n+2) mod P=(F(n+1)+F(n)) mod P=(F(n+ ...
- bzoj5104: Fib数列
Description Fib数列为1,1,2,3,5,8... 求在Mod10^9+9的意义下,数字N在Fib数列中出现在哪个位置 无解输出-1 Input 一行,一个数字N,N < = 10 ...
- 动态规划之Fib数列类问题应用
一,问题描述 有个小孩上楼梯,共有N阶楼梯,小孩一次可以上1阶,2阶或者3阶.走到N阶楼梯,一共有多少种走法? 二,问题分析 DP之自顶向下分析方式: 爬到第N阶楼梯,一共只有三种情况(全划分,加法原 ...
- UVaLive 3357 Pinary (Fib数列+递归)
题意:求第 k 个不含前导 0 和连续 1 的二进制串. 析:1,10,100,101,1000,...很容易发现长度为 i 的二进制串的个数正好就是Fib数列的第 i 个数,因为第 i 个也有子问题 ...
- 【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,表示数据 ...
随机推荐
- Simula-Virtual function
Simula is the name of two simulation programming languages, Simula I and Simula 67, developed in the ...
- my.cnf配置样例
[mysql] no-auto-rehash port = socket = /data/mysql/data/mysqld.sock [mysqld] user = mysql port = bas ...
- 路飞学城Python-Day31
19-生产者消费者模型 生产者:生成数据的任务 消费者:处理数据的任务 在并发编程的过程中,如果生产者处理速度很快,而消费者处理速度很慢,那么生产者就必须等待消费者处理,才能继续生产数据:同样的,如果 ...
- ES modules
注意:这篇文章讲的是正经的es module规范 及浏览器的实现!webpack项目中es module会被parse成commonjs,和这个没大关系! 总结: ES模块加载的主要过程: 构造 —— ...
- js类的使用
brush示例 以d3的一个brush进行叙述,示例见: https://bl.ocks.org/xunhanliu/6f0b46789842e9e19e6cfe9bd0b16806 应用情形: 当页 ...
- HDU 1576 A/B( 逆元水 )
链接:传送门 思路: 现在给出 n = A % 9973,n = A - A/9973×9973,已知 B|A ,设 A = Bx,可以得到如下形式的式子:Bx + 9973×y = n ,因为gcd ...
- poj 3311 Hie with the Pie (状压dp) (Tsp问题)
这道题就是Tsp问题,稍微加了些改变 注意以下问题 (1)每个点可以经过多次,这里就可以用弗洛伊德初始化最短距离 (2)在循环中集合可以用S表示更清晰一些 (3)第一维为状态,第二维为在哪个点,不要写 ...
- MyBatis学习总结(1)——MyBatis快速入门
一.Mybatis介绍 MyBatis是一个支持普通SQL查询,存储过程和高级映射的优秀持久层框架.MyBatis消除了几乎所有的JDBC代码和参数的手工设置以及对结果集的检索封装.MyBatis可以 ...
- ZJU 2676 Network Wars
Network Wars Time Limit: 5000ms Memory Limit: 32768KB This problem will be judged on ZJU. Original I ...
- nodejs是一个平台,是平台
node.js是用javascript来写服务器代码的平台