首页 > 其他 > 详细

HIT 2060 Fibonacci Problem Again

时间:2015-08-14 13:44:25      阅读:235      评论:0      收藏:0      [点我收藏+]

点击此处即可传送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之间的斐波那契数的和

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

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

版权声明:本文为博主原创文章,未经博主允许不得转载。

HIT 2060 Fibonacci Problem Again

原文:http://blog.csdn.net/qingshui23/article/details/47660041

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!