HIT 2060 Fibonacci Problem Again

点击此处即可传送HIT 2060

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

F(n) == {1   n==0||n==1
        {F(n-1)+F(n-2)  n>1;
Given two numbers a and b , calculate . 从a到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 mod 1,000,000,000, since S may be quite large. 

Sample Input 
1 1
3 5
10 1000
0 0

Sample Output 1
16
496035733

题目大意:就是给你一个a和b, 求从a到b之间的斐波那契数的和

解题思路:矩阵乘法,注意考虑边界条件:
具体详见代码:
上代码:

/*
2015 - 8 - 14 上午
Author: ITAK
今日的我要超越昨日的我,明日的我要胜过今日的我,
以创作出更好的代码为目标,不断地超越自己。
*/
#include <iostream>
#include <cstdio>

using namespace std;
const int maxn = 3;
typedef long long LL;
const int mod = 1e9;
typedef struct
{
    LL m[maxn][maxn];
} Matrix;

Matrix P = {1,1,0,
            1,0,0,
            1,1,1,
           };
Matrix I = {1,0,0,
            0,1,0,
            0,0,1,
           };
Matrix matrix_mul(Matrix a, Matrix b)
{
    int i, j, k;
    Matrix c;
    for(i=0; i<maxn; i++)
    {
        for(j=0; j<maxn; j++)
        {
            c.m[i][j] = 0;
            for(k=0; k<maxn; k++)
                c.m[i][j] += (a.m[i][k] * b.m[k][j]) % mod;
            c.m[i][j] %= mod;
        }
    }
    return c;
}

Matrix quick_mod(LL m)
{
    Matrix ans = I, b = P;
    while(m)
    {
        if(m & 1)
            ans = matrix_mul(ans, b);
        m >>= 1;
        b = matrix_mul(b, b);
    }
    return ans;
}
int main()
{
    LL a, b;
    while(~scanf("%lld%lld",&a,&b))
    {
        Matrix tmp1, tmp2;
        if(!a && !b)
            break;
        if(a == 0)
        {
            if(b == 1)
                puts("2");
            else
            {
                tmp2 = quick_mod(b-1);
                LL ans2 = tmp2.m[2][0]+tmp2.m[2][1]+2*tmp2.m[2][2];
                LL ans = (ans2%mod-1)%mod;
                if(ans < 0)
                    ans += mod;
                printf("%lld\n",ans+1);
            }
        }
        else if(a == 1)
        {
            if(b == 1)
                puts("1");
            else
            {
                tmp2 = quick_mod(b-1);
                LL ans2 = tmp2.m[2][0]+tmp2.m[2][1]+2*tmp2.m[2][2];
                LL ans = (ans2%mod-1)%mod;
                if(ans < 0)
                    ans += mod;
                printf("%lld\n",ans);
            }
        }
        else
        {
            tmp1 = quick_mod(a-2);
            tmp2 = quick_mod(b-1);
            LL ans1 = tmp1.m[2][0]+tmp1.m[2][1]+2*tmp1.m[2][2];
            LL ans2 = tmp2.m[2][0]+tmp2.m[2][1]+2*tmp2.m[2][2];
            //cout<<ans1 << " "<<ans2<<" ";
            LL ans = (ans2%mod - ans1%mod);
            if(ans < 0)
                ans += mod;
            printf("%lld\n",ans);
        }
    }
    return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值