洛谷P1962 斐波那契数列题解
题目背景
大家都知道,斐波那契数列是满足如下性质的一个数列:
• f(1) = 1
• f(2) = 1
• f(n) = f(n-1) + f(n-2) (n ≥ 2 且 n 为整数)
题目描述
请你求出 f(n) mod 1000000007 的值。
输入格式
·第 1 行:一个整数 n
输出格式
第 1 行: f(n) mod 1000000007 的值
输入输出样例
输入 #1 复制
5
输出 #1 复制
5
输入 #2 复制
10
输出 #2 复制
55
说明/提示
对于 60% 的数据: n ≤ 92
对于 100% 的数据: n在long long(INT64)范围内。
解析:
\(\displaystyle \begin{array}{{>{\displaystyle}l}}
\ 现在我需要求的矩阵是:\\
\begin{bmatrix}
f[ i]\\
f[ i-1]
\end{bmatrix}\\
根据题目中给出的条件:f[ i] =f[ i-1] +f[ i-2]\\
下一步求f[ i+1]\\
所以求初始矩阵为\\
\begin{bmatrix}
1 & 1\\
1 & 0
\end{bmatrix}\\
对初始矩阵进行矩阵快速幂然后输出a[ 1][ 1]
\end{array}\)
#include <cstdio>
#include <iostream>
#include <cmath>
#include <cstring>
#include <queue>
#include <stack>
#define re register
#define Max 200000012
#define int long long
int n;
const int mod=1000000007;
struct Mat {
int a[3][3];
Mat() {memset(a,0,sizeof a);}
inline void build() {
memset(a,0,sizeof a);
for(re int i = 1 ; i <= 2 ; ++ i) a[i][i]=1;
}
};
Mat operator*(Mat &a,Mat &b)
{
Mat c;
for(re int k = 1 ; k <= 2 ; ++ k)
for(re int i = 1 ; i <= 2 ; ++ i)
for(re int j = 1 ; j <= 2 ; ++ j)
c.a[i][j]=(c.a[i][j]+a.a[i][k]*b.a[k][j]%mod)%mod;
return c;
}
Mat a;
Mat quick_Mat(int x)
{
Mat ans;ans.build();
while(x) {
if((x&1)==1) ans = ans * a;
a = a * a;
x >>= 1;
}
return ans;
}
signed main()
{
scanf("%lld",&n);
a.a[1][1]=1;a.a[1][2]=1;
a.a[2][1]=1;Mat b;
b.a[1][1]=1;b.a[2][1]=1;
if(n>=1 && n<=2) {
printf("1");return 0;
}
Mat ans=quick_Mat(n-2);
ans=ans*b;
printf("%lld",ans.a[1][1]);
return 0;
}
洛谷P1962 斐波那契数列题解的更多相关文章
- 洛谷P1962 斐波那契数列【矩阵运算】
洛谷P1962 斐波那契数列[矩阵运算] 题目背景 大家都知道,斐波那契数列是满足如下性质的一个数列: • f(1) = 1 • f(2) = 1 • f(n) = f(n-1) + f(n-2) ( ...
- 洛谷P1962 斐波那契数列 || P1349 广义斐波那契数列[矩阵乘法]
P1962 斐波那契数列 大家都知道,斐波那契数列是满足如下性质的一个数列: • f(1) = 1 • f(2) = 1 • f(n) = f(n-1) + f(n-2) (n ≥ 2 且 n 为整数 ...
- 洛谷——P1962 斐波那契数列
P1962 斐波那契数列 题目背景 大家都知道,斐波那契数列是满足如下性质的一个数列: • f(1) = 1 • f(2) = 1 • f(n) = f(n-1) + f(n-2) (n ≥ 2 且 ...
- 洛谷—— P1962 斐波那契数列
https://www.luogu.org/problem/show?pid=1962 题目背景 大家都知道,斐波那契数列是满足如下性质的一个数列: • f(1) = 1 • f(2) = 1 • f ...
- 洛谷P1962 斐波那契数列(矩阵快速幂)
题目背景 大家都知道,斐波那契数列是满足如下性质的一个数列: • f(1) = 1 • f(2) = 1 • f(n) = f(n-1) + f(n-2) (n ≥ 2 且 n 为整数) 题目描述 请 ...
- 【洛谷P1962 斐波那契数列】矩阵快速幂+数学推导
来提供两个正确的做法: 斐波那契数列双倍项的做法(附加证明) 矩阵快速幂 一.双倍项做法 在偶然之中,在百度中翻到了有关于斐波那契数列的词条(传送门),那么我们可以发现一个这个规律$ \frac{F_ ...
- 洛谷 P1962 斐波那契数列
题目链接:https://www.luogu.org/problemnew/show/P1962 题目大意: 略 分析: 由于数据规模很大,需要用矩阵快速幂来解. 代码如下: #pragma GCC ...
- 题解——洛谷P1962 斐波那契数列(矩阵乘法)
矩阵乘法加速线性递推的典型 大概套路就是先构造一个矩阵\( F \)使得另一初始矩阵\( A \)乘以\( F^{x} \)能够得出第n项 跑的飞快 虽然我也不知道那个矩阵要怎么构造 或许就像我使用了 ...
- 洛谷P1962 斐波那契数列
传送门 不难得到状态转移矩阵 然后带进去乱搞 //minamoto #include<iostream> #include<cstdio> #include<cstrin ...
随机推荐
- javaScript 对象的hasOwnProperty方法打印window自定义属性
for (var name in window) { if (window.hasOwnProperty(name)) { window.console.log ( name + " : & ...
- prometheus消耗内存问题
参考: https://stackoverflow.com/questions/56115912/why-does-prometheus-consume-so-much-memory https:// ...
- 题解 POJ 2559【Largest Rectangle in a Histogram】(单调栈)
题目链接:http://poj.org/problem?id=2559 思路:单调栈 什么是单调栈? 单调栈,顾名思义,就是单调的栈,也就是占中存的东西永远是单调(也就是递增或递减)的 如何实现一个单 ...
- centos从零开始安装elasticSearch
前言:elasticSearch作为一款优秀的分布式搜索工具,被广泛用在数据搜集和整理的业务中,知名的比如有github就是采用es来精准的搜索几千万行代码,百度也大量应用es做数据爬取分析,本篇博客 ...
- Myeclipse6.5迁移到IDEA
背景 myeclipse开发的javaweb项目用svn管理.现要转用idea开发.因为发现idea实在是太好用了.myeclipse6.5是个纯净版,用了两年,对于新手来说用myeclipse6.5 ...
- ip2region.jar实现ip转地址
ip转地址 根据ip地址查询出所在地址. GitHub地址 https://github.com/lionsoul2014/ip2region/ pom坐标 <dependency> &l ...
- python基础--py2与py3编码
python2 与 python3的编码和解码 注意:小心,容易弄混 目录: 1.python2d 的encode & decode 2.python3的encode & decode ...
- SimHash算法--文章相似度匹配
SimHash原理 1.SimHash背景 SimHash算法来自于 GoogleMoses Charikar发表的一篇论文"detecting near-duplicates for we ...
- react 爬坑记录
1.父子组件优化其一发生render条件:数据改变(state或者props改变),有时子组件会过多render.这时可在子组件里面的生命周期钩子里执行 shouldComponentUpdate(n ...
- SpringBoot2.x搭建Eureka
1 说明 全部配置基于1.8.0_111 当前SpringBoot使用2.0.5 2 创建项目 在SpringBoot项目生成器中,输入Group和Artifact,如下配置: 3 pom.xml配置 ...