题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2604

递推式是百度的,主要是练习一下如何使用矩阵快速幂优化。

递推式:f(n)=f(n-1)+f(n-3)+f(n-4),其中f(0)=2, f(1)=4, f(2)=6, f(3)=9。

当n>4时候,需要通过这个关系来递推。

构造矩阵这种东西我以前一直认为是很玄学的,但是如果深入研究的话不难发现其实也有规律可循。这是一个齐次递推式,很好构造。

我们希望通过如下矩阵(1)得到矩阵(2)

| f(n) |      |f(n+)|
|f(n-)| | f(n) |
|f(n-)| |f(n-)|
|f(n-)| |f(n-)|
() ()

那么实际上,我们是通过一个转换矩阵把f(n),f(n-1),f(n-2)移动了一下位置,并且推出了f(n+1)。再考虑到矩阵乘法的特性,我们就能得到一个递推矩阵:

|   |
| |
| |
| |

我把第一行列为递推方程的系数,这样递推矩阵的第一列和目标矩阵的整列相乘即可得到递推的下一个结果。那剩下的怎么办呢?向下看会发现,按照行x列的特性,分别又列到了对应的位置。再根据矩阵乘法的性质就会很容易证明满足递推方程。

代码如下:

 #include <algorithm>
#include <iostream>
#include <iomanip>
#include <cstring>
#include <climits>
#include <complex>
#include <fstream>
#include <cassert>
#include <cstdio>
#include <bitset>
#include <vector>
#include <deque>
#include <queue>
#include <stack>
#include <ctime>
#include <set>
#include <map>
#include <cmath> using namespace std; const int maxn = ;
typedef struct Matrix {
int m[maxn][maxn];
int r;
int c;
Matrix(){
r = c = ;
memset(m, , sizeof(m));
}
} Matrix; Matrix mul(Matrix m1, Matrix m2, int mod) {
Matrix ans = Matrix();
ans.r = m1.r;
ans.c = m2.c;
for(int i = ; i <= m1.r; i++) {
for(int j = ; j <= m2.r; j++) {
for(int k = ; k <= m2.c; k++) {
if(m2.m[j][k] == ) continue;
ans.m[i][k] = ((ans.m[i][k] + m1.m[i][j] * m2.m[j][k] % mod) % mod) % mod;
}
}
}
return ans;
} Matrix quickmul(Matrix m, int n, int mod) {
Matrix ans = Matrix();
for(int i = ; i <= m.r; i++) {
ans.m[i][i] = ;
}
ans.r = m.r;
ans.c = m.c;
while(n) {
if(n & ) {
ans = mul(m, ans, mod);
}
m = mul(m, m, mod);
n >>= ;
}
return ans;
} int n, m; int main() {
// freopen("in", "r", stdin);
while(~scanf("%d %d", &n, &m)) {
Matrix p;
p.r = , p.c = ;
p.m[][] = ;
p.m[][] = ;
p.m[][] = ;
p.m[][] = ;
if(n <= ) {
printf("%d\n", p.m[-n+][] % m);
continue;
}
Matrix s;
s.r = s.c = ;
s.m[][] = ; s.m[][] = , s.m[][] = , s.m[][] = ;
s.m[][] = ; s.m[][] = , s.m[][] = , s.m[][] = ;
s.m[][] = ; s.m[][] = , s.m[][] = , s.m[][] = ;
s.m[][] = ; s.m[][] = , s.m[][] = , s.m[][] = ;
s = quickmul(s, n-, m);
int ans = ;
for(int i = ; i <= p.r; i++) {
ans = (ans + (p.m[i][] * s.m[][i]) % m) % m;
}
printf("%d\n", ans % m);
}
return ;
}

