Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 3141    Accepted Submission(s): 1431

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
 
Author
WhereIsHeroFrom
 
Source

//矩阵快速幂解递推式:f(n)=f(n-1)+f(n-2)+f(n-3)
#include<cstdio>
#include<cstring>
using namespace std;
struct node{
int mat[4][4];
};
int L,M;
const int n=4;
node mat_multi(node a,node b)
{
node c;
int i,j,k;
memset(c.mat,0,sizeof(c.mat));
for(i=0;i<n;i++)
for(j=0;j<n;j++)
for(k=0;k<n;k++)
{
c.mat[i][j]+=a.mat[i][k]*b.mat[k][j];
c.mat[i][j]%=M;
}
return c;
}
node pow_mod(node a,int t)
{
node c;
int i;
memset(c.mat,0,sizeof(c.mat));
for(i=0;i<n;i++)
c.mat[i][i]=1;
for(;t>0;t>>=1)
{
if(t&1) c=mat_multi(a,c);
a=mat_multi(a,a);
}
return c;
}
int main()
{
int i,t;
int b[4]={9,6,4,2};
node c;
while(~scanf("%d%d",&L,&M))
{
c.mat[0][0]=1;
c.mat[0][1]=0;
c.mat[0][2]=1;
c.mat[0][3]=1; c.mat[1][0]=1;
c.mat[1][1]=0;
c.mat[1][2]=0;
c.mat[1][3]=0; c.mat[2][0]=0;
c.mat[2][1]=1;
c.mat[2][2]=0;
c.mat[2][3]=0; c.mat[3][0]=0;
c.mat[3][1]=0;
c.mat[3][2]=1;
c.mat[3][3]=0;
int ans=0;
if(L==0) ans=0;
else if(L==1) ans=2;
else if(L==2) ans=4;
else if(L==3) ans=6;
else if(L==4) ans=9;
else{
t=L-4;
c=pow_mod(c,t); for(i=0;i<4;i++)//构造矩阵的(L-4)次幂后 再乘以前4项就是结果
ans+=c.mat[0][i]*b[i];
}
printf("%d\n",ans%M);
}
}

  公式是从网上找的。。。。

Queuing的更多相关文章

  1. hdu---(2604)Queuing(矩阵快速幂)

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

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

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

  3. Message Queuing(MSMQ)

    一.前言 MicroSoft Message Queuing(微软消息队列)是在多个不同的应用之间实现相互通信的一种异步传输模式,相互通信的应用可以分布于同一台机器上,也可以分布于相连的网络空间中的任 ...

  4. Queuing(以前写的没整理)

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

  5. 排队时延(Queuing delay)

    网络时延的构成 Network delay including four parts: Processing delay - time routers take to process the pack ...

  6. HTB Linux queuing discipline manual - user guide笔记

    1. Introduction HTB is meant as a more understandable, intuitive and faster replacement for the CBQ ...

  7. HDU 2604 Queuing(矩阵快速幂)

    题目链接:Queuing 题意:有一支$2^L$长度的队伍,队伍中有female和male,求$2^L$长度的队伍中除 fmf 和 fff 的队列有多少. 题解:先推导递推式:$f[i]=f[i-1] ...

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

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

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

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

  10. HDU 2604 Queuing 矩阵高速幂

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

随机推荐

  1. 1009: josephus问题

    1009: josephus问题 Time Limit: 1 Sec  Memory Limit: 64 MBSubmit: 549  Solved: 227 Description josephus ...

  2. CKEditor使用笔记

    相关资源 1. 首页地址:http://ckeditor.com/ 2. 下载地址:http://ckeditor.com/download 3. SDK地址:http://sdk.ckeditor. ...

  3. BestCoder Round #60 1002

    Problem Description You are given two numbers NNN and MMM. Every step you can get a new NNN in the w ...

  4. 浅谈B树

    B树即二叉搜索树: 1.所有非叶子结点至多拥有两个儿子(Left和Right): 2.所有结点存储一个关键字: 3.非叶子结点的左指针指向小于其关键字的子树,右指针指向大于其关键字的子树: 如:    ...

  5. Linux 怎么查看服务的启动进程所占用的目录

    lsof简介 lsof(list open files)是一个列出当前系统打开文件的工具.在linux环境下,任何事物都以文件的形式存在,通过文件不仅仅可以访问常规数据,还可以访问网络连接和硬件.所以 ...

  6. Linux MySQL差异备份技巧

    MSSQL差异备份使用技巧 15 Apr 2013 所谓的差异备份,就是只备份最近一次备份之后到此次备份之前所增加的那一部分数据.打个比方我第N次备份后数据库存放的内容是ABCD,然后我第N+1次 备 ...

  7. top对僵尸进程的处理

    ps        --forest        ASCII art process tree 2 怎样来清除僵尸进程:    1.改写父进程,在子进程死后要为它收尸.具体做法是接管SIGCHLD信 ...

  8. 【Network】一张图看懂 Reactor 与 Proactor 模型的区别

    首先来看看Reactor模式,Reactor模式应用于同步I/O的场景.我们以读操作为例来看看Reactor中的具体步骤:读取操作:1. 应用程序注册读就需事件和相关联的事件处理器2. 事件分离器等待 ...

  9. java类的封装 继承 多态

    1.猜数字小游戏 package cn.jiemoxiaodi_02; import java.util.Scanner; /** * 猜数字小游戏 * * @author huli * */ pub ...

  10. mysql工具

    MySQL工具汇总 收录了MySQL相关有用的工具.工具包含:性能测试,状态分析,SQL路由等方面 本文汇总了和MySQL运维开发相关的所有工具,并会持续更新 工具套件集 percona-toolki ...