矩阵乘法加速fib数列
考虑矩阵(1,1)(1,0)
#include<cstdio>
#include<cstring>
#include<iostream>
using namespace std;
#define LL long long
const int mod = 1e9+;
const int N = ;
struct Matrix{
int a[N][N];
Matrix(){
this -> clear();
}
void clear(){
memset(a,,sizeof(a));
}
void setone(){
this ->clear();for(int i=;i<N;++i)
a[i][i]=;
} Matrix operator * (const Matrix &x) {
Matrix c;
for (int k=;k<N;++k)
for (int i=;i<N;++i)
for (int j=;j<N;++j)
c.a[i][j]=(c.a[i][j]+(LL)a[i][k]*x.a[k][j])%mod;
return c;
}
};
int fibn(int x)
{
Matrix a,b;
a.clear();
a.a[][]=a.a[][]=a.a[][]=;
b.setone();
while(x)
{
if(x&) b=b*a;
a=a*a;
x>>=;
}
return (b.a[][]+b.a[][])%mod;
}
int main()
{
int n;
while (~scanf("%d",&n))
{
if (n==||n==) puts("");
else printf("%d\n",fibn(n-));
}
return ;
}
矩阵乘法加速fib数列的更多相关文章
- 如何使用矩阵乘法加速动态规划——以[SDOI2009]HH去散步为例
对这个题目的最初理解 开始看到这个题,觉得很水,直接写了一个最简单地动态规划,就是定义 f[i][j]为到了i节点路径长度为j的路径总数, 转移的话使用Floyd算法的思想去转移,借助这个题目也理解了 ...
- 『公交线路 状压dp 矩阵乘法加速』
公交线路 Description 小Z所在的城市有N个公交车站,排列在一条长(N-1)km的直线上,从左到右依次编号为1到N,相邻公交车站间的距离均为1km. 作为公交车线路的规划者,小Z调查了市民的 ...
- [模板][题解][Luogu1939]矩阵乘法加速递推(详解)
题目传送门 题目大意:计算数列a的第n项,其中: \[a[1] = a[2] = a[3] = 1\] \[a[i] = a[i-3] + a[i - 1]\] \[(n ≤ 2 \times 10^ ...
- BZOJ 1009 GT考试 (AC自动机 + 矩阵乘法加速dp)
题目链接: https://www.lydsy.com/JudgeOnline/problem.php?id=1009 题意: 准考证号为\(n\)位数\(X_1X_2....X_n(0<=X_ ...
- c++的矩阵乘法加速trick
最近读RNNLM的源代码,发现其实现矩阵乘法时使用了一个trick,这里描述一下这个trick. 首先是正常版的矩阵乘法(其实是矩阵乘向量) void matrixXvector(float* des ...
- P3193 [HNOI2008]GT考试(KMP+矩阵乘法加速dp)
P3193 [HNOI2008]GT考试 思路: 设\(dp(i,j)\)为\(N\)位数从高到低第\(i\)位时,不吉利数字在第\(j\)位时的情况总数,那么转移方程就为: \[dp(i,j)=dp ...
- POJ ---3070 (矩阵乘法求Fibonacci 数列)
Fibonacci Description In the Fibonacci integer sequence, F0 = 0, F1 = 1, and Fn = Fn − 1 + Fn − 2 ...
- 【bzoj5118】Fib数列2 费马小定理+矩阵乘法
题目描述 Fib定义为Fib(0)=0,Fib(1)=1,对于n≥2,Fib(n)=Fib(n-1)+Fib(n-2) 现给出N,求Fib(2^n). 输入 本题有多组数据.第一行一个整数T,表示数据 ...
- 题解——洛谷P1962 斐波那契数列(矩阵乘法)
矩阵乘法加速线性递推的典型 大概套路就是先构造一个矩阵\( F \)使得另一初始矩阵\( A \)乘以\( F^{x} \)能够得出第n项 跑的飞快 虽然我也不知道那个矩阵要怎么构造 或许就像我使用了 ...
随机推荐
- Linux系统入门-Bash初识
目录 Linux系统入门-Bash初识 Bash Shell介绍 Bash Shell的作用 Bash的两种使用方式 命令提示符 shell的基础语法 shell的基本特性 命令补全 linux快捷键 ...
- Nordic Collegiate Programming Contest 2015 B. Bell Ringing
Method ringing is used to ring bells in churches, particularly in England. Suppose there are 6 bells ...
- 笔记-python-实用-程序运算时间计算
方法1 import datetime starttime = datetime.datetime.now() #long running endtime = datetime.datetime.no ...
- Android Studio中不能显示svn的上传下载两个图标同时version control为灰,不可点击
最近在接触Android Studio,涉及到svn的配置,因为是先安装的svn,后安装的Android Studio,后边同事告诉我, Android Studio 的SVN安装与其他IDE有很大差 ...
- 2015多校训练第二场 hdu5305
把这题想复杂了,一直在考虑怎么快速的判断将选的边和已选的边无冲突,后来经人提醒发现这根本没必要,反正数据也不大开两个数组爆搜就OK了,搜索之前要先排除两种没必要搜的情况,这很容易想到,爆搜的时候注意几 ...
- iphone使用keychain来存取用户名和密码
1.在arc下系统提示使用__bridge http://www.cnblogs.com/zzltjnh/p/3885012.html 参考文档:http://blog.csdn.net/jerr ...
- JDK并发基础与部分源码解读
之前写的一个ppt 搬到博客来
- 大数据学习——spark安装
一主多从 1 上传压缩包 2 解压 -bin-hadoop2..tgz 删除安装包 -bin-hadoop2..tgz 重命名 mv spark-1.6.2-bin-hadoop2.6/ spark ...
- python - 接口自动化测试 - ReadExcel - 读取测试数据封装
# -*- coding:utf-8 -*- ''' @project: ApiAutoTest @author: Jimmy @file: read_excel.py @ide: PyCharm C ...
- dp好题 玲珑杯 Expected value of the expression
152 - Expected value of the expression Time Limit:2s Memory Limit:128MByte Submissions:135Solved:65 ...