斐波那契额数列的第N项

斐波那契数列的定义如下:

F(0) = 0

F(1) = 1

F(n) = F(n - 1) + F(n - 2) (n >= 2)

(1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, 144, 233, 377, ...)

给出n,求F(n),由于结果很大,输出F(n) % 1000000009的结果即可。

输入

输入1个数n(1 <= n <= 10^18)。

输出

输出F(n) % 1000000009的结果。

输入样例

11

输出样例

89

思路

矩阵快速幂的模板

下面是快速幂的模板,具体怎么来的请自行百度,我们将根据这个模板写矩阵快速幂

LL Qpow(LL a, LL b)
{
LL ret = 1, base = a;
while(b)
{
if(b & 1)
ret *= base;
base *= base;
b >>= 1;
}
return ret;
}

当我们用矩阵代替底数a时,这就涉及到ret和base的初始化和矩阵乘法

所以得出下面矩阵快速幂的模板

struct M
{
LL m[N][N];
};
M mul(M a, M b, int n)
{
M t;
//初始化
for(int i = 1;i <= n;++i)
for(int j = 1;j <= n;++j)
t.m[i][j] = 0;
//乘法运算
for(int i = 1;i <= n;++i)
for(int j = 1;j <= n;++j)
for(int k = 1;k <= n;++k)
t.m[i][j] = (t.m[i][j] + a.m[i][k] * b.m[k][j]) % MOD;//取模,视题目而定
return t;
}
M M_Qpow(M a, LL b, int n)
{
M ret, base;
//初始化,根据快速幂中,ret = 1,ret初始化成单位矩阵
for(int i = 1;i <= n;++i)
{
for(int j = 1;j <= n;++j)
{
ret.m[i][j] = 0;
ret.m[i][i] = 1;
base.m[i][j] = a.m[i][j];
}
}
//注意矩阵乘法中a*b != b*a
while(b)
{
if(b & 1)
ret = mul(ret, base, n);
base = mul(base, base, n);
b >>= 1;
}
return ret;
}

这道题中,构造这样的矩阵[fn, fn - 1] * b = [fn + 1, fn],然后求矩阵b,这个矩阵容易推算出来

b = [1, 1

1, 0]

解题代码

#include <iostream>
#include <cstdio>
#include <string>
#include <cstring>
#include <cmath>
#include <sstream>
#include <algorithm>
#include <set>
#include <map>
#include <vector>
#include <queue>
#include <iomanip>
#include <stack> using namespace std; typedef long long LL;
const int INF = 0x3f3f3f3f;
const int N = 105;
const int MOD = 1e9 + 9; struct M
{
LL m[N][N];
};
M mul(M a, M b, int n)
{
M t;
//初始化
for(int i = 1;i <= n;++i)
for(int j = 1;j <= n;++j)
t.m[i][j] = 0;
//乘法运算
for(int i = 1;i <= n;++i)
for(int j = 1;j <= n;++j)
for(int k = 1;k <= n;++k)
t.m[i][j] = (t.m[i][j] + a.m[i][k] * b.m[k][j]) % MOD;
return t;
} M M_Qpow(M a, LL b, int n)
{
M ret, base;
//初始化,根据快速幂中,ret = 1,ret初始化成单位矩阵
for(int i = 1;i <= n;++i)
{
for(int j = 1;j <= n;++j)
{
ret.m[i][j] = 0;
ret.m[i][i] = 1;
base.m[i][j] = a.m[i][j];
}
}
//注意矩阵乘法中a*b != b*a
while(b)
{
if(b & 1)
ret = mul(ret, base, n);
base = mul(base, base, n);
b >>= 1;
}
return ret;
} int main()
{
M a, b;
b.m[1][1] = 1, b.m[1][2] = 1, b.m[2][1] = 1, b.m[2][2] = 0;
a.m[1][1] = 1, a.m[1][2] = 0;
LL n;
cin >> n;
M c = mul(a, M_Qpow(b, n - 1, 2), 2);
cout << c.m[1][1] << endl;
return 0;
}

