斐波那契与快速幂(一)

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

.

.

在利用这个结论的时候,要注意到f[0]。如果不是0就要注意最后结果f[n]的位置

一般情况下用到斐波那契之和的时候几乎都用到了这个矩阵快速幂。关于快速幂,先看一段代码:

#include<stdio.h>

__int64 qpow(int a,int b)//求a^b

{

         __int64 c,d;

         c=1;

         d=a;

         while(b>0)

         {

                   if(b%2==1)

                            c=c*d;

                   d=d*d;

                   b=b>>1;//或b=b/2

         }

         return c;

}

int main()

{

         int a,b;

        

         while(scanf("%d%d",&a,&b)!=EOF)

         {

                   printf("%I64d\n",qpow(a,b));

         }

         return 0;

}

根据上面的一段求a^b的理解就可以运用矩阵快速幂求斐波那契了,我的一段代码:

poj 3070

#include<stdio.h>

/*求斐波那契第n项模p。。*/

__int64 temp[3][3],a[3][3],c[3][3];

__int64 n;

int p;

void Cal()

{

         int i,j,k;

         __int64 b;

         b=n;

         temp[0][0]=1,temp[0][1]=0,temp[1][0]=0,temp[1][1]=1;//temp[][]刚开始是一个单位矩阵

    a[0][0]=1,a[0][1]=1,a[1][0]=1,a[1][1]=0;

         c[0][0]=0,c[0][1]=0,c[1][0]=0,c[1][1]=0;

         while(b>0)

         {

                   if(b%2==1)

                   {       

                            c[0][0]=0,c[0][1]=0,c[1][0]=0,c[1][1]=0;

                            for(i=0;i<2;i++)//矩阵乘法

                                     for(j=0;j<2;j++)

                                     {

                                               for(k=0;k<2;k++)

                                                        c[i][j]=c[i][j]+temp[i][k]*a[k][j];

                                               c[i][j]=c[i][j]%p;

                                     }

                            for(i=0;i<2;i++)

                                     for(j=0;j<2;j++)

                                               temp[i][j]=c[i][j];

                   }

                  

                   c[0][0]=0,c[0][1]=0,c[1][0]=0,c[1][1]=0;

                   for(i=0;i<2;i++)//矩阵乘法

                   for(j=0;j<2;j++)

                   {

                            for(k=0;k<2;k++)

                                     c[i][j]=c[i][j]+a[i][k]*a[k][j];

                            c[i][j]=c[i][j]%p;

                   }

                   for(i=0;i<2;i++)

                            for(j=0;j<2;j++)

                                     a[i][j]=c[i][j];

         b=b/2;

         }

}

int main()

{

         p=10000;//看情况p是多少

         while(1)

         {

                   scanf("%I64d",&n);

                   if(n==-1) break;

                   Cal();

                   printf("%I64d\n",temp[0][1]);

         }

         return 0;

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值