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

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.
4 7
4 8
2
1
//矩阵快速幂解递推式: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的更多相关文章
- hdu---(2604)Queuing(矩阵快速幂)
Queuing Time Limit: 10000/5000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Su ...
- 【递推+矩阵快速幂】【HDU2604】【Queuing】
Queuing Time Limit: 10000/5000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Total S ...
- Message Queuing(MSMQ)
一.前言 MicroSoft Message Queuing(微软消息队列)是在多个不同的应用之间实现相互通信的一种异步传输模式,相互通信的应用可以分布于同一台机器上,也可以分布于相连的网络空间中的任 ...
- Queuing(以前写的没整理)
Queuing Time Limit: 10000/5000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Total Subm ...
- 排队时延(Queuing delay)
网络时延的构成 Network delay including four parts: Processing delay - time routers take to process the pack ...
- HTB Linux queuing discipline manual - user guide笔记
1. Introduction HTB is meant as a more understandable, intuitive and faster replacement for the CBQ ...
- HDU 2604 Queuing(矩阵快速幂)
题目链接:Queuing 题意:有一支$2^L$长度的队伍,队伍中有female和male,求$2^L$长度的队伍中除 fmf 和 fff 的队列有多少. 题解:先推导递推式:$f[i]=f[i-1] ...
- HDU 2604 Queuing(递推+矩阵)
Queuing [题目链接]Queuing [题目类型]递推+矩阵 &题解: 这题想是早就想出来了,就坑在初始化那块,只把要用的初始化了没有把其他的赋值为0,调了3,4个小时 = = 本题是可 ...
- HDU - 2604 Queuing(递推式+矩阵快速幂)
Queuing Time Limit: 10000/5000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Su ...
- HDU 2604 Queuing 矩阵高速幂
Queuing Time Limit: 10000/5000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Total S ...
随机推荐
- [Effective JavaScript笔记]第3条:当心隐式的强制转换
js对类型错误出奇的宽容 3+true; //4 3*””; //0 3+[]; //3 3+[3]; //33 以上表达式在许多语言早就变红了.而js不但不报错还给你个结果. 极少情况会产生即时 ...
- ubuntu下git输出的颜色变化
(这些文章都是从我的个人主页上粘贴过来的,大家也可以访问我的主页 www.iwangzheng.com) 11点进家门,感觉很温暖哦. 以下是如何在用git的时候清晰的看出关键字的方法. $ vim ...
- js检测是否安装了flash插件
function flashChecker() { var hasFlash = 0; //是否安装了flash var flashVersion = 0; //flash版本 var isIE = ...
- 数据库多张表导出到excel
数据库多张表导出到excel public static void export() throws Exception{ //声明需要导出的数据库 String dbName = "hdcl ...
- [转载]WiFi有死角? 巧用旧无线路由器扩展覆盖
怎么了,家里的WiFi有死角?老旧无线路由器的无线覆盖不给力?现在大功率无线产品或双频无线产品的售价并不便宜,而且仅靠一台无线路由器并不能满足多户型家庭的无线覆盖需求.那么,是不是有什么廉价而又实用的 ...
- c++ exports def文件
https://msdn.microsoft.com/zh-cn/library/hyx1zcd3(v=vs.80).aspx EXPORTS 引入了一个由一个或多个 definitions(导出的函 ...
- RAID阵列的初始化与管理
如果我们创建RAID阵列的目的是新部署一台服务器,我们建议所有新创建的RAID阵列都应该做初始化操作,这样,硬盘上原有的用户数据将被清除,以便进行后续的系统,软件安装. 转自: http://zh.c ...
- Largest Divisible Subset
Given a set of distinct positive integers, find the largest subset such that every pair (Si, Sj) of ...
- xml 解析 java 基础复习
document 解析 sax 解析 dom4j 解析(摘自csdn redarmychen) dom4j是一个Java的XML API,类似于jdom,用来读写XML文件的.dom4j是一个非常 ...
- Android app主线程UI更新间歇性崩溃的问题
对App进行开发测试时,偶尔出现app崩溃的问题.日志如下: 10-25 18:44:52.935 15290-15290/com.zzq.cnblogs E/AndroidRuntime﹕ FATA ...