[HDOJ2604]Queuing(递推,矩阵快速幂)的更多相关文章

  1. [hdu 2604] Queuing 递推 矩阵快速幂

    Problem Description Queues and Priority Queues are data structures which are known to most computer ...

  2. HDU Queuing(递推+矩阵快速幂)

    Queuing Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Su ...

  3. hdu 2604 递推 矩阵快速幂

    HDU 2604 Queuing (递推+矩阵快速幂) 这位作者讲的不错,可以看看他的 #include <cstdio> #include <iostream> #inclu ...

  4. HDU 5950 Recursive sequence 【递推+矩阵快速幂】 (2016ACM/ICPC亚洲区沈阳站)

    Recursive sequence Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Other ...

  5. 【递推+矩阵快速幂】【HDU2604】【Queuing】

    Queuing Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total S ...

  6. HDU 2842 (递推+矩阵快速幂)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2842 题目大意:棒子上套环.第i个环能拿下的条件是:第i-1个环在棒子上,前i-2个环不在棒子上.每个 ...

  7. 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] 类似于斐波那契数列的递推式子吧, 但 ...

  8. HDU6030 Happy Necklace(递推+矩阵快速幂)

    传送门:点我 Little Q wants to buy a necklace for his girlfriend. Necklaces are single strings composed of ...

  9. 五校联考R1 Day1T3 平面图planar(递推 矩阵快速幂)

    题目链接 我们可以把棱柱拆成有\(n\)条高的矩形,尝试递推. 在计算的过程中,第\(i\)列(\(i\neq n\))只与\(i-1\)列有关,称\(i-1\)列的上面/下面为左上/左下,第\(i\ ...

  10. LightOJ 1244 - Tiles 猜递推+矩阵快速幂

    http://www.lightoj.com/volume_showproblem.php?problem=1244 题意:给出六种积木,不能旋转,翻转,问填充2XN的格子有几种方法.\(N < ...

随机推荐

  1. C# TcpListener的编程要点

    using System; using System.Collections.Generic; using System.Linq; using System.Net; using System.Ne ...

  2. 【转载】一淘技术专家王晓哲:Nginx_lua的测试及选择

    对于Web高性能服务器上的选择,这个是很多人头痛的问题.其实Apache.lighttpd.Nginx都用他们优点,在什么情况下我们如何去选择适合自己的Web高性能服务器,如何去搭建一个适合自己的架构 ...

  3. ASP.NET用户控件事件的定义和实践

    假定用户控件(UserControl.ascx)中包含按钮控件  AButton,希望实现按  Button  按钮时,包含该用户控件的页面可以接收到事件. UserControl.ascx.cs   ...

  4. 【转】CSS实现div的高度填满剩余空间

    转自:http://www.cnblogs.com/zhujl/archive/2012/03/20/2408976.html 高度自适应问题,我很抵触用js去解决,因为不好维护,也不够自然,但是纯用 ...

  5. allow_url_include和allow_url_fopen

    allow_url_fopen没什么好说的,主要是allow_url_include 从PHP5.2开始allow_url_include就默认为Off了,而allow_url_fopen一直是On的 ...

  6. Find the smallest number whose digits multiply to a given number n

    Given a number ‘n’, find the smallest number ‘p’ such that if we multiply all digits of ‘p’, we get ...

  7. CF444C DZY Loves Colors

    考试完之后打的第一场CF,异常惨烈呀,又只做出了一题了.A题呆滞的看了很久,领悟到了出题者的暗示,应该就是两个点的时候最大吧,不然的话这题肯定特别难敲,YY一发交上去然后就过了.然后就在不停地YY B ...

  8. Javascript Arguments,calle,caller,call,apply

    一.Arguments 该对象代表正在执行的函数和调用他的函数的参数. [function.]arguments[n] 参数function :选项.当前正在执行的 Function 对象的名字. n ...

  9. SQL Server 中的存储过程

    一:初步了解存储过程的使用 创建一个简单的存储过程 CREATE PROC spEmployee AS SELECT * FROM HumanResources.Employee; 执行这个存储过程: ...

  10. Java集合框架(二)

    Set Set:无序,不可以重复元素. |--------HashSet:数据结构是哈希表. 线程是非同步的.保证元素唯一性的原理是:判断元素的hashCode值是否相同,如果相同,还会继续判断元素的 ...