一、题目描述

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. Spring整合Shiro从源代码探究机制

    首先从例如以下配置開始说起 ShiroDbFilterFactoryBean继承了ShiroFilterFactoryBean这个由jar提供的bean类, 而且它实现了InitializingBea ...

  2. Apicloud自定义模块

    各种坑,折腾了两天才有点头绪.我用的是Android Studio编辑器,官网是Eclipse的视频.文档也比较蛋疼. 自定义模块的目录结构要按照下面来处理 其中res_模块名,存放res和Andro ...

  3. CSS3个人盲点总结【总结中..........】

    ~:表示同辈元素之后指定类型的元素,如;elm1 ~ elm2表示,elm1之后的所有elm2元素,且elm1与elm2都是在同一个父级元素. +:表示同辈元素的兄弟元素. \A:一个空白换行符 &l ...

  4. kettle工具的设计模块

    大家都知道,每个ETL工具都用不同的名字来区分不同的组成部分.kettle也不例外. 比如,在 Kettle的四大不同环境工具 本博客,是立足于kettle工具的设计模块的概念介绍. 1.转换 转换( ...

  5. 【实战经验】64位Win7安装+32位Oracle + PL/SQL 解决方法

    软件环境:64位win7.32位Oracle 10g. PL/SQL 9.0.4.1644 前言:以前开发用的都是32位系统,突然换到64位上,安装环境真的有点麻烦了,尤其对于PL/SQL只支持32位 ...

  6. Android tablayout增加选择tab 的事件.

    tablayout在点击或者滑动的时候会触发监听事件  , 当你调用这个方法的时候 会触发事件 mTablayout.addOnTabSelectedListener(new TabLayout.On ...

  7. 【参考】Linux下的Memcache安装

    服务器端主要是安装memcache服务器端.下载:http://www.danga.com/memcached/dist/memcached-1.2.2.tar.gz另外,Memcache用到了lib ...

  8. java 通过cookie判断是否登陆

    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOEx ...

  9. Linux下重启mysql数据库的方法

    原文地址:Linux下重启mysql数据库的方法作者:于士博的视频教程 方法一: 命令: [root@localhost /]# /etc/init.d/mysql   start|stop|rest ...

  10. Vue 中 换行符获取

    当要获取到 vue 中 文本域的换行符时, 需要用到正则匹配. let reg = new RegExp('/n',"g"); let str = text.replace(reg ...