fibonacci封闭公式及矩阵连乘

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 zero and one. 

What is the numerical value of the nth Fibonacci number?

Input

For each test case, a line will contain an integer i between 0 and 10 8 inclusively, for which you must compute the ith Fibonacci number fi. Fibonacci numbers get large pretty quickly, so whenever the answer has more than 8 digits, output only the first and last 4 digits of the answer, separating the two parts with an ellipsis (“...”).

There is no special way to denote the end of the of the input, simply stop when the standard input terminates (after the EOF).

Sample Input

0
1
2
3
4
5
35
36
37
38
39
40
64
65

Sample Output

0
1
1
2
3
5
9227465
14930352
24157817
39088169
63245986
1023...4155
1061...7723
1716...7565

#include <iostream>
#include <cstdio>
#include <cmath>
using namespace std;

typedef struct
{
  long long m[2][2];
}mat;

mat p={0,1,1,1},I={1,0,0,1};

mat calc(mat a,mat b)
{
    int i,j,k;
    mat c;
    for(i=0;i<2;i++)
    for(j=0;j<2;j++)
    {
        c.m[i][j]=0;
       for(k=0;k<2;k++)
      {
        c.m[i][j]+=a.m[i][k]*b.m[k][j]%10000;
      }
      c.m[i][j]%=10000;
    }
    return c;
}

mat matirx(int n)
{
    mat m=p,b=I;
    while(n>=1)
    {
        if(n&1) b=calc(b,m);
        n>>=1;
        m=calc(m,m);
    }
    return b;
}

int main()
{
    int i,a[42],len;
    int n;
    double s,d;
    a[0]=0;
    a[1]=1;
    for(i=2; i<40; i++)
        a[i]=a[i-1]+a[i-2];
    while(scanf("%d",&n)!=EOF)
    {
        if(n<40)
            printf("%d\n",a[n]);
        else
        {
            s=log10(1.0/sqrt(5))+n*log10((1+sqrt(5))/2.0);//fibonacci封闭公式求前四位
            len=(int)s;
            d=s+3-len;
            printf("%d...",(int)pow(10,d));

            mat tmp;
            tmp=matirx(n);                                //矩阵连乘求后四位
            printf("%04d\n",tmp.m[0][1]);
        }
    }
    return 0;
}
时间: 2024-09-30 19:19:55

fibonacci封闭公式及矩阵连乘的相关文章

fibonacci封闭公式

Description 2007年到来了.经过2006年一年的修炼,数学神童zouyu终于把0到100000000的Fibonacci数列 (f[0]=0,f[1]=1;f[i] = f[i-1]+f[i-2](i>=2))的值全部给背了下来. 接下来,CodeStar决定要考考他,于是每问他一个数字,他就要把答案说出来,不过有的数字太长了.所以规定超过4位的只要说出前4位就可以了,可是CodeStar自己又记不住.于是他决定编写一个程序来测验zouyu说的是否正确. Input 输入若干数字n

fibonacci卷积公式的使用

Fibonacci数列的定义如下: f(n) = f(n - 1) + f(n - 2) (n >= 3) f(1) = 1, f(2) = 2 f(0)可定义为1. 用归纳法可以证明性质: f(n + m) = f(m - 1)f(n + 1) + f(m - 2)f(n)  (m>= 2) 利用这条性质,我们可以将比较大的n的Fibonacci数转化成比较小的Fibonacci数,从而使计算起来更为方便. 这里有一个问题: Fibonacii数列 Fn (mod k) 的循环节长度是多少?

fibonacci数列(二)_矩阵快速幂

描述 In the Fibonacci integer sequence, F0 = 0, F1 = 1, and Fn = Fn − 1 + Fn − 2 for n ≥ 2. For example, the first ten terms of the Fibonacci sequence are: 0, 1, 1, 2, 3, 5, 8, 13, 21, 34, … An alternative formula for the Fibonacci sequence is . Given

Blocks 推出矩阵公式。矩阵快速密

Blocks 设涂到第I块时,颜色A,B都为偶数的数量为ai,一奇一偶的数量为bi,都为奇数为ci,  那么涂到第i+1快时有 a[i+1]=2*a[i]+b[i]+0*c[i]; b[i+1]=2*a[i]+2*b[i]+2*c[i]; C[i+1]=0*a[i]+b[i]+2*c[i]; 1 #include <iostream> 2 #include <cstdio> 3 #include <cstring> 4 #include <cmath> 5

2017多校第10场 HDU 6172 Array Challenge 猜公式,矩阵幂

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=6172 题意:如题. 解法: #include <bits/stdc++.h> using namespace std; typedef long long LL; const LL mod = 1e9+7; struct Matrix{ LL a[3][3]; void set1(){ memset(a, 0, sizeof(a)); } void set2(){ memset(a, 0, siz

[再寄小读者之数学篇](2014-04-22 平方差公式在矩阵中的表达)

设 A,B 都是 n 阶复方阵, 且 A^2+B^2=2AB . 证明: (1)  AB-BA 不可逆; (2)  如果 \rank(A-B)=1 , 那么 AB=BA .

组合数求和公式以及矩阵快速幂的公式

原文地址:https://www.cnblogs.com/zxz666/p/10548379.html

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

求Fibonacci数的三种方法和时间复杂度解析

题目: 定义Fibonacci数列如下: f(0)=1 f(1)=1 f(n)=f(n-1)+f(n-2), n>=2 输入n,用最快的方法求该数列的第n项. 解答一: 直接用公式写递归函数.很简单,很低效,就不写了.时间复杂度T(N) = T(N-1) + T(N-2); 也是f(n)本身,2^(n/2)<f(n)<2^n. 解答二: 用循环求,也很直接,效率很高了,时间复杂度是O(n). int f(int n) { if(n <= 1) return 1; int f0=1,