(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;
}