hrbust/哈理工oj 1126 Final Destination II【矩阵快速幂+递推】
来源:互联网 发布:香蕉网络免费频道 编辑:程序博客网 时间:2024/04/29 22:19
JiaoZhu likes going on adventure! One day, he walks into a big castle, and there is an unique stairway. JiaoZhu finds a board ,it says “The one who want to go upstairs only can go three steps the most once, meaning that you can go 1 or 2 or 3 steps once!”. Now, we have a problem, can you tell me the number of ways to go to the destination? If you can’t ,death is the only choice。
In the beginning, you are in the 0th step.
InputFirst input a integer T(T<50), represent the number of case.
Each case ,the input will consist only a positive integer n (0<=n<=1000000000), represent the nth steps you want to go to..
OutputOrder the sample output format to output.
Line 1,print the Case k.
Line 2,print one integer represent the number of ways to go to nth steps.(MOD 1000000007)
Sample InputWhen n=2,you can go one step once to go to 2th ,or go 2 steps once to 2th,so the answer is 2.
Author齐达拉图题目大意:你初始的时候在第0阶台阶上,给你一个目标N阶 ,问一共有多少种上法,注意1 2 和2 1算作两种。分析:
一道很简单的递推题目,主要是N特别大,直接跑o(N)也是一样会超时。所以我们利用公式+矩阵快速幂就能够优化时间复杂度,尤其在N特别大的时候,优化的越明显。
有关矩阵快速幂的学习大家可以去POJ 做一下3070 这里我提供题解:http://blog.csdn.net/mengxiang000000/article/details/50595065。
思路:
推出Fn=Fn-1+Fn-2+Fn-3。
然后老老实实的用草纸写下这样的方程式:
(???)X(Fn-1)=(Fn)(???) (Fn-2) (Fn-1)(???) (Fn-3) (Fn-2)如果你会矩阵乘法的话,你就一定能够算出这么些个问号都等于什么结果为:
1 1 1
1 0 0
0 1 0
然后自己拿草纸幂两次,不难发现B.mat【0】【0】就是结果。
AC代码:
#include<stdio.h>#include<iostream>#include<string.h>using namespace std;#define mod 1000000007#define ll long long inttypedef struct Matrix{ ll mat[3][3];}matrix;matrix A,B;Matrix matrix_mul(matrix a,matrix b)//矩阵乘法{ matrix c; memset(c.mat,0,sizeof(c.mat)); int i,j,k; for(int i=0;i<3;i++) { for(int j=0;j<3;j++) { for(int k=0;k<3;k++) { c.mat[i][j]+=a.mat[i][k]*b.mat[k][j]; c.mat[i][j]%=mod; } } } return c;}Matrix matrix_quick_power(matrix a,int k)//矩阵快速幂0.0{ matrix b; memset(b.mat,0,sizeof(b.mat)); for(int i=0;i<3;i++) b.mat[i][i]=1;//单位矩阵b while(k) { if(k%2==1) { b=matrix_mul(a,b); k-=1; } else { a=matrix_mul(a,a); k/=2; } } return b;}int main(){ int kase=0; int t; scanf("%d",&t); while(t--) { int n; scanf("%d",&n); A.mat[0][0]=1;A.mat[0][1]=1;A.mat[0][2]=1;//我们通过推论得到的矩阵A A.mat[1][0]=1;A.mat[1][1]=0;A.mat[1][2]=0; A.mat[2][0]=0;A.mat[2][1]=1;A.mat[2][2]=0; B=matrix_quick_power(A,n); printf("Case %d:\n",++kase); printf("%d\n",B.mat[0][0]); }}
- hrbust/哈理工oj 1126 Final Destination II【矩阵快速幂+递推】
- HRBUST 1126 Final Destination II (矩阵快速幂)
- hrbust 1430 哈理工oj 1430 神秘植物【递推+矩阵快速幂】
- 哈理工OJ 1124 Final Destination(简单递推)
- hrbust/哈理工oj 1592 六芒星【矩阵快速幂】
- HLG 1126 Final Destination II(快速幂,矩阵乘法)
- 哈理工hrbust OJ 2225 解题报告 【递推】
- hrbust/哈理工oj 1787 New Fibonacci Number【欧拉降幂+矩阵快速幂】
- Hust oj 1124 Final Destination(递推)
- hrbust 1430 神秘植物 [递推+矩阵快速幂]
- Hrbust 1401 九连环【递推+矩阵快速幂】
- hrbust 哈理工oj 1352 中间的球【floyd递推关系过】
- hrbust/哈理工oj 1660素数矩阵【预处理+思维】
- HLG 1126 Final Destination II
- hrbust 1375 The Active Leyni【暴力打表+递推+矩阵快速幂】
- hrbust 哈理工oj 1588 神医【贪心】
- hrbust 哈理工oj 网线【MST+Prim】
- 哈理工oj/hrbust 1790 武林【DP】
- 模板,新手读代码砸电脑的诱因
- ssh获取session
- 关于微信退款出现错误代码:52的解决方法
- 1012
- 只关心零件的Buider
- hrbust/哈理工oj 1126 Final Destination II【矩阵快速幂+递推】
- MaterialDrawer使用指南
- Android数据库greenDAO框架用法和源码分析
- jws,axis2,Xfire以及cxf对比
- LeetCode--223. Rectangle Area
- 拒绝装饰模式
- unity3D OnTriggerEnter和OnCollisionEnter的一点个人心得(主要讲区别)
- 【小镇的技术天梯】C语言,冒泡、快速、希尔、选择排序
- 触摸java常量池