Geeks 面试题之Fibonacci numbers优化为lgn效率

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:

返回栏目页:http://www.bianceng.cnhttp://www.bianceng.cn/Programming/sjjg/

通过这个矩阵相乘的方法,得到的时间复杂度依然是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];
    }
};

作者:csdn博客 靖心

以上是小编为您精心准备的的内容,在的博客、问答、公众号、人物、课程等栏目也有的相关内容,欢迎继续使用右上角搜索按钮进行搜索int
, fibonacci数列
, return
, matrix
, numbers
, fibonacci堆
, fibonacci
, The
Fib数列
fibonacci numbers、geeks3d furmark、geeks、freaks and geeks、geeks3d,以便于您获取更多的相关知识。

时间: 2024-11-10 00:37:10

Geeks 面试题之Fibonacci numbers优化为lgn效率的相关文章

Geeks 面试题之Ugly Numbers

Ugly Numbers Ugly numbers are numbers whose only prime factors are 2, 3 or 5. The sequence 1, 2, 3, 4, 5, 6, 8, 9, 10, 12, 15, - shows the first 11 ugly numbers. By convention, 1 is included. Write a program to find and print the 150′th ugly number.

hdu 3117 Fibonacci Numbers

点击此处即可传送到hdu 3117 **Fibonacci Numbers** Problem Description The Fibonacci sequence is the sequence of numbers such that every element is equal to the sum of the two previous elements, except for the first two elements f0 and f1 which are respectively

Geeks面试题: Closest Pair of Points及O(nlogn) Implementati

Closest Pair of Points | O(nlogn) Implementation We are given an array of n points in the plane, and the problem is to find out the closest pair of points in the array. This problem arises in a number of applications. For example, in air-traffic cont

Geeks面试题:Box Stacking Problem

Dynamic Programming | Set 22 (Box Stacking Problem) You are given a set of n types of rectangular 3-D boxes, where the i^th box has height h(i), width w(i) and depth d(i) (all real numbers). You want to create a stack of boxes which is as tall as pos

Geeks 面试题之Maximum size square sub-matrix with all 1s

Maximum size square sub-matrix with all 1s Given a binary matrix, find out the maximum size square sub-matrix with all 1s. For example, consider the below binary matrix. 0 1 1 0 1 1 1 0 1 0 0 1 1 1 0 1 1 1 1 0 1 1 1 1 1 0 0 0 0 0 The maximum square s

Geeks 面试题之Optimal Binary Search Tree

Dynamic Programming | Set 24 (Optimal Binary Search Tree) Given a sorted array keys[0.. n-1] of search keys and an array freq[0.. n-1] of frequency counts, where freq[i] is the number of searches to keys[i]. Construct a binary search tree of all keys

Geeks面试题:Closest Pair of Points

Divide and Conquer | Set 2 (Closest Pair of Points) We are given an array of n points in the plane, and the problem is to find out the closest pair of points in the array. This problem arises in a number of applications. For example, in air-traffic c

北京一家软件公司的面试题:将IP地址转化为32位无符号整型表示法,并考虑用网络字节顺序表示

问题描述 你会做了吗? 解决方案 解决方案二:么意思?解决方案三:很简单的usingSystem.Net;//------------------IPAddressip=IPAddress.Parse("192.168.1.1");byte[]arr=ip.GetAddressBytes();Array.Reverse(arr);uintipui=BitConverter.ToUInt32(arr,0);解决方案四:楼上周星星,UP,顺便接分解决方案五:引用2楼viena的回复: 很简

ANDROID性能调优

http://www.trinea.cn/android/android-performance-demo/#comment-115 本文主要分享自己在appstore项目中的性能调优点,包括同步改异步.缓存.Layout优化.数据库优化.算法优化.延迟执行等.   性能优化专题已完成五部分: 性能优化总纲--性能问题及性能调优方式性能优化第三篇--Java(Android)代码优化性能优化第二篇--布局优化性能优化第一篇--数据库性能优化 性能优化实例    一.性能瓶颈点 整个页面主要由6个