Queuing

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

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
 

题意

给定一个队伍长度k,和mod,求队伍有多少种排列可能。其中,队伍排列要求: 不能出现 fff 或者 fmf

解题思路

类似递推思路, 1.若最后一个为m,则无论前一个为什么情况都可以,sum+=dp[i-1]

若最后一个为f,则  {

             2. 若前一位为m,则再之前一位必定为m,此时队列为mmf,此时可同第一种情况,由mmf前一位决定,此时sum+=dp[i-3]

             3. 若前一位为f,则队伍要符合题意之前依旧只能是mm,原理同第二种情况,此时队列为mmff,由mmff前一位决定,此时sum+=dp[i-4]

          }

得递推式,f(n)=f(n-1)+f(n-3)+f(n-4);

之后直接矩阵快速幂即可

则  f(n)    1 0 1 1      f(n-1)

  f(n-1)    1 0 0 0   f(n-2)

  f(n-2)       0 1 0 0  f(n-3)

f(n-3)        0 0 1 0   f(n-4)

手推枚举前4项,得f(1)=2  f(2)=4 f(3)=6 f(4)=9

具体实现看代码吧

#include<bits/stdc++.h>
using namespace std;
const int maxn=;
typedef long long ll;
#define mod(x) ((x)%MOD)
ll MOD;
//2 4 6 9
struct mat
{
int m[maxn][maxn];
mat(){
memset(m,,sizeof(m));
}
}unit;
mat operator*(mat a,mat b)
{
mat ret;
ll x,n=;
for(int i=;i<n;i++)
{
for(int j=;j<n;j++)
{
x=;
for(int k=;k<n;k++)
{
x+=mod((ll)a.m[i][k]*b.m[k][j]);
}
ret.m[i][j]=mod(x);
}
}
return ret;
}
void iint()
{
for(int i=;i<maxn;i++)
{
unit.m[i][i]=;
}
return ;
}
mat pow1(mat a,ll n)
{
mat ret=unit;
while(n)
{
if(n&)
{
n--;ret=ret*a;
}
n>>=;
a=a*a;
}
return ret;
}
int main()
{
ll k;
mat b;
b.m[][]=; b.m[][]=; b.m[][]=; b.m[][]=;
//f(1)对于f(n)来说是f(n-4),这四项写反,查错了好久,哭
while(cin>>k>>MOD)
{
if(k<=) { cout<<b.m[-k][]%MOD<<endl;continue;}
iint(); //构建单位阵
mat a;
a.m[][]=a.m[][]=a.m[][]=a.m[][]=a.m[][]=a.m[][]=;
//a.m[0][1]=a.m[1][1]=a.m[1][2]=a.m[1][3]=a.m[2][0]=a.m[2][2]=a.m[2][3]=a.m[3][0]=a.m[3][1]=a.m[3][3]=0;
a=pow1(a,k-); //进行k-4次快速幂即可
a=a*b;
/*for(int i=0;i<4;i++)
{ //这是查看矩阵的= =
for(int j=0;j<4;j++)
{
if(j+1==4) cout<<a.m[i][j]<<endl;
else cout<<a.m[i][j]<<" ";
}
}*/
cout<<mod(a.m[][])<<endl;
}
return ;
}
 

