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. CMDB 配置管理数据库

  2. go基础知识之变量,类型,常量,函数

    3 变量 变量是什么 变量指定了某存储单元(Memory Location)的名称,该存储单元会存储特定类型的值.在 Go 中,有多种语法用于声明变量. 声明单个变量 var name type 是声 ...

  3. java web实践

    语言:java.javascript 软件:eclipse.mysql 环境配置:下载jdk:配置jdk环境变量.相关教程:https://jingyan.baidu.com/article/db55 ...

  4. 【转】PHP实现验证码

    转自http://www.jb51.net/article/40341.htm 新建一个captcha.php: //验证码类class Captcha { private $charset = 'a ...

  5. Oracle常用命令-用户、表空间、赋权限、导入导出

    1.1   删除表空间 drop tablespace QBKJ including contents and datafiles; 1.2   删除用户 drop user admin cascad ...

  6. 1.The Necessity of a Broad Education 全面教育的必要性

    1.The Necessity of a Broad Education 全面教育的必要性 (1) According to a survey,which was based on the respo ...

  7. ArcMap等值面

    先说一下题目,ArcMap中没有由栅格直接生成等值面的功能,但由栅格直接生成等值线的功能存在,可通过如下方式得到等值面: 1.提取等值线 由dem直接提取等值线:Spatial Analyst Too ...

  8. Application-identifier entitlement does not match问题的解决

    以下是一个老外的回答: This happened to me after installing a build from TestFlight and overwriting it with the ...

  9. android:padding和android:margin的区别[转]

    本文综合了:http://zhujiao.iteye.com/blog/1856980 和 http://blog.csdn.net/maikol/article/details/6048647 两篇 ...

  10. WP8整合Bing应用,生活有求Bing

    在Windows 8中,Bing应用一直随系统而存在,提供多样化的资讯.它们是我的“御用”App,因为可以根据我的使用习惯对应用进行定制. 在Windows Phone 8系统第三次官方更新之后, B ...