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 ...
随机推荐
- web window pixel等笔记
原文:http://www.w3cplus.com/css/viewports.html 屏幕尺寸 Screen size =显示器尺寸 screen.width 和 screen.height.这两 ...
- ansible-playbook剧本
Playbooks 是一种简单的配置管理系统与多机器部署系统的基础, 非常适合于复杂应用的部署 由 yaml 语言编写, 运行过程中, ansible-playbook 命令根据自上而下的顺序依次执行 ...
- 查看win信任的证书办法机构(CA机构的公钥)
cmd mmc
- 前端 HTML文档结构介绍
<!DOCTYPE HTML> <html> <head>...</head> <body>...</body> </ht ...
- Jquery-plugins-toastr-消息提示
toastr是一个基于jQuery简单.漂亮的消息提示插件,使用简单.方便,可以根据设置的超时时间自动消失. 1.使用很简单,首选引入toastr的js.css文件 html <link rel ...
- ConcurrentModificationException
//需求:如何集合中有给定的元素就在集合中在插入一个元素public class ListIteratorDemo2 { public static void main(String[] args) ...
- Git 全局设置
Git 全局设置: git config --global user.name "ASxx" git config --global user.email "123456 ...
- VMware coding Challenge:Date of Weekday
这道题再次证明了这种细节的题目,画个图容易搞清楚 import java.util.Scanner; public class Solution2 { static int DateOfWeekday ...
- #C++初学记录(ACM试题2)
Max Sum Given a sequence a[1],a[2],a[3]......a[n], your job is to calculate the max sum of a sub-seq ...
- mysql_connect
in this passage, we slove the problem about Mysql_connect. first, let' see an example: resource mysq ...