斐波那契额数列的第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. Docker for mac安装

    Mac安装Docker docker下载地址: https://hub.docker.com/editions/community/docker-ce-desktop-mac docker for m ...

  2. 组织机构sql

    with cte as (     select vcOrganID, vcParentID, vcOrganName, 0 as lvl from tbOrgan     where vcOrgan ...

  3. Reading——简约至上

    读书感言: 简约至上——Giles Colborne,我去,这是哪里来的渣书,通篇都是泛泛而谈,实在受不鸟了> <,没学到啥实质性的东西,论述一大堆.!!!还姐的20多块钱.最讨厌这样的书 ...

  4. Go 面向对象概念

    前言: 本文是学习<<go语言程序设计>> -- 清华大学出版社(王鹏 编著) 的2014年1月第一版 做的一些笔记 , 如有侵权, 请告知笔者, 将在24小时内删除, 转载请 ...

  5. 将windows上面的项目拷贝到Linux环境下报错不能够找到对应的表com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException: Table 'puyang.ServiceType' doesn't exist

    将一模一样的项目从win迁移到到linux上报错: 一开始还是以为是linux不能识别hql语句,查找资料发现是因为Liunx服务器上mysql是区分大小写的,而本地是不区分的如:代码是这样写的 @E ...

  6. NArrange 配置与使用

    1. 在VS中设置一下就可以用了 2.It is also recommended to setup a restore command using the same parameters, repl ...

  7. Qt资源整理ING

    QCustomPlot:图表库,开源, 链接地址http://www.qcustomplot.com/index.php/download 一些Qt的开发库:http://qt-project.org ...

  8. HDU 6097 Mindis (计算几何)

    题意:给一个圆C和圆心O,P.Q是圆上或圆内到圆心距离相等的两个点,在圆上取一点D,求|PD| + |QD|的最小值 析:首先这个题是可以用三分过的,不过也太,.... 官方题解: 很不幸不总是中垂线 ...

  9. SpringCloud教程 | 第四篇:断路器(Hystrix)(Finchley版本)

    在微服务架构中,根据业务来拆分成一个个的服务,服务与服务之间可以相互调用(RPC),在Spring Cloud可以用RestTemplate+Ribbon和Feign来调用.为了保证其高可用,单个服务 ...

  10. touch和click优先性

    jQuery的touch事件是当用户触摸事件(页面)时触发的. jQuery的click事件是当用户点击元素时触发的. 而事件执行流程是手指点击一个元素,会经过:touchstart --> t ...