HDU - 2604 Queuing(递推式+矩阵快速幂)的更多相关文章

  1. hdu 5950 Recursive sequence 递推式 矩阵快速幂

    题目链接 题意 给定\(c_0,c_1,求c_n(c_0,c_1,n\lt 2^{31})\),递推公式为 \[c_i=c_{i-1}+2c_{i-2}+i^4\] 思路 参考 将递推式改写\[\be ...

  2. HDU5950 Recursive sequence 非线性递推式 矩阵快速幂

    题目传送门 题目描述:给出一个数列的第一项和第二项,计算第n项. 递推式是 f(n)=f(n-1)+2*f(n-2)+n^4. 由于n很大,所以肯定是矩阵快速幂的题目,但是矩阵快速幂只能解决线性的问题 ...

  3. [题解][SHOI2013]超级跳马 动态规划/递推式/矩阵快速幂优化

    这道题... 让我见识了纪中的强大 这道题是来纪中第二天(7.2)做的,这么晚写题解是因为 我去学矩阵乘法啦啦啦啦啦对矩阵乘法一窍不通的童鞋戳链接啦 层层递推会TLE,正解矩阵快速幂 首先题意就是给你 ...

  4. HDU-6185-Covering(推递推式+矩阵快速幂)

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

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

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

  6. [HDOJ2604]Queuing(递推,矩阵快速幂)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2604 递推式是百度的,主要是练习一下如何使用矩阵快速幂优化. 递推式:f(n)=f(n-1)+f(n- ...

  7. hdu 6185 递推+【矩阵快速幂】

    <题目链接> <转载于 >>> > 题目大意: 让你用1*2规格的地毯去铺4*n规格的地面,告诉你n,问有多少种不同的方案使得地面恰好被铺满且地毯不重叠.答案 ...

  8. hihoCoder 1143 : 骨牌覆盖问题·一(递推,矩阵快速幂)

    [题目链接]:click here~~ 时间限制:10000ms 单点时限:1000ms 内存限制:256MB 描述 骨牌,一种古老的玩具.今天我们要研究的是骨牌的覆盖问题: 我们有一个2xN的长条形 ...

  9. [Lonlife1031]Bob and Alice are eating food(递推,矩阵快速幂)

    题目链接:http://www.ifrog.cc/acm/problem/1031 题意:6个水果中挑出n个,使得其中2个水果个数必须是偶数,问有多少种选择方法. 设中0代表偶数,1代表奇数.分别代表 ...

随机推荐

  1. spring学习 十 schema-based 前置后后置通知

    spring 提供了 2 种 AOP 实现方式:(1)Schema-based ,(2)AspectJ Schema-based:每个通知都需要实现接口或类,配置 spring 配置文件时在<a ...

  2. clion中资源文件以及头文件的引用

    首先在使用clion中没有将文件target就会出现下面的错误  在使用的时候可以默认一下  在以后的使用中如果不需要某个文件时  就可以在CMakeLis.txt文件把它删除掉 在代码界面的最上面出 ...

  3. oracle merge into语法

    oracle的merge into语法,在这种情况下: 基于某些字段,存在就更新,不存在就插入 不需要先去判断一下记录是否存在,直接使用merge into oerge into 语法: MERGE ...

  4. 2018.11.01 NOIP训练 树的排列(树形dp)

    传送门 跟这道题差不多. 只不过是让权值小的儿子做权值大的儿子的父亲而已. 代码

  5. Everything的简单使用

    1.Everythings下载地址: http://www.voidtools.com/ 下载完后直接解压,运行everything.exe即可打开使用: 2.基本设置 (1)去除不需要搜索的文件夹: ...

  6. 1.7.8使用return 停止线程

    package com.cky.thread; /** * Created by edison on 2017/12/3. */ public class MyThread12 extends Thr ...

  7. 实战--利用SVM对基因表达标本是否癌变的预测

    利用支持向量机对基因表达标本是否癌变的预测 As we mentioned earlier, gene expression analysis has a wide variety of applic ...

  8. PAT甲级 1122. Hamiltonian Cycle (25)

    1122. Hamiltonian Cycle (25) 时间限制 300 ms 内存限制 65536 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHEN, Yue The ...

  9. poj 2046&&poj1961KMP 前缀数组

    Power Strings Time Limit: 3000 MS Memory Limit: 65536 KB 64-bit integer IO format: %I64d , %I64u Jav ...

  10. delphi PosAnsi

    function ValidateName(n: string): string; var banned, res: string; i,j: integer; begin res:= n; bann ...