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. 《团队-爬取豆瓣电影TOP250-设计文档》

    搭建环境: 1.安装python3.4 2.安装pycharm集成开发环境 3.安装Git for Windows 4.安装python第三方包 bs4开发阶段: 1.团队成员申请并配置github账 ...

  2. python学习之ansible api

    Python API 2.0 从2.0的事情开始更复杂一些,但是你会得到更多离散和可读的类: #!/usr/bin/env python import json from collections im ...

  3. mysql 表锁进程非常多的情况

    今天要说的是mysql 的 MYISAM引擎下的表锁问题. 通常来说,在MyISAM里读写操作是串行的,但当对同一个表进行查询和插入操作时,为了降低锁竞争的频率,根据concurrent_insert ...

  4. 2019.01.20 bzoj2238: Mst(kruskal+树链剖分)

    传送门 树链剖分菜题. 题意简述:给一个无向图,边有边权,每次询问删一条边(对后面的询问无影响)之后的最小生成树. 思路: 先跑一次kruskalkruskalkruskal并把跑出来的最小生成树给链 ...

  5. react添加方法的两种形式

    1.使用bind <button onClick={this.test.bind(this)}>确定</button> 2.使用箭头函数 <button onClick= ...

  6. java常用设计模式八:代理模式

    一.概述 代理模式是指客户端并不直接调用实际的对象,而是通过调用代理,来间接的调用实际的对象. 其特征是代理类与委托类有同样的接口,真正的核心业务逻辑还是在实际对象里面. 二.为什么要使用代理模式 当 ...

  7. CString int转换

    1.CString 转 int      CString strtemp = "100";    int  intResult;    intResult= atoi(strtem ...

  8. ERR_CACHE_MISS 上一页提示确认重新提交表单

    SITUATION: 设备搜索后,根据返回结果 list.php 进入特定设备的详细页面 one.php,但点击后退按钮(上一页)返回 list.php,会出现确认重新提交表单的错误页面 ERR_CA ...

  9. BT1120时序,可以用于自测用

    module bt1120_gen #( , , , , , )( input clk, input rst_p, // input [5:0] h_sync_pixcels, // input [5 ...

  10. 2018-01-13 view绘制流程-activity启动流程-window-decorView-ViewRootImpl关系

    1.activity启动流程: https://www.jianshu.com/p/927ca995bca6 http://blog.csdn.net/qian520ao/article/detail ...