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
aaarticlea/jpeg;base64,/9j/4AAQSkZJRgABAQEAYABgAAD/2wBDAAgGBgcGBQgHBwcJCQgKDBQNDAsLDBkSEw8UHRofHh0aHBwgJC4nICIsIxwcKDcpLDAxNDQ0Hyc5PTgyPC4zNDL/2wBDAQkJCQwLDBgNDRgyIRwhMjIyMjIyMjIyMjIyMjIyMjIyMjIyMjIyMjIyMjIyMjIyMjIyMjIyMjIyMjIyMjIyMjL/wAARCAARABIDASIAAhEBAxEB/8QAGAABAAMBAAAAAAAAAAAAAAAAAAMFBwT/xAAlEAACAQQCAQMFAAAAAAAAAAABAgMABAURBiESIjFBMjZxdbP/xAAYAQACAwAAAAAAAAAAAAAAAAAAAwEEBf/EABsRAQEAAgMBAAAAAAAAAAAAAAEAAgMEEyFh/9oADAMBAAIRAxEAPwDQeRW+SyVnctBIkiiScOk87qm0ciP0aZWA8dkEDZA2fcGPCWPI+PXkUt3GIcQjkyQxTGdtMrAhUVQO5CraVd/UB1pa7cnHmbaW5hjxEktoZJJGulnjChWYsT4lvLoHvr3B1vommvuQYaSe/jGSxrW9yXEiCWIiTe9eWohvs/LH8n5ocDh9jlnsER+zt+9wDE9G0uKWO4hSaGRJIpFDI6MCrKewQR7ilVfFPs7B/r4P5rStB8ZJW9KUqIlKUoi//9k=" alt="" /> 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[][];
}; Matrix matrixMul(Matrix a, Matrix b) {
Matrix temp;
for (int i = ; i != ; i++) {
for (int j = ; j != ; j++) {
temp.v[i][j] = ;
for (int k = ; k != ; 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 & ) {
b = matrixMul(b, a);
}
n >>= ;
a = matrixMul(a, a);
}
return b;
} int main(int argc, char* argv[])
{
Matrix a = {, , , }, b = {, , , };
long long n;
while (cin >> n) {
if (n == )
cout << << endl;
else {
Matrix result = power(a, b, n - );
cout << result.v[][] << endl;
}
} return ;
}

sicily 1001. Fibonacci 2的更多相关文章

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

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5018 题目意思:给出在 new Fibonacci 中最先的两个数 A 和 B(也就是f[1] = A ...

  2. <Sicily>Fibonacci 2

    一.题目描述 In the Fibonacci integer sequence, F0 = 0, F1 = 1, and Fn = Fn-1 + Fn-2 for n ≥ 2. For exampl ...

  3. <Sicily>Fibonacci

    一.题目描述 In the Fibonacci integer sequence, F0 = 0, F1 = 1, and Fn = Fn-1 + Fn-2 for n ≥ 2. For exampl ...

  4. 学习笔记之1001 Inventions That Changed the World

    1001 Inventions That Changed the World: Jack Challoner: 9780764161360: Amazon.com: Books https://www ...

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

    大菲波数 Time Limit: 1000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Submissio ...

  6. BNU 13024 . Fi Binary Number 数位dp/fibonacci数列

    B. Fi Binary Number     A Fi-binary number is a number that contains only 0 and 1. It does not conta ...

  7. ACM-SG函数之Fibonacci again and again——hdu1848

    Fibonacci again and again Time Limit: 1000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Jav ...

  8. 算法与数据结构(九) 查找表的顺序查找、折半查找、插值查找以及Fibonacci查找

    今天这篇博客就聊聊几种常见的查找算法,当然本篇博客只是涉及了部分查找算法,接下来的几篇博客中都将会介绍关于查找的相关内容.本篇博客主要介绍查找表的顺序查找.折半查找.插值查找以及Fibonacci查找 ...

  9. #26 fibonacci seqs

    Difficulty: Easy Topic: Fibonacci seqs Write a function which returns the first X fibonacci numbers. ...

随机推荐

  1. Python程序性能分析模块----------cProfile

    cProfile分析器可以用来计算程序整个运行时间,还可以单独计算每个函数运行时间,并且告诉你这个函数被调用多少次 def foo(): pass import cProfile cProfile.r ...

  2. I/O复用----select

    2018-07-31 (星期二)I/O复用:    一个应用程序通常需要服务一个以上的文件描述符.    例如stdin,stdout,进程间通信以及若干文件进行I/O,如果不借助线程的话,(线程通常 ...

  3. [洛谷P5173]传球

    题目大意:有$n(n\leqslant3500)$个人坐成一个环,$0$号手上有个球,每秒钟可以向左或向右传球,问$m$秒后球在$0$号手上的方案数. 题解:一个$O(nm)$的$DP$,$f_{i, ...

  4. SPOJ8222/NSUBSTR:Substrings——题解

    https://www.luogu.org/problemnew/show/SP8222#sub http://www.spoj.com/problems/NSUBSTR/ 翻译来自洛谷. 你得到一个 ...

  5. [Leetcode] reverse integer 反转整数

    Reverse digits of an integer. Example1: x = 123, return 321Example2: x = -123, return -321 click to ...

  6. BZOJ4873 [Shoi2017]寿司餐厅 【最大权闭合子图】

    题目链接 BZOJ4873 题解 题意很鬼畜,就可以考虑网络流[雾] 然后就会发现这是一个裸的最大权闭合子图 就是注意要离散化一下代号 #include<algorithm> #inclu ...

  7. Linux 第30天: (08月5日) 练习和作业

    变量脚本 1.编写脚本/root/bin/systeminfo.sh,显示当前主机系统信息,包括主机名,IPv4地址,操作系统版本,内核版本,CPU型号,内存大小,硬盘大小 server_ip=`if ...

  8. best code #54 div 2 A 水

    A problem of sorting Accepts: 443 Submissions: 1696 Time Limit: 2000/1000 MS (Java/Others) Memory Li ...

  9. php下载大文件

    <?php $file = @ fopen($file_dir . $file_name,"r"); $filesize=filesize($file_dir.$file_n ...

  10. C++11新特性,对象移动,右值引用,移动构造函数

    C++11新标准中的一个最主要的特性就是移动而非拷贝对象的能力.接下来简要介绍一下相关概念. 右值引用 所谓右值引用就是必须绑定到右值的引用.通过 && 而不是 & 来获得右值 ...