@description@

Fib数列为1,1,2,3,5,8...

求在Mod10^9+9的意义下,数字N在Fib数列中出现在哪个位置

无解输出-1

原题传送门。

@solution@

一个熟练的 OIer 选手应该能迅速发现 5 在模 10^9 + 9 意义下有二次剩余。考虑斐波那契通项公式:

\[f_i = \frac{1}{\sqrt{5}}((\frac{1 + \sqrt{5}}{2})^n - (\frac{1 - \sqrt{5}}{2})^n) = N
\]

不妨记 \(a = \frac{1 + \sqrt{5}}{2}, b = \frac{1 - \sqrt{5}}{2}\),则我们要解方程 \(a^n - b^n = \sqrt{5}N = K\)。

这个方程可解吗?注意 a, b 存在关系:\(a + b = 1, ab = -1\)。前一个显然不好用,我们用后一个:

\[a^n - (\frac{-1}{a})^n = K\\
a^{2n} - Ka^{n} - (-1)^n = 0\\
a^n = \frac{K \pm \sqrt{K^2 + 4\times(-1)^n}}{2}
\]

讨论一下 n 的奇偶可以解出 \(a^n\),然后 BSGS 即可。平方根也可 BSGS + 原根来解。

@accepted code@

#include <cstdio>
#include <vector>
#include <cassert>
#include <iostream>
#include <algorithm>
using namespace std; const int MOD = int(1E9) + 9;
const int INV2 = (MOD + 1) / 2;
const int INV5 = (MOD + 1) / 5;
const int SQ5 = 383008016;
const int A = 1LL*(MOD + 1 + SQ5)*INV2%MOD;
const int B = 1LL*(MOD + 1 - SQ5)*INV2%MOD;
const int C = 1LL*SQ5*INV5%MOD;
const int G = 13;
const int BLOCK = 32000;
const int HASHSIZE = 1000037;
const int MA = 133086171;
const int INVMA = 74832817;
const int GCD = 3; inline int add(int x, int y) {return (x + y >= MOD ? x + y - MOD : x + y);}
inline int sub(int x, int y) {return (x - y < 0 ? x - y + MOD : x - y);}
inline int mul(int x, int y) {return 1LL * x * y % MOD;} int pow_mod(int b, int p) {
int ret = 1;
for(int i=p;i;i>>=1,b=mul(b,b))
if( i & 1 ) ret = mul(ret, b);
return ret;
}
int fib(int n) {
int x = sub(pow_mod(A, n), pow_mod(B, n));
return mul(C, x);
} vector<pair<int, int> >h[HASHSIZE];
int hash_search(int x) {
int y = x % HASHSIZE;
for(int i=0;i<h[y].size();i++)
if( h[y][i].first == x )
return h[y][i].second;
return -1;
}
void hash_insert(int x, int k) {
int y = x % HASHSIZE;
for(int i=0;i<h[y].size();i++)
if( h[y][i].first == x )
return ;
h[y].push_back(make_pair(x, k));
}
void init() {
int p = pow_mod(G, BLOCK);
for(int i=1,q=p;i<=BLOCK;i++,q=mul(q,p))
hash_insert(q, i*BLOCK);
}
int bsgs(int x) {
if( x == 0 ) return -1;
int ret = MOD;
for(int i=1,q=G;i<=BLOCK;i++,q=mul(q,G)) {
int p = hash_search(mul(x, q));
if( p != -1 ) ret = min(ret, p - i);
}
assert(pow_mod(G, ret) == x);
return ret;
}
int msqrt(int x) {
if( x == 0 ) return 0;
int p = bsgs(x);
return p % 2 ? -1 : pow_mod(G, p / 2);
} int ans = -1;
void update(int x, int r) {
x = 1LL*(x / GCD)*INVMA%((MOD - 1) / GCD);
if( x % 2 == r )
if( ans == -1 || ans > x )
ans = x;
}
int main() {
int N, K; scanf("%d", &N), K = mul(N, SQ5), init(); int P = msqrt(add(mul(K, K), 4)); // even
if( P != -1 ) {
int k = bsgs(mul(add(K, P), INV2));
if( k != -1 && k % GCD == 0 ) update(k, 0);
k = bsgs(mul(sub(K, P), INV2));
if( k != -1 && k % GCD == 0 ) update(k, 0);
} P = msqrt(sub(mul(K, K), 4)); // odd
if( P != -1 ) {
int k = bsgs(mul(add(K, P), INV2));
if( k != -1 && k % GCD == 0 ) update(k, 1);
k = bsgs(mul(sub(K, P), INV2));
if( k != -1 && k % GCD == 0 ) update(k, 1);
} if( ans != -1 ) assert(fib(ans) == N);
printf("%d\n", ans);
}

