矩阵连乘与finacci

1. 求解Fibonacci的某一项(这个范围一般在45之内)

这类题目long long 就可以了!

2. 求解Fibonacci的某一项模K(这个一般是大数),通用解决方法是构造矩阵求幂次

  这得使用矩阵连乘可以解决。见上面我推导的公式。要会使用模板。

3. 求解Fibonacci的前多少位 (这个一般是大数), 通用解法是使用通项公式

   下面举例来说明计算前4

123456.32=1234.56*10^2

s=d.xxx*10^(len-4)

log10(s)=log10(d.xxxxx)+log10(10^(len-4))=log10(d.xxxx)+len-4;

log10(s)+4-len=log10(d.xxxx)

d.xxxx=10^(log10(s)+4-len)

 

s=(1/sqrt(5))*[(1+sqrt(5))/2.0]^i;

len=(int)log10(s)+1;

d.xxxx=10^(log10(s)+4-((int)log10(s)+1))=10^(log10(s)-(int)log10(s)+3);

例如:HDU3117题。

4. 求解Fibonacci的后多少位,这个和取模类似

  和2相同

5. 求解Fibonacci的前n项和,利用推导式, S(n) = F(n + 2) - F(2)即可

   最好不要记住公式,要自己会推导。本题用分块矩阵可以实现,还可以构造矩阵来实现,我会专门说一下构造矩阵。

6. 更多的是基于Fibonacci的综合题,包括DP,构造,等等.

   这样的题目较多,亚洲赛,多校联合等都会看到这样的题目。

 

接着说一下知识点:a^b%mod , A^N%mod, 2分查找,大数加法运算,字典数等知识,矩阵连乘算是DP的一种。 

 

下面是 m^n  % k 的快速幂:

// m^n % k
int quickpow_n(int m,int n,int k)
{
    int b = 1;
    while (n > 0)
    {
          if (n & 1)
             b = (b*m)%k;
          n = n >> 1 ;
          m = (m*m)%k;
    }
    return b;



下面是矩阵快速幂:


|| 快速幂(quickpow)模板 
|| P 为等比,I 为单位矩阵
|| MAX 初始化!!!!
||


#include 
const int MAX = 3;

typedef  struct{
        int  m[MAX][MAX];
}  Matrix;

Matrix P = {5,-7,4,
            1,0,0,
            0,1,0,
           };

Matrix I = {1,0,0,
            0,1,0,
            0,0,1,
           };
           
Matrix matrixmul(Matrix a,Matrix b) //矩阵乘法
{
       int i,j,k;
       Matrix c;
       for (i = 0 ; i < MAX; i++)
           for (j = 0; j < MAX;j++)
             {
                 c.m[i][j] = 0;
                 for (k = 0; k < MAX; k++)
                     c.m[i][j] += (a.m[i][k] * b.m[k][j])�97;
                 c.m[i][j] %= 9997;
             }
       return c;
}
          
Matrix quickpow(long long n)
{
       Matrix m = P, b = I;
       while (n >= 1)
       {
             if (n & 1)
                b = matrixmul(b,m);
             n = n >> 1;
             m = matrixmul(m,m);
       }
       return b;
}
使用:在main()里直接调用quickpow()就可以了。

 

 

 

下面是网上的一些帖子,写得还行,就是有些解题的方法和我的思路不一样,但效果相同。可以照着做做,但下面的浙大OJ题目我没都做,AC不了的题可以互相讨论。

 

hdu_1021:

Problem Description

There are another kind of Fibonacci numbers: F(0) = 7, F(1) = 11, F(n) = F(n-1) + F(n-2) (n>=2).

Input

Input consists of a sequence of lines, each containing an integer n. (n < 1,000,000).

 Output

Print the word "yes" if 3 divide evenly into F(n).
Print the word "no" if not.

解题报告:  观察法  找出循环节=8   然后%8=2或 %8=6   YES

本题也可以用矩阵乘法。

 

Hdu_1250:

Problem Description

A Fibonacci sequence is calculated by adding the previous two members the sequence, with the first two members being both 1.
F(1) = 1, F(2) = 1, F(3) = 1,F(4) = 1, F(n>4) = F(n - 1) + F(n-2) + F(n-3) + F(n-4)
Your task is to take a number as input, and print that Fibonacci number.

解题报告: 没办法,纯大数运算

 

Hdu-1568:

Problem Description

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

Input

输入若干数字n(0 <= n <= 100000000),每个数字一行。读到文件尾。

Output

输出f[n]的前4个数字(若不足4个数字,就全部输出)。

解题报告:

因为是前4位,所以可以用封闭公式:   1/sqrt(5).((1+sqrt(5))/2.0)^n  ,再设f(n)=d.xxxxx*10^x,取对数,就可以了。

 

Hdu-3117:

For each test case, a line will contain an integer i between 0 and 108 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).

