hit2060 Fibonacci Problem Again

26 篇文章 0 订阅

(http://acm.hit.edu.cn/hoj/problem/view?id=2060)

题意:求斐波那契数列某连续项的和模10^9, 学了矩阵连乘,就很好做了。

/*This Code is Submitted by 1229836201 for Problem 2060 at 2015-08-14 19:11:26*/
#include <iostream>
#include <stdio.h>
#include <math.h>
#define mod 1000000000
using namespace std;
typedef struct
{
    long long m[3][3];
}Matrix;
Matrix P={1,1,0,//公式推出来的
          0,1,1,
          0,1,0};
Matrix I={1,0,0,//单位阵
          0,1,0,
          0,0,1};
Matrix matrixmul(Matrix a,Matrix b)//矩阵乘法
{
    Matrix c;
    for(int i=0;i<3;i++)
       for(int j=0;j<3;j++)
       {
           c.m[i][j]=0;
           for(int k=0;k<3;k++)
           {
              c.m[i][j]+=(a.m[i][k]*b.m[k][j])%mod;
           }
           c.m[i][j]=c.m[i][j]%mod;
       }
       return c;
}
Matrix quickpow(long long n)//快速幂
{
    Matrix a,b;
    a=P;b=I;
    while(n)
    {
        if(n&1)
        b=matrixmul(b,a);
        n>>=1;
        a=matrixmul(a,a);
    }
    return b;
}
int main()
{
    int a,b,i,j,sum,sum1,ans;
    Matrix tmp,tmp1;
    while(scanf("%d%d",&a,&b)!=EOF&&(a||b))
    {
            tmp=quickpow(b);
            tmp1=quickpow(a-1);
            sum=tmp.m[0][0]+tmp.m[0][1]+tmp.m[0][2];
            sum1=tmp1.m[0][0]+tmp1.m[0][1]+tmp1.m[0][2];
            ans=(sum%mod-sum1%mod+mod)%mod;
            printf("%d\n",ans);

    }
    return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值