The Fibonacci numbers are the numbers in the following integer sequence.

0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, 141, ……..

In mathematical terms, the sequence Fn of Fibonacci numbers is defined by the recurrence relation

with seed values

Write a function int fib(int n) that returns . For example, if n = 0, then fib() should return 0. If n = 1, then it should return 1. For n > 1, it should return 

Following are different methods to get the nth Fibonacci number.

http://www.geeksforgeeks.org/program-for-nth-fibonacci-number/

斐波那契数列大家都很熟悉了,递归和动态规划法这里就不讲了。

原来有一个更加优化的方法,时间效率为O(lgn)。

Method 4 ( Using power of the matrix {{1,1},{1,0}} )
This another O(n) which relies on the fact that if we n times multiply the matrix M = {{1,1},{1,0}} to itself (in other words calculate power(M, n )), then we get the (n+1)th Fibonacci number as the element at row and column (0, 0) in the resultant matrix.

The matrix representation gives the following closed expression for the Fibonacci numbers:

通过这个矩阵相乘的方法,得到的时间复杂度依然是O(n),但是可以进一步使用二分法,把复杂度降低到O(lgn)。

Method 5 ( Optimized Method 4 )
The method 4 can be optimized to work in O(Logn) time complexity. We can do recursive multiplication to get power(M, n) in the prevous method (Similar to the optimization done in this post)

下面是Method4 和5的程序:

