模板-->常系数线性齐次递推(矩阵快速幂)
如果有相应的OJ题目,欢迎同学们提供相应的链接
相关链接
简单的测试
None
代码模板
/*
* TIME COMPLEXITY:O(n^3log(t))
* PARAMS:
* a The constant array.
* b The initial array.
* n The length of array.
* t The iterator's value.
*
* MATRIX M:
* 0 1 0 ... 0
* 0 0 1 ... 0
* ... ... ...
* 0 0 0 ... 1
* a[n-1] a[n-2] a[n-3] ... a[0] But,here we use M.a[last row][i]=a[i],so inverse a before you call this function.
*
* MATRIX F
* b[0]
* b[1]
* .
* .
* .
* b[n] As we call M*F,the first row will gone and insert a new b[n+1]=M.a[last_row]*F.a[first_column] at the last row position.Until t is reached.
*/
int solve(int a[],int b[],int n,int t){
Matrix M,F,E;
M.clear(),F.clear(),E.clear();
M.n=M.m=n;
E.n=E.m=n;
F.n=n,F.m=1;
for(int i=0;i<n-1;i++)
M.a[i][i+1]=1;
for(int i=0;i<n;i++){
//notice:Maybe,first you should inverse a array before you call this function.
M.a[n-1][i]=a[i];
F.a[i][0]=b[i];
E.a[i][i]=1;
}
if(t<n)
return F.a[t][0];
//Here is fast Exponentiation.
for(t-=n-1;t;t/=2){
if(t&1)
E=M*E;
M=M*M;
}
F=E*F;
return F.a[n-1][0];
}
模板-->常系数线性齐次递推(矩阵快速幂)的更多相关文章
- 2019牛客暑期多校训练营(第五场)- B generator 1 (齐次线性递推+矩阵快速幂)
题目链接:https://ac.nowcoder.com/acm/contest/885/B 题意:已知齐次线性式xn=a*xn-1+b*xn-2,已知a,b,x0,x1,求xn,n很大,n<= ...
- HDU 5950 Recursive sequence 【递推+矩阵快速幂】 (2016ACM/ICPC亚洲区沈阳站)
Recursive sequence Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Other ...
- hdu 2604 递推 矩阵快速幂
HDU 2604 Queuing (递推+矩阵快速幂) 这位作者讲的不错,可以看看他的 #include <cstdio> #include <iostream> #inclu ...
- POJ3070 斐波那契数列递推 矩阵快速幂模板题
题目分析: 对于给出的n,求出斐波那契数列第n项的最后4为数,当n很大的时候,普通的递推会超时,这里介绍用矩阵快速幂解决当递推次数很大时的结果,这里矩阵已经给出,直接计算即可 #include< ...
- 2017中国大学生程序设计竞赛 - 女生专场 Happy Necklace(递推+矩阵快速幂)
Happy Necklace Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 131072/131072 K (Java/Others) ...
- HDU 2842 (递推+矩阵快速幂)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2842 题目大意:棒子上套环.第i个环能拿下的条件是:第i-1个环在棒子上,前i-2个环不在棒子上.每个 ...
- Recursive sequence HDU - 5950 (递推 矩阵快速幂优化)
题目链接 F[1] = a, F[2] = b, F[i] = 2 * F[i-2] + F[i-1] + i ^ 4, (i >= 3) 现在要求F[N] 类似于斐波那契数列的递推式子吧, 但 ...
- HDU6030 Happy Necklace(递推+矩阵快速幂)
传送门:点我 Little Q wants to buy a necklace for his girlfriend. Necklaces are single strings composed of ...
- 五校联考R1 Day1T3 平面图planar(递推 矩阵快速幂)
题目链接 我们可以把棱柱拆成有\(n\)条高的矩形,尝试递推. 在计算的过程中,第\(i\)列(\(i\neq n\))只与\(i-1\)列有关,称\(i-1\)列的上面/下面为左上/左下,第\(i\ ...
随机推荐
- 搭建BCE本地开发环境
1. 在官网下载VirtualBox & 虚拟机 http://bce.baidu.com/doc/BAE/GUIGettingStarted.html#.E4.B8.8B.E8.BD.BD. ...
- Device.js——检测设备平台、操作系统的Javascript 库
http://segmentfault.com/a/1190000000373735 Device.js 是一个可以让你检测设备的平台,操作系统和方向 JavaScript 库,它会自动在 <h ...
- 转:Ubuntu12.04 LTS 使用心得-开机挂载其他分区
1.在/media目录下创建好用来关联你要挂载的分区的文件夹(相当于一个虚拟目录/挂载点,链接/映射到你要挂载的盘符去) 我要挂载4个分区,所以创建了四个挂载点,名字随便取,只要你自己认的出来哪个对应 ...
- 使用CSS3改变选中元素背景色
CSS3代码如下: /* SELECTION ----------------- */ ::-moz-selection { background: #f00533; color: white; te ...
- Qt tip 网络请求 QNetworkRequest QJason 处理 JSON
http://blog.csdn.net/linbounconstraint/article/details/52399415 http://download.csdn.net/detail/linb ...
- TControl的显示函数(5个非虚函数,4个虚函数)和三个例子的执行过程(包括SetParent的例子)
// 9个显示函数 procedure SetBounds(ALeft, ATop, AWidth, AHeight: Integer); virtual; // 虚函数,important 根据父控 ...
- ParentWindow属性及其一系列函数的作用——适合于那些不需要父控件管理内存释放的子控件
TWinControl = class(TControl) property ParentWindow: HWnd read FParentWindow write SetParentWindow; ...
- 【spring-boot】spring aop 面向切面编程初接触--切点表达式
众所周知,spring最核心的两个功能是aop和ioc,即面向切面,控制反转.这里我们探讨一下如何使用spring aop. 1.何为aop aop全称Aspect Oriented Programm ...
- Lua table使用
days = {"Sunday", "Monday", "Tuesday", "Wednesday", "Th ...
- 【HDOJ】2333 Assemble
二分+贪心策略.其中注释处很重要. #include <iostream> #include <cstdio> #include <cstring> #includ ...