51nod 1242 斐波那契数列的第N项
之前一直没敢做矩阵一类的题目
其实还好吧
推荐看一下 : http://www.cnblogs.com/SYCstudio/p/7211050.html
但是后面的斐波那契 推导不是很懂 前面讲的挺好的
后来看到了 http://blog.csdn.net/flyfish1986/article/details/48014523
相当于 是一个那个东西的k-1次方 而且由于 F(1) = 1 所以直接求k-1次方就可以了
#include<bits/stdc++.h>
using namespace std;
const int mod = 1e9+;
typedef long long ll; class Matrix
{
public:
ll s[][];
Matrix()
{
memset(s,,sizeof(s));
}
Matrix(ll a[][])
{
for(int i=;i<;i++)
for(int j=;j<;j++)
s[i][j] = a[i][j];
}
};
Matrix operator *(Matrix a,Matrix b)
{
Matrix ans;
for(int i=;i<;i++)
for(int j=;j<;j++)
for(int k=;k<;k++)
ans.s[i][j] = (ans.s[i][j] + a.s[i][k]*b.s[k][j]%mod)%mod;
return ans;
} void _pow(ll k)
{ ll a[][];
a[][] = ,a[][]=,a[][]=,a[][]=;//初始化特征矩阵
Matrix A(a),B(a);
while (k>)
{
if(k&) A= A*B;
B= B*B;
k>>=;
}
printf("%lld\n",A.s[][]);//最后结果存储在矩阵第一行第一列上
} int main ()
{
ll n;
scanf("%lld",&n);
if(n<=)
{
printf("1\n");
return ;
}
n-=;//这里是因为 本身是矩阵的k-1次幂 但是 本身建立矩阵已经1次了
// 所以n-=2次
_pow(n);
}
51nod 1242 斐波那契数列的第N项的更多相关文章
- (矩阵快速幂)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, ...
- 51Nod 1242 斐波那契数列的第N项(矩阵快速幂)
#include <iostream> #include <algorithm> using namespace std; typedef long long LL; ; ; ...
- 51nod 1242 斐波那契数列的第N项——数学、矩阵快速幂
普通算法肯定T了,所以怎么算呢?和矩阵有啥关系呢? 打数学符号太费时,就手写了: 所以求Fib(n)就是求矩阵 | 1 1 |n-1 第一行第一列的元素. | 1 0 | 其实学过线代 ...
- 1242 斐波那契数列的第N项
1242 斐波那契数列的第N项 基准时间限制:1 秒 空间限制:131072 KB 分值: 0 难度:基础题 斐波那契数列的定义如下: F(0) = 0 F(1) = 1 F(n) = F( ...
- 51 Nod 1242 斐波那契数列的第N项(矩阵快速幂模板题)
1242 斐波那契数列的第N项 基准时间限制:1 秒 空间限制:131072 KB 分值: 0 难度:基础题 收藏 关注 斐波那契数列的定义如下: F(0) = 0 F(1) = 1 F(n) ...
- 51Nod——T 1242 斐波那契数列的第N项
https://www.51nod.com/onlineJudge/questionCode.html#!problemId=1242 基准时间限制:1 秒 空间限制:131072 KB 分值: 0 ...
- python脚本10_打印斐波那契数列的第101项
#打印斐波那契数列的第101项 a = 1 b = 1 for count in range(99): a,b = b,a+b else: print(b) 方法2: #打印斐波那契数列的第101项 ...
- 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, ...
- 斐波那契数列的第N项
题目链接:http://www.51nod.com/onlineJudge/questionCode.html#!problemId=1242 题目: 斐波那契数列的定义如下: F(0) = 0 ...
随机推荐
- Python 标准输出 sys.stdout 重定向
本文环境:Python 2.7 使用 print obj 而非 print(obj) 一些背景 sys.stdout 与 print 当我们在 Python 中打印对象调用 print obj 时候, ...
- Linux下设置python脚本文件为服务
(最简单的方式nohup python xxx.py) ------------------------------------------------------------------------ ...
- ListView and gridview常用属性
刷新:notifyDataSetChanged 1.gridview常用属性 GridView的一些特殊属性: 1.Android:numColumns=”auto_fit” //GridVi ...
- Git、bower 安装
1>下载并安装nodejs .老师分享的nodejs版本“node-v8.9.4-x64” 下载页面http://nodejs.cn/download/ 一直无脑下一步操作即可安装完毕 ...
- visualSVN server安装使用
SVN服务推荐使用visualSVN server,安装完成之后自动设置开机启动服务,具体使用如下图:
- Word Add-in 函数调用顺序
这个图表明的函数的调用顺序,主要代码如下: // MyAddin.cpp : Implementation of DLL Exports. // Note: Proxy/Stub Informatio ...
- [LeetCode] 232. Implement Queue using Stacks_Easy tag: Design
Implement the following operations of a queue using stacks. push(x) -- Push element x to the back of ...
- bootstrap modal 垂直居中对齐
bootstrap modal 垂直居中对齐 文章参考 http://www.bubuko.com/infodetail-666582.html http://v3.bootcss.com/Jav ...
- Trove系列(一)—入门篇
概述DBaaS是目前云计算服务的重要部分,数据库作为一种特殊的应用程序,在应用中普遍存在.而其独特性不仅在于普遍性,而且其性能对应用的表现是至关重要的.数据库的通用性和重要性使得维护一个健壮的数据库实 ...
- 如何制作Windows镜像
1.在https://msdn.itellyou.cn/网站中下载(使用迅雷)Windows2003R2 中文版ISO 2.使用qemu-img create命令创建一个空的 后缀为.img的文件 q ...