Problem Description
Queues and Priority Queues are data structures which are known to most computer scientists. The Queue occurs often in our daily life. There are many people lined up at the lunch time. 
  Now we define that ‘f’ is short for female and ‘m’ is short for male. If the queue’s length is L, then there are 2L numbers of queues. For example, if L = 2, then they are ff, mm, fm, mf . If there exists a subqueue as fmf or fff, we call it O-queue else it is a E-queue.
Your task is to calculate the number of E-queues mod M with length L by writing a program.
 
Input
Input a length L (0 <= L <= 10 6) and M.
 
Output
Output K mod M(1 <= M <= 30) where K is the number of E-queues with length L.
 
Sample Input
3 8
4 7
4 8
 
Sample Output
6
2
1
 
题意:L个人排队,求不含fmf, fff这两种组合的总组合数对M求余的结果。
思路:用后向前看,f(n)为第n个人的取法总和:
   1.第n位为m  则前面的可以任意取 即为f(n-1)种取法
   2.第n位为f  第n-1位为f  则第n-2位只能是m  第n-3位也只能是m  第n-4位就可以任意取了  即为f(n-4)种取法
      3.第n位为f  第n-1位为m  则第n-2位只能为m  第n-3位就可以任意取  即为f(n-3)种取法
   可得出递推关系式:f(n) = f(n-1) + f(n-3) + f(n-4)
   可以直接用递推求解,不过差点超时,递推可以转化为矩阵的乘法
          

直接递推:

#include <iostream>
#include <stdio.h>
#include <cstring>
#include <algorithm>
using namespace std; int L, M; int solve()
{
int a[];
a[] = ; a[] = ;
a[] = ; a[] = ;
for (int i = ; i <= L; i++) {
a[i] = a[i-]+a[i-]+a[i-];
a[i] %= M;
}
return a[L];
} int main()
{
//freopen("1.txt", "r", stdin);
while (~scanf("%d%d", &L, &M)) {
printf("%d\n", solve());
} return ;
}

矩阵快速幂

#include <iostream>
#include <cstring>
#include <stdio.h>
#include <algorithm>
#include <math.h>
using namespace std;
#define LL long long
const int Max = ;
int L, M;
struct Mat
{
LL m[Max][Max]; void clear() {
memset(m, , sizeof(m));
} void Init() {
clear();
for (int i = ; i < Max; i++)
m[i][i] = ;
} }; Mat operator * (Mat a, Mat b)
{
Mat c;
c.clear();
for (int i = ; i < Max; i++)
for (int j = ; j < Max; j++)
for (int k = ; k < Max; k++) {
c.m[i][j] += (a.m[i][k]*b.m[k][j])%M;
c.m[i][j] %= M;
}
return c;
} Mat quickpow(Mat a, int k)
{
Mat ret;
ret.Init();
while (k) {
if (k & )
ret = ret*a;
a = a*a;
k >>= ;
}
return ret;
} int main()
{
//freopen("1.txt", "r", stdin);
Mat a, b, c;
a.clear(); b.clear(); c.clear();
a.m[][] = ; a.m[][] = ;
a.m[][] = ; a.m[][] = ; b.m[][] = b.m[][] = b.m[][] =
b.m[][] = b.m[][] = b.m[][] = ; while (~scanf("%d%d", &L, &M)) {
LL ret;
if (L == )
ret = ;
else if (L <= )
ret = a.m[-L][]%M;
else {
c = quickpow(b, L-);
c = c*a;
ret = c.m[][]%M;
}
printf("%lld\n", ret);
} return ;
}

[hdu 2604] Queuing 递推 矩阵快速幂的更多相关文章

  1. HDU 2604 Queuing(递推+矩阵)

    Queuing [题目链接]Queuing [题目类型]递推+矩阵 &题解: 这题想是早就想出来了,就坑在初始化那块,只把要用的初始化了没有把其他的赋值为0,调了3,4个小时 = = 本题是可 ...

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

    链接:传送门 题意:一个队列是由字母 f 和 m 组成的,队列长度为 L,那么这个队列的排列数为 2^L 现在定义一个E-queue,即队列排列中是不含有 fmf or fff ,然后问长度为L的E- ...

  3. hdu 2604 Queuing(动态规划—>矩阵快速幂,更通用的模版)

    题目 最早不会写,看了网上的分析,然后终于想明白了矩阵是怎么出来的了,哈哈哈哈. 因为边上的项目排列顺序不一样,所以写出来的矩阵形式也可能不一样,但是都是可以的 //愚钝的我不会写这题,然后百度了,照 ...

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

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

  5. hdu 2604 递推 矩阵快速幂

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

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

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

  7. HDU - 2604 Queuing(递推式+矩阵快速幂)

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

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

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

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

随机推荐

  1. C# 多线程参数传递

    之前使用多线程的时候,基本没有遇到过参数传递的情况,最近,接连遇到需要进行参数传递的多线程的使用.每次都要重新上网查一下,太麻烦了.为了方便以后的使用,就把经常参阅的网上资料记录下来. 原文地址如下: ...

  2. walle部署系统的使用

    在项目开发的时候要管理各种开发 测试 线上环境的代码 部署 回滚等操作 这里可以使用walle walle官网:http://www.walle-web.io/ 学习安装:https://blog.c ...

  3. DCloud-MUI:杂项

    ylbtech-DCloud-MUI:杂项 1.返回顶部   2.返回顶部   3.返回顶部   4.返回顶部   5.返回顶部     6.返回顶部   7.返回顶部   8.返回顶部   9.返回 ...

  4. mycat 新增分片和字符集

    执行 select * from travelrecord ,分析Debug日志,说明整个执行逻辑,包括连接获取,连接同步信息,数据合并,数据返回,连接释放 新增一个分片表 T_VOTE (ID,PR ...

  5. mybatis---demo1--(单表增删改查)----bai

    实体类: package com.etc.entity; public class News { private int id; private String title; private Strin ...

  6. 手机的RAM和ROM

    RAM是由英文Random Access Memory的首字母构成的,意为随机存储器,即在正常工作状态下可以往存储器中随时读写数据.根据存储单元工作原理的不同,RAM又可分为静态存储器(SRAM)和动 ...

  7. Solaris11.1网络配置(Fixed Network)

    Solaris11的网络配置与Solaris10有很大不同,Solaris11通过network configuration profiles(NCP)来管理网络配置. Solaris11网络配置分为 ...

  8. Java中Exception的种类

    通常来讲,Java中的异常会被分为三种: Error: 这种异常被设计成不被捕获,因为这种异常产生于JVM自身. Runtime Exception: 运行时异常往往与环境有关,编译时无法检查,并且可 ...

  9. jstl 判断 null

    <c:if test="${not empty object }"> ${object}不为空 </c:if>

  10. HotSpot JVM垃圾收集器

    HotSpot JVM收集器 上面有7中收集器,分为两块,上面为新生代收集器,下面是老年代收集器.如果两个收集器之间存在连线,就说明它们可以搭配使用. Serial(串行GC)收集器 Serial收集 ...