void mulMatrix(int F[2][2])
{int a = F[0][0];int b = F[1][0];F[0][0] = a+b;F[0][1] = a;F[1][0] = a;F[1][1] = b;
}
void powMatrix(int F[2][2], int n)
{for (int i = 2; i < n; i++) mulMatrix(F);
}
int fib(int n)
{if (n == 0) return 0;int F[2][2] = {{1,1},{1,0}};powMatrix(F, n);return F[0][0];
}class OptiFib
{
public:void mulOneMatrix(int F[2][2]){int a = F[0][0];int b = F[1][0];F[0][0] = a+b;F[0][1] = a;F[1][0] = a;F[1][1] = b;}void pow2Matrix(int F1[2][2]){int a = F1[0][0];int b = F1[0][1];int c = F1[1][0];int d = F1[1][1];F1[0][0] = a*a+b*c;F1[0][1] = a*b+b*d;F1[1][0] = c*a+c*d;F1[1][1] = c*b+d*d;}void powMatrix(int F[2][2], int n){if (n < 2) return;int mid = n>>1;powMatrix(F, mid);pow2Matrix(F);if (n%2 == 1) mulOneMatrix(F);}int fib(int n){if (n == 0) return 0;//注意:不要遗漏这个特例!int F[2][2] = {{1,1},{1,0}};powMatrix(F, n-1);return F[0][0];}
};

Geeks 面试题:Fibonacci numbers 优化为lgn效率相关推荐

  1. 求解斐波那契数列(Fibonacci Numbers)算法居然有9种,你知道哪几种吗?

    By LongLuo 斐波那契数列(Fibonacci sequence),又称黄金分割数列,因数学家莱昂纳多·斐波那契(Leonardoda Fibonacci)以兔子繁殖为例子而引入,故又称为&q ...

  2. Codeforces Round #FF 446 C. DZY Loves Fibonacci Numbers

    參考:http://www.cnblogs.com/chanme/p/3843859.html 然后我看到在别人的AC的方法里还有这么一种神方法,他预先设定了一个阈值K,当当前的更新操作数j<K ...

  3. Codeforces 446C —— DZY Loves Fibonacci Numbers(线段树)

    题目:DZY Loves Fibonacci Numbers 题意比較简单,不解释了. 尽管官方的题解也是用线段树,但还利用了二次剩余. 可是我没有想到二次剩余,然后写了个感觉非常复杂度的线段树,还是 ...

  4. 【Android 热修复】热修复原理 ( 修复包 Dex 文件准备 | Dex 优化为 Odex | Dex 文件拷贝 | 源码资源 )

    文章目录 一.修复包 Dex 文件准备 二.Odex 优化 三.Dex 文件拷贝 四. 源码资源 一.修复包 Dex 文件准备 异常代码 : 故意写一个异常代码 , 并执行该代码 , 肯定会崩溃 ; ...

  5. 大数的菲波那契计算/Huge Fibonacci Numbers - ACM

    Huge Fibonacci Numbers 时间限制: 1 Sec   内存限制: 128 MB 题目描述 A Fibonacci sequence is calculated by adding ...

  6. [CF446C]DZY Loves Fibonacci Numbers

    Description: 给出一个数列,每次可以选取一个区间,按顺序加上第i个Fibonacci Numbers(斐波那契数)进行更新,也可以查询某一个区间的总和. Hint: \(n \le 3*1 ...

  7. Codeforces 446C. DZY Loves Fibonacci Numbers【斐波那契+线段树】

    C. DZY Loves Fibonacci Numbers [题目描述] 传送门 [题解] 我们可以知道斐波那契数列有两个性质: ∑i=1nFi=Fn+2−F2\sum_{i=1}^{n} F_i= ...

  8. GCC为什么不将a * a * a * a * a * a优化为(a * a * a)*(a * a * a)?

    我正在对科学应用程序进行一些数值优化. 我注意到的一件事是,GCC将通过将其编译为a*a来优化调用pow(a,2) ,但是调用pow(a,6)并未经过优化,实际上会调用库函数pow ,这大大降低了速度 ...

  9. UVA10579 Fibonacci Numbers【大数】

    A Fibonacci sequence is calculated by adding the previous two members of the sequence, with the firs ...

  10. UVA11582 Colossal Fibonacci Numbers!【快速模幂+数列模除】

    The i'th Fibonacci number f(i) is recursively defined in the following way: • f(0) = 0 and f(1) = 1 ...

最新文章

  1. 对于sync.Mutex使用注意事项
  2. 移动端适配之二:visual viewport、layout viewport和ideal viewport介绍
  3. 2022年了我才发现Chrome原来可以直接生成二维码...
  4. python 粒子滤波目标追踪_Python实现基于相关滤波的单目标跟踪算法
  5. Python介绍与特点(自学python知识整理)
  6. jsp 修改的员工select怎么_jsp修改的时候下拉框怎么取修改数据的值?
  7. 机器学习入门——详解主成分分析
  8. 【交易技术前沿】低时延基础设施杂谈
  9. 无人机群编队分析的定位问题 分析与思考-1(数学建模竞赛2022年B题)
  10. Unity基础知识之协程
  11. SQL获取数据表最新一条记录
  12. 基于Tablet pc 的墨迹手写识别输入法
  13. python安装教程
  14. php图片64位处理,php实现图片以base64显示的方法
  15. 应用后台被回收,点击崩溃
  16. 【JVAV】—继承、多态、抽象类
  17. Dynamics AX 2009 Trainning
  18. Mac远程Win桌面官方工具——Microsoft Remote Desktop for mac
  19. [大数据文章之其一] 大数据对你来说意味着什么?
  20. ffmpeg 命令的使用

热门文章

  1. Android的一个登陆注册页面
  2. 阿里iconfont矢量图库使用
  3. 蓝阔无线打印服务器怎么连接5g,TP-Link TL-WDR7500路由器5G无线WDS桥接设置
  4. 异数OS 星星之火(三)--异数OS-织梦师云 微服务编写入门
  5. 修改html倒计时,利用自定义HTML小工具实现倒计时
  6. input框输入的文本类型都是字符串类型
  7. 什么是数据可视化大屏?如何制作一个数据可视化大屏?
  8. 微信小程序之文本内的p标签去除
  9. greenDAO简单使用经验
  10. python 类 对象 知乎_Python 基础入门(八)类与对象 学习小结