一、题目描述

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).

二、输入

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

There are multiple test cases!

三、输出

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

例如:

输入:9

输出:34

四、解题思路

这次要求输入的数会很大,使用递归或者动态规划的方法时,会超时。

所以只能使用矩阵方法求解。把问题转换为矩阵相乘,矩阵相乘可以通过快速幂的求解方法提高效率。

1、矩阵表示

2、矩阵推导公式

3、带入f(0),f(1)

由上面的公式能可知,可以把原问题转换成求一个矩阵的n次幂问题。

4、整数n次方的快速幂求法

例如求3的999次方

快速幂求法

5、使用同样的方法求一矩阵n次方

五、代码

#include <iostream>
using namespace std; const long long int MODE = 1000000000+7; //当数太大是取模(题目要求) struct Matrix //矩阵
{
long long int mat[2][2];
}; Matrix matrixMultiply(Matrix m1, Matrix m2) //两个矩阵乘积
{
Matrix result; for(int i = 0; i < 2; i++) //第i行(m1)
{
for(int j = 0; j < 2; j++) //第j列(m2)
{
result.mat[i][j] = 0;
for(int k = 0; k < 2; k++) //m1的第i行乘以m2的第j列
{
result.mat[i][j] += m1.mat[i][k] * m2.mat[k][j];
result.mat[i][j] %= MODE;
}
}
} return result;
} Matrix exponentiate(Matrix m1, long long int n) //矩阵的n次幂
{
Matrix result = {1, 0, 1, 0};
while(n)
{
if(n & 1) result = matrixMultiply(result, m1); //当n为奇数时
m1 = matrixMultiply(m1, m1);
n >>= 1;
} return result;
} int main()
{
Matrix baseMatrix = {1, 1, 1, 0};
Matrix resultMatrix;
long long int n, fn;
while(cin >> n)
{
if(n == 0 || n == 1)
{
cout << n << endl;
continue;
} resultMatrix = exponentiate(baseMatrix, n);
fn = resultMatrix.mat[0][1];
cout << fn << endl;
} return 0; }

<Sicily>Fibonacci 2的更多相关文章

  1. <Sicily>Fibonacci

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

  2. sicily 1001. Fibonacci 2

    1001. Fibonacci 2   Description In the Fibonacci integer sequence, F0 = 0, F1 = 1, and Fn = Fn-1 + F ...

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

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

  4. #26 fibonacci seqs

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

  5. 关于java的递归写法,经典的Fibonacci数的问题

    经典的Fibonacci数的问题 主要想展示一下迭代与递归,以及尾递归的三种写法,以及他们各自的时间性能. public class Fibonacci { /*迭代*/ public static ...

  6. 斐波拉契数列(Fibonacci) 的python实现方式

    第一种:利用for循环 利用for循环时,不涉及到函数,但是这种方法对我种小小白来说比较好理解,一涉及到函数就比较抽象了... >>> fibs = [0,1] >>&g ...

  7. fibonacci数列(五种)

    自己没动脑子,大部分内容转自:http://www.jb51.net/article/37286.htm 斐波拉契数列,看起来好像谁都会写,不过它写的方式却有好多种,不管用不用的上,先留下来再说. 1 ...

  8. POJ3070 Fibonacci[矩阵乘法]

    Fibonacci Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 13677   Accepted: 9697 Descri ...

  9. sicily 中缀表达式转后缀表达式

    题目描述 将中缀表达式(infix expression)转换为后缀表达式(postfix expression).假设中缀表达式中的操作数均以单个英文字母表示,且其中只包含左括号'(',右括号‘)’ ...

随机推荐

  1. Elasticsearch yellow 意味着主分片可用,副本不可用

    摘自:http://unasm.com/2016/11/644/ 在通过 /_cluster/state 命令查看es 状态的时候,发现es 处于一个yellow的状态, 这个很奇怪,按照官方的解释, ...

  2. PyCharm基本设置、常用快捷键

    1. 下载安装 PyCharm官方下载地址:  https://www.jetbrains.com/pycharm/download/index.html#section=windows 安装完成后在 ...

  3. Service Mesh(服务网格)

    Service Mesh(服务网格) 什么是Service Mesh(服务网格)Service mesh 又译作 "服务网格",作为服务间通信的基础设施层.Buoyant 公司的 ...

  4. JQuery中的时间和动画

    我们知道JavaScript和HTML之间的交互是通过用户操作和浏览器成生成事件来完成的,比如当浏览钱加载完一个HTML文档或用户点击一个按钮都会生成一个事件,虽然利用传统的JavaScript事件可 ...

  5. jqGrid冻结列

    jqgrid冻结列 冻结列:就是横向移动表格时,让某一列保持不动 做法: 1.colModel的行要加上属性: frozen:true.注意:冻结列必须从第一列开始,包括隐藏列 2.加载jqgrid后 ...

  6. python 3.x 学习笔记11 (静态、类、属性、特殊成员方法)

    1.静态方法通过@staticmethod装饰器即可把其装饰的方法变为一个静态方法.静态方法是不可以访问实例变量或类变量的即没有self,一个不能访问实例变量和类变量的方法,其实相当于跟类本身已经没什 ...

  7. Codeforces 986A. Fair(对物品bfs暴力求解)

    解题思路: 1.对物品i bfs,更新每个小镇j获得每个物品i的最短距离. 2.时间复杂度o(n*k),满足2s的要求. 代码: #include <iostream> #include ...

  8. 51nod 1562 玻璃切割 (STL map+一点点的思考)

    1562 玻璃切割 题目来源: CodeForces 基准时间限制:1.5 秒 空间限制:131072 KB 分值: 20 难度:3级算法题 现在有一块玻璃,是长方形的(w 毫米× h 毫米),现在要 ...

  9. windows 路由

    route ? 查看帮助 route print 查看路由表 添加一条路由: route add 10.10.10.0 mask 255.255.255.0  192.168.1.1 #到达10.10 ...

  10. 路飞学城Python-Day15(模块二思维导图)