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;
}
目录
相关文章
LeetCode 216. Combination Sum III
找出所有相加之和为 n 的 k 个数的组合。组合中只允许含有 1 - 9 的正整数,并且每种组合中不存在重复的数字。
105 0
LeetCode 216. Combination Sum III
LeetCode 39. Combination Sum
给定一个无重复元素的数组 candidates 和一个目标数 target ,找出 candidates 中所有可以使数字和为 target 的组合。 candidates 中的数字可以无限制重复被选取。
71 0
LeetCode 39. Combination Sum
|
API
[LeetCode]--374. Guess Number Higher or Lower
We are playing the Guess Game. The game is as follows: I pick a number from 1 to n. You have to guess which number I picked. Every time you guess wrong, I’ll tell you whether the number i
1120 0
LeetCode 216 Combination Sum III(Backtracking)(*)
版权声明:转载请联系本人,感谢配合!本站地址:http://blog.csdn.net/nomasp https://blog.csdn.net/NoMasp/article/details/51935033 翻译 找出所有的k个数字相加得到数字n的组合,只有1到9的数字可以被使用,并且每个组合间需要是不同的数字集。
729 0
LeetCode - 40. Combination Sum II
40. Combination Sum II  Problem's Link  ---------------------------------------------------------------------------- Mean:  给你一个待选集合s和一个数n,选出所有相加之和为n的组合.
1006 0
LeetCode - 39. Combination Sum
39. Combination Sum  Problem's Link  ---------------------------------------------------------------------------- Mean:  给你一个待选集合s和一个数n,让你找出集合s中相加之和为n的所有组合.
823 0