sicily 1001. Fibonacci 2

                              1001. Fibonacci 2

Description

In the Fibonacci integer sequence, F0 = 0, F1 = 1, and Fn = Fn-1 + Fn-2 for n ≥ 2. For example, the first ten terms of the Fibonacci sequence are:

0, 1, 1, 2, 3, 5, 8, 13, 21, 34, …

Given an integer n, your goal is to compute the last Fn mod (10^9 + 7).

Input

The input test file will contain a single line containing n (n ≤ 2^31-1).

There are multiple test cases!

Output

For each test case, print the Fn mod (10^9 + 7).

Sample Input

 Copy sample input to clipboard

9

Sample Output

34

用矩阵快速幂的方法,具体可见: http://blog.csdn.net/ACdreamers/article/details/25616461
#include <iostream>

using namespace std;

#define M 1000000007

struct Matrix{
    long long v[2][2];
};

Matrix matrixMul(Matrix a, Matrix b) {
    Matrix temp;
    for (int i = 0; i != 2; i++) {
        for (int j = 0; j != 2; j++) {
            temp.v[i][j] = 0;
            for (int k = 0; k != 2; k++) {
                temp.v[i][j] += a.v[i][k] * b.v[k][j];
                temp.v[i][j] %= M;
            }
        }
    }
    return temp;
}

Matrix power(Matrix a, Matrix b, long long n) {
    while (n) {
        if (n & 1) {
            b = matrixMul(b, a);
        }
        n >>= 1;
        a = matrixMul(a, a);
    }
    return b;
}

int main(int argc, char* argv[])
{
    Matrix a = {1, 1, 1, 0}, b = {1, 0, 0, 1};
    long long n;
    while (cin >> n) {
        if (n == 0)
            cout << 0 << endl;
        else {
            Matrix result = power(a, b, n - 1);
            cout << result.v[0][0] << endl;
        }
    }

    return 0;
}
时间: 2024-08-08 17:49:32

sicily 1001. Fibonacci 2的相关文章

Sicily 1001. Alphacode

1001. Alphacode Constraints Time Limit: 1 secs, Memory Limit: 32 MB Description Alice and Bob need to send secret messages to each other and are discussing ways to encode their messages: Alice: "Let's just use a very simple code: We'll assign `A' the

Sicily 1779. Fibonacci Sequence Multiplication

1779. Fibonacci Sequence Multiplication Constraints Time Limit: 1 secs, Memory Limit: 63.9990234375 MB Description Maybe all of you are familiar with Fibonacci sequence. Now you are asked to solve a special version of Fibonacci sequence: The Multipli

Sicily 1001(dp)

题目连接:http://soj.sysu.edu.cn/1001 解题报告:f[i]表示第i个字母前能组成的种类 情况:1.s[i]==0,f[i]=f[i-1] 2.s[i-1]*10+s[i]>=10&&<=26,f[i]=f[i-1]+f[i-2] 3.!s[i-1]*10+s[i]>=10&&<=26,f[i]=f[i-1]; #include <iostream> #include <cstring> #includ

Sicily 1001

1001. Alphacode Constraints Time Limit: 1 secs, Memory Limit: 32 MB Description Alice and Bob need to send secret messages to each other and are discussing ways to encode theirmessages: Alice: "Let's just use a very simple code: We'll assign `A' the

sicily Fibonacci 2

矩阵快速幂,1001. Fibonacci 2求斐波那契第n项!毕竟数据量太大! http://soj.sysu.edu.cn/show_problem.php?pid=1001&cid=1740 1 #include <iostream> 2 #include <cstring> 3 4 using namespace std; 5 6 struct mat { 7 long long m[2][2]; 8 mat() 9 { 10 memset(m, 0, sizeof

编程题目分类(剪辑)

1. 编程入门 2. 数据结构 3. 字符串 4. 排序 5. 图遍历 6. 图算法 7. 搜索:剪枝,启发式搜索 8. 动态规划/递推 9. 分治/递归 10. 贪心 11. 模拟 12. 算术与代数 13. 组合问题 14. 数论 15. 网格,几何,计算几何 [编程入门] PC 110101, uva 100, The 3n+1 problem, 难度 1 PC 110102, uva 10189, Minesweeper, 难度 1 PC 110103, uva 10137, The T

BestCoder10 1001 Revenge of Fibonacci(hdu 5018) 解题报告

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5018 题目意思:给出在 new Fibonacci 中最先的两个数 A 和 B(也就是f[1] = A, f[2] = B),通过这条式子f[n] = f[n-1] + f[n-2],问 C 是否在这条 new Fibonacci sequence 内.(1 <= A, B, C <= 1 000 000 000) 首先,要想到 C 有可能是 A 或者 B,这种情况也是属于在这个序列范围内的. 还

杭电1848 Fibonacci again and again(博弈-打表)

Fibonacci again and again Time Limit: 1000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 5093    Accepted Submission(s): 2127 Problem Description 任何一个大学生对菲波那契数列(Fibonacci numbers)应该都不会陌生,它是这样定义的: F(1)=1; F(2)

大菲波数(Fibonacci)java大数(hdu1715)

大菲波数 Time Limit: 1000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 11520 Accepted Submission(s): 3911 Problem Description Fibonacci数列,定义如下:f(1)=f(2)=1f(n)=f(n-1)+f(n-2) n>=3.计算第n项Fibonacci数值. Input 输入第一行为一个整数N,接