bzoj5104: Fib数列
2024-09-09 22:22:00
Description
Fib数列为1,1,2,3,5,8...
求在Mod10^9+9的意义下,数字N在Fib数列中出现在哪个位置
无解输出-1
Input
一行,一个数字N,N < = 10^9+9
$r_1=\frac{1+\sqrt 5}{2}\\ r_2=\frac{1-\sqrt 5}{2}=-\frac{1}{r_1}\\ N=Fib_x=r_1^x-r_2^x\\ N^2=r_1^{2x}+r_2^{2x}-2(-1)^x\\ ±(r_1^x+r_2^x)=\sqrt{N^2+4(-1)^x}\\ \frac{N±\sqrt{N^2+4(-1)^x}}{2}=r_1^x,-r_2^x\\ x=min(log_{r_1}(\frac{N±\sqrt{N^2+4(-1)^x}}{2}),log_{r_2}(-\frac{N±\sqrt{N^2+4(-1)^x}}{2}))\\ 枚举x的奇偶,利用离散对数计算答案$
#include<cstdio>
typedef long long i64;
const int P=1e9+,g=,sqrt5=,I2=(P+)/,B=;
const int r1=(P++sqrt5)/,r2=(P+-sqrt5)/;
const int lr1=,lr2=;
inline int mul(int a,int b){return (i64)a*b%P;}
inline void muls(int&a,int b){a=mul(a,b);}
inline int fix(int x){return x+(x>>&P);}
int pw(int a,int n){
int v=;
for(;n;n>>=,muls(a,a))if(n&)muls(v,a);
return v;
}
void exgcd(int a,int b,int&x,int&y){
if(!b){x=,y=;return;}
exgcd(b,a%b,y,x);
y-=a/b*x;
}
int inv(int a,int b){
int x,y;
exgcd(a,b,x,y);
return (x%b+b)%b;
}
int gcd(int a,int b){return b?gcd(b,a%b):a;}
int solve(int a,int b,int c,int tp){
if(!a)return b?-:-tp;
int g=gcd(a,c);
if(b%g)return -;
a/=g,b/=g,c/=g;
i64 t=(i64)b*inv(a,c)%c;
if(!t)t=c;
if(t%!=tp)t+=c;
return t%!=tp?-:t;
}
int h[][];
int&at(int x){
int w=x&;
while(h[w][]){
if(h[w][]==x)return h[w][];
w=(w+)&;
}
h[w][]=x;
return h[w][];
}
void pre(){
int v=;
for(int i=;i<B;++i){
at(v)=i+;
muls(v,g);
}
}
int log(int x){
int t=pw(g,P--B);
for(int i=;;i+=B){
int y=at(x);
if(y)return y-+i;
muls(x,t);
}
}
int sqrt(int x){
int t=log(x);
return t&?-:pw(g,t/);
}
int ans=-;
void upd(int v){
if(~v&&(v<ans||ans==-))ans=v;
}
void cal(int x,int tp){
upd(solve(lr1,log(x),P-,tp));
upd(solve(lr2,log(fix(-x)),P-,tp));
}
int main(){
pre();
int v;
scanf("%d",&v);
if(!v)return puts("-1"),;
muls(v,sqrt5);
int v2=mul(v,v);
int s1=sqrt(fix(v2-)),s2=sqrt(fix(v2+-P));
if(~s1){
cal(mul(fix(v+s1-P),I2),);
cal(mul(fix(v-s1),I2),);
}
if(~s2){
cal(mul(fix(v+s2-P),I2),);
cal(mul(fix(v-s2),I2),);
}
return printf("%d\n",ans),;
}
最新文章
- CorelDRAW X8 如何破解激活(附国际版安装包+激活工具) 2016-12-15
- 数据结构和算法 &ndash; 6.构建字典: DictionaryBase 类和 SortedList 类
- HDU4549 M斐波那契数列 矩阵快速幂+欧拉函数+欧拉定理
- 基于html5 canvas和js实现的水果忍者网页版
- Asp.net Mvc 第二回 UrlRouting
- Sublime text3使用技巧及快捷键
- Windows安全事件日志中的事件编号与描述
- 从Ubunt的安装到hadoop集群的搭建
- python+selenium自动化软件测试(第1章):环境搭建,你也可以直接用Anaconda!
- 移动端,input输入框被手机输入法解决方案
- 【Docker】(6)---Dockerfile文件
- canvas绘制环形进度条
- Postman Post请求上传文件
- IDEA加载项目的设置是tomcat
- PowerBI发布到网页
- eclipse 假死
- 关于在VS2008和VS2010中禁用及卸载Visual Assist X的方法研究——转载
- 《DSP using MATLAB》第2章习题Problem2.1
- fastJson API
- iOS 从0到1搭建高可用App框架
热门文章
- Python实现12306自动查票程序
- 修改XAMPP的默认根目录
- erlang开发工具之intellij idea基本使用
- idea 里自动下载私服jar一直不能下载下来
- 前端使用Mock服务Json-server
- 记一次idea启动tomcat后控制台乱码的坑
- ubuntu两个conda安装和切换
- urllib.error.URLError: <;urlopen error [WinError 10061] 由于目标计算机积极拒绝,无法连接。>;
- ccf-炉石传说-201609-3
- less--入门