解题报告:

下面举例来说明计算前4

123456.32=1234.56*10^2

 

s=d.xxx*10^(len-4)

log10(s)=log10(d.xxxxx)+log10(10^(len-4))=log10(d.xxxx)+len-4;

log10(s)+4-len=log10(d.xxxx)

d.xxxx=10^(log10(s)+4-len)

s=(1/sqrt(5))*[(1+sqrt(5))/2.0]^i;

len=(int)log10(s)+1;

d.xxxx=10^(log10(s)+4-((int)log10(s)+1))=10^(log10(s)-(int)log10(s)+3);

-----------------------------------------------------------------------------

计算后4位   矩阵乘法幂取模,注意后4位时 ,例如 0123   输出要占4位,而不是123

-------------------------------------------------------------------------

先把前8位的fibonacci的值打表打出来;8位以后的f(i)的值用上面方法

 

Hit_2060:

描述

As we know , the Fibonacci numbers are defined as follows: 

F(0)=1,f(1)=1;f(n)=f(n-1)+f(n-2) n>=2;

Given two numbers a and b , calculate S(n)=f(a)+f(a+1)+….+f(b)

 

Input 

The input contains several test cases. Each test case consists of two non-negative integer numbers a and b (0 ≤ a ≤ b ≤1,000,000,000). Input is terminated by a = b = 0. 

 

Output 

For each test case, output S(n) mod 1,000,000,000, since S(n) may be quite large.

解题报告:

因此题目就转换为了求 F(b + 2) - F(a + 2 - 1)
而又是大数取模,考虑使用矩阵求幂,(A^n)中的(a[0][0]+a[0][1])f(n)的值 

矩阵求幂注意要用long long,因为本题是对1000000000,一不小心就会溢出。

还要考虑结果是负数,要+1000000000.

-----------------------------------------------------------------------------------------------------

构造矩阵:通过HDU3306来讲

 

Another kind of Fibonacci

Time Limit: 3000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)
Total Submission(s): 802    Accepted Submission(s): 326

Problem Description

As we all known , the Fibonacci series : F(0) = 1, F(1) = 1, F(N) = F(N - 1) + F(N - 2) (N >= 2).Now we define another kind of Fibonacci : A(0) = 1 , A(1) = 1 , A(N) = X * A(N - 1) + Y * A(N - 2) (N >= 2).And we want to Calculate S(N) , S(N) = A(0)2 +A(1)2+……+A(n)2.

 

 

Input

There are several test cases.
Each test case will contain three integers , N, X , Y .
N : 2<= N <= 231 – 1
X : 2<= X <= 231– 1
Y : 2<= Y <= 231 – 1

 

 

Output

For each test case , output the answer of S(n).If the answer is too big , divide it by 10007 and give me the reminder.

 

 

Sample Input

2 1 1 

3 2 3 

 

 

Sample Output

6

196

 

 

 

下为解题报告:林大

 

该题为矩阵连乘问题,主要考虑以下公式:

 

f(n)=x*f(n-1)+y*f(n-2)

 

 

先看 s(n)=s(n-1)+f(n)^2;

右边出现了n,不可以的,只能出现n-1  n-2

所以:s(n-1)=s(n-2)+f(n-1)^2;  右边有2项,s(n-2)f(n-1)^2必须要有了

  S(n-2)-----------às(n-1)

   F(n-1)^2------à f(n)^2=[xf(n-1)+yf(n-2)]^2=出现了f(n-2)^22xyf(n-1)f(n-2),所以共4

 

 

 

posted @ 2012-09-20 11:21  兴安黑熊  阅读(457)  评论(0编辑  收藏  举报