@details@

因为几乎都是常数,可以预处理出来直接用。

话说这道题的主要难点是解方程那一块吧,感觉数学味儿多一点。

@bzoj - 5104@ Fib数列的更多相关文章

  1. BZOJ 5104 Fib数列(二次剩余+BSGS)

    斐波那契数列的通项: \[\frac{1}{\sqrt{5}}((\frac{1+\sqrt{5}}{2})-(\frac{1-\sqrt{5}}{2}))\] 设T=\(\sqrt{5}*N\),\ ...

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

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

  3. FIB数列

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

  4. bzoj5104: Fib数列

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

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

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

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

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

  7. 【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,表示数据 ...

  8. HDU3977 Evil teacher 求fib数列模p的最小循环节

    In the math class, the evil teacher gave you one unprecedented problem! Here f(n) is the n-th fibona ...

  9. 1022. Fib数列

    https://acm.sjtu.edu.cn/OnlineJudge/problem/1022 Description 定义Fib数列:1,1,2,3,5,8,13,…1,1,2,3,5,8,13, ...

随机推荐

  1. MySQL(9)— 规范数据库设计

    九.规范数据库设计 9-1.为什么要设计? 当数据库比较复杂时,我们就需要设计了! 糟糕的数据库设计: 数据冗余,浪费大量存储空间 使用物理外键,大量的增删改操作麻烦,异常 查询效率低下 良好的数据库 ...

  2. GitHub 热点速览 Vol.21:Go 新手起手式,学就完事儿了

    作者:HelloGitHub-小鱼干 摘要:说到学习之道,方法很重要,好的学习方法能让你比他人更快地入门到精通,比如本周被 3k 多人 pick 的 learngo 项目,它收录了多个例子和练习,新手 ...

  3. python - 怎样使用 requests 模块发送http请求

    最近在学python自动化,怎样用python发起一个http请求呢? 通过了解 request 模块可以帮助我们发起http请求 步骤: 1.首先import 下 request 模块 2.然后看请 ...

  4. JVM调优总结(三)-垃圾回收面临的问题

    如何区分垃圾 上面说到的“引用计数”法,通过统计控制生成对象和删除对象时的引用数来判断.垃圾回收程序收集计数为0的对象即可.但是这种方法无法解决循环引用.所以,后来实现的垃圾判断算法中,都是从程序运行 ...

  5. PAT 1033 To Fill or Not to Fill (25分) 贪心思想

    题目 With highways available, driving a car from Hangzhou to any other city is easy. But since the tan ...

  6. 面试官,别问我DNS了,也就这些!

    提到网络,基本上都能把DNS给扯上去.为啥呢,今天我们来一探究竟. 1 Chrome浏览器原理 还记得面试过程中被问了千百遍的"输入URL后发生了什么"这个经典问题吗,因为这个问题 ...

  7. Beta冲刺——5.24

    这个作业属于哪个课程 软件工程 这个作业要求在哪里 Beta冲刺 这个作业的目标 Beta冲刺 作业正文 正文 github链接 项目地址 其他参考文献 无 一.会议内容 1.安排每个人进行为期3天的 ...

  8. Rocket - debug - TLDebugModuleInner - innerCtrl

    https://mp.weixin.qq.com/s/7UY99gEJ8QpVBJIohdqKhA 简单介绍TLDebugModuleInner中innerCtrl相关的寄存器. 1. innerCt ...

  9. Sublime Text3 注册码(Windows/Build 3176版本)| 开发工具

    转自:dushusir.com 1.修改hosts文件(路径:C:\Windows\System32\drivers\etc): 0.0.0.0 www.sublimetext.com 0.0.0.0 ...

  10. Java实现 LeetCode 481 神奇字符串

    481. 神奇字符串 神奇的字符串 S 只包含 '1' 和 '2',并遵守以下规则: 字符串 S 是神奇的,因为串联字符 '1' 和 '2' 的连续出现次数会生成字符串 S 本身. 字符串 S 的前几 ...