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 ;
}
 

最新文章

  1. WPF中Grid实现网格,表格样式通用类
  2. PyCharm断点调试django
  3. jQuery.unique引发一个血案
  4. dom4j读取某个元素的某个属性
  5. jquery删除数组中重复元素
  6. iOS开发---集成ShareSDK实现第三方登录、分享、关注等功能。
  7. readonly与const
  8. ORA-01810: 格式代码出现两次
  9. spring定时任务的配置
  10. 构建微服务-使用OAuth 2.0保护API接口
  11. linux如何自动获取ip地址
  12. JDK,JRE,JVM的区别与联系
  13. Redis之持久化(RDB AOF)
  14. [转] 2016 JavaScript 发展现状大调查
  15. Alienware 15 R3 装Ubuntu 和 win10 双系统
  16. NSNotification相关
  17. PAT甲题题解-1043. Is It a Binary Search Tree (25)-二叉搜索树
  18. 微信公众平台开发 - 动手篇。使用weinxinFundation开始一个微信公众平台的开发
  19. SpringMVC由浅入深day01_2springmvc入门程序
  20. 查看docker容器的IP地址

热门文章

  1. 使用Simple MvvmToolkit开发Android和iOS程序
  2. centos7 hdfs yarn spark 搭建笔记
  3. centos 7 安装svn客户端
  4. 2018.11.01 洛谷P3953 逛公园(最短路+dp)
  5. join和split 的使用
  6. boost--ref
  7. struts上传文件报argument type mismatch错误
  8. c++关键字extern的作用
  9. linux上安装rabbitMQ
  10. 1101 Quick Sort