<Sicily>Fibonacci 2
一、题目描述
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的更多相关文章
- <Sicily>Fibonacci
一.题目描述 In the Fibonacci integer sequence, F0 = 0, F1 = 1, and Fn = Fn-1 + Fn-2 for n ≥ 2. For exampl ...
- sicily 1001. Fibonacci 2
1001. Fibonacci 2 Description In the Fibonacci integer sequence, F0 = 0, F1 = 1, and Fn = Fn-1 + F ...
- 算法与数据结构(九) 查找表的顺序查找、折半查找、插值查找以及Fibonacci查找
今天这篇博客就聊聊几种常见的查找算法,当然本篇博客只是涉及了部分查找算法,接下来的几篇博客中都将会介绍关于查找的相关内容.本篇博客主要介绍查找表的顺序查找.折半查找.插值查找以及Fibonacci查找 ...
- #26 fibonacci seqs
Difficulty: Easy Topic: Fibonacci seqs Write a function which returns the first X fibonacci numbers. ...
- 关于java的递归写法,经典的Fibonacci数的问题
经典的Fibonacci数的问题 主要想展示一下迭代与递归,以及尾递归的三种写法,以及他们各自的时间性能. public class Fibonacci { /*迭代*/ public static ...
- 斐波拉契数列(Fibonacci) 的python实现方式
第一种:利用for循环 利用for循环时,不涉及到函数,但是这种方法对我种小小白来说比较好理解,一涉及到函数就比较抽象了... >>> fibs = [0,1] >>&g ...
- fibonacci数列(五种)
自己没动脑子,大部分内容转自:http://www.jb51.net/article/37286.htm 斐波拉契数列,看起来好像谁都会写,不过它写的方式却有好多种,不管用不用的上,先留下来再说. 1 ...
- POJ3070 Fibonacci[矩阵乘法]
Fibonacci Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 13677 Accepted: 9697 Descri ...
- sicily 中缀表达式转后缀表达式
题目描述 将中缀表达式(infix expression)转换为后缀表达式(postfix expression).假设中缀表达式中的操作数均以单个英文字母表示,且其中只包含左括号'(',右括号‘)’ ...
随机推荐
- DB-MySQL:MySQL 函数
ylbtech-DB-MySQL:MySQL 函数 1. MySQL 字符串函数返回顶部 1. MySQL 字符串函数 函数 描述 实例 ASCII(s) 返回字符串 s 的第一个字符的 ASCII ...
- Z 字形变换 C++实现 java实现 leetcode系列(六)
Z 字形变换 java实现 C++实现 将一个给定字符串根据给定的行数,以从上往下.从左到右进行 Z 字形排列. 比如输入字符串为 "LEETCODEISHIRING" 行数为 ...
- Kafka Consumer1
本文的代码基于kafka的0.10.1的版本. 重新设计的原因 0.9以前的consumer是通过zookeeper来进行状态管理里的. 羊群效应 任何Broker或者Consumer的增减都会触发所 ...
- POJ 3617 Best Cow Line 贪心算法
Best Cow Line Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 26670 Accepted: 7226 De ...
- angular 报错笔记
1.错误信息: Failed to instantiate module app due to: Error: [$injector:unpr] http://errors.angularjs.org ...
- XML学习(一)——xml内容简介
一.什么是XML xml全称为Extensible Markup Language,意思是可扩展的标记语言.XML语法上和HTML比较相似,但是HTML中的元素是固定的,而XML的标签是可以用户定义的 ...
- 路飞学城Python-Day7
Moudle 2 1.鸡汤中国人均阅读4.35本:日本40本:韩国17本:法国20本:以色列60本成长的路上需要读书,坚持读书内心会得到升华的想法不要太多,尽量多读书,多充电多读书,多看报,少吃零食, ...
- VB创建文件夹
If Dir("D:\Program Files\AutoCAD 2006\Express\", vbDirectory) = "" Then '判断文件夹是否 ...
- Centos7&docker-ce&compose&wordpress
如题,最近帮人装个WordPress,想起来用docker方便,这里做个记录. 原文:https://my.oschina.net/finchxu/blog/2877580 因为docker要求lin ...
- 使用vuex的流程随笔
1.在建好的vue项目中新建一个vuex文件夹在此文件夹下建一个index.js文件,在此文件下引入vuex 模块(当然需要先npm下载)和vue模块,在引入你所有的自定义的module.js模块(下 ...