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;
}
目录
相关文章
|
API
LeetCode 375. Guess Number Higher or Lower II
我们正在玩一个猜数游戏,游戏规则如下: 我从 1 到 n 之间选择一个数字,你来猜我选了哪个数字。 每次你猜错了,我都会告诉你,我选的数字比你的大了或者小了。 然而,当你猜了数字 x 并且猜错了的时候,你需要支付金额为 x 的现金。直到你猜到我选的数字,你才算赢得了这个游戏。
78 0
LeetCode 375. Guess Number Higher or Lower II
|
API
LeetCode 374. Guess Number Higher or Lower
我们正在玩一个猜数字游戏。 游戏规则如下: 我从 1 到 n 选择一个数字。 你需要猜我选择了哪个数字。 每次你猜错了,我会告诉你这个数字是大了还是小了。
54 0
LeetCode 374. Guess Number Higher or Lower
LeetCode 216. Combination Sum III
找出所有相加之和为 n 的 k 个数的组合。组合中只允许含有 1 - 9 的正整数,并且每种组合中不存在重复的数字。
79 0
LeetCode 216. Combination Sum III
LeetCode 39. Combination Sum
给定一个无重复元素的数组 candidates 和一个目标数 target ,找出 candidates 中所有可以使数字和为 target 的组合。 candidates 中的数字可以无限制重复被选取。
55 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
1097 0
LeetCode 216 Combination Sum III(Backtracking)(*)
版权声明:转载请联系本人,感谢配合!本站地址:http://blog.csdn.net/nomasp https://blog.csdn.net/NoMasp/article/details/51935033 翻译 找出所有的k个数字相加得到数字n的组合,只有1到9的数字可以被使用,并且每个组合间需要是不同的数字集。
714 0
LeetCode - 39. Combination Sum
39. Combination Sum  Problem's Link  ---------------------------------------------------------------------------- Mean:  给你一个待选集合s和一个数n,让你找出集合s中相加之和为n的所有组合.
806 0
LeetCode - 40. Combination Sum II
40. Combination Sum II  Problem's Link  ---------------------------------------------------------------------------- Mean:  给你一个待选集合s和一个数n,选出所有相加之和为n的组合.
970 0