矩阵快速幂--51nod-1242斐波那契数列的第N项的更多相关文章

  1. (矩阵快速幂)51NOD 1242斐波那契数列的第N项

    斐波那契数列的定义如下:   F(0) = 0 F(1) = 1 F(n) = F(n - 1) + F(n - 2) (n >= 2)   (1, 1, 2, 3, 5, 8, 13, 21, ...

  2. 51nod 1242 斐波那契数列的第N项

    之前一直没敢做矩阵一类的题目 其实还好吧 推荐看一下 : http://www.cnblogs.com/SYCstudio/p/7211050.html 但是后面的斐波那契 推导不是很懂  前面讲的挺 ...

  3. 51Nod 1242 斐波那契数列的第N项(矩阵快速幂)

    #include <iostream> #include <algorithm> using namespace std; typedef long long LL; ; ; ...

  4. 51nod 1242 斐波那契数列的第N项——数学、矩阵快速幂

    普通算法肯定T了,所以怎么算呢?和矩阵有啥关系呢? 打数学符号太费时,就手写了: 所以求Fib(n)就是求矩阵  |  1  1  |n-1  第一行第一列的元素. |  1  0  | 其实学过线代 ...

  5. 51 Nod 1242 斐波那契数列的第N项(矩阵快速幂模板题)

    1242 斐波那契数列的第N项  基准时间限制:1 秒 空间限制:131072 KB 分值: 0 难度:基础题  收藏  关注 斐波那契数列的定义如下: F(0) = 0 F(1) = 1 F(n) ...

  6. 1242 斐波那契数列的第N项

    1242 斐波那契数列的第N项  基准时间限制:1 秒 空间限制:131072 KB 分值: 0 难度:基础题   斐波那契数列的定义如下:   F(0) = 0 F(1) = 1 F(n) = F( ...

  7. 51Nod——T 1242 斐波那契数列的第N项

    https://www.51nod.com/onlineJudge/questionCode.html#!problemId=1242 基准时间限制:1 秒 空间限制:131072 KB 分值: 0  ...

  8. python脚本10_打印斐波那契数列的第101项

    #打印斐波那契数列的第101项 a = 1 b = 1 for count in range(99): a,b = b,a+b else: print(b) 方法2: #打印斐波那契数列的第101项 ...

  9. 51Nod - 1242 斐波那契(快速幂)

    斐波那契数列的定义如下:   F(0) = 0 F(1) = 1 F(n) = F(n - 1) + F(n - 2) (n >= 2)   (1, 1, 2, 3, 5, 8, 13, 21, ...

随机推荐

  1. python学习的一点点心得

    好久没发博客了,不解释....接下来写一点自己最近学习python的一点心得. 想要学习python的初衷,是看<软件测试技术大全>一书时,了解到像perl.python.ruby等脚本类 ...

  2. springboot启动正常,访问restController报404

    原因:spring boot只会扫描启动类当前包和以下的包 比如以下: 主类:Application放在包com.springboot.main controller类放在包com.springboo ...

  3. 【转载】Redis sort 排序命令详解

    转载地址:http://www.jb51.net/article/69131.htm 本文介绍redis排序命令 redis支持对list,set,sorted set元素的排序 sort 排序命令格 ...

  4. hibernate 多表联合查询

    以前用sql实现联合查询 是非常简单的事,只需要写sql语句就可以,第一次遇到hibernate要实现多表联合查询的时候还楞了一下.最后看了下资料,才恍然大悟,hibernate实现多表联合查询跟SQ ...

  5. javascript的caller,callee,call,apply[转]

    在提到上述的概念之前,首先想说说javascript中函数的隐含参数:arguments Arguments 该对象代表正在执行的函数和调用它的函数的参数. [function.]arguments[ ...

  6. 再次理解js中的call函数

    a.call(b); 网上说明的版本比较多.有的说,是指针替换.有说,将a对象的方法加在b对象执行.官方说:什么对象替换什么对象.反正看了几个版本,尽管有具体的实例,看了我三次都没看懂它的具体含义.看 ...

  7. Linux中Consul集群部署

    分配三台虚拟机: 192.168.5.125 192.168.5.128 192.168.5.129 在每台虚拟机上创建  /usr/consul 文件件  命令: mkdir /usr/consul ...

  8. centos7 安装git

    centos7下git的安装和配置   git的安装: yum 源仓库里的 Git 版本更新不及时,最新版本的 Git 是 1.8.3.1,但是官方最新版本已经到了 2.9.2.想要安装最新版本的的 ...

  9. Python短路原则

    1.括号内逻辑先执行 2.and优先级大于or 3.and一假为假 4.or一真为真 and:如果左边为假,返回左边值.如果左边不为假,返回右边值. or:如果左边为真,返回左边值.如果左边不为真,返 ...

  10. 爬虫开发6.selenuim和phantonJs处理网页动态加载数据的爬取

    selenuim和phantonJs处理网页动态加载数据的爬取阅读量: 1203 动态数据加载处理 一.图片懒加载 什么是图片懒加载? 案例分析:抓取站长素材http://sc.chinaz.com/ ...