Queuing

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

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
 
Author
WhereIsHeroFrom
 
Source
 
Recommend
lcy
 

首先,这是一个递推问题。mm结尾的只能由fm结尾的或者mm结尾的推来。以mf结尾的只能由mm结尾的推来,以fm结尾的只能由mf或者ff推来,以ff结尾的只能由mf推来。

F(n)=F(n-1)+F(n-3)+F(n-4)。可是正常用大数的话会TLE。后来查阅DISCUSS得知应该使用矩阵乘法。

(設f(n)為字符串為n時符合條件的字符串個數。
以字符串最後一個字符為分界點,當最後一個字符為m時前n-1個字符沒有限制,即為f(n-1);
當最後一個字符為f時就必須去除最後3個字符是fmf和fff的情況,此時最後三個字符可能為mmf和mff,
當後三個字符為mmf時,前n-3個字符沒有限制,即為f(n-3);
但是當後三個自負為mff時,後四個字符必須為mmff時前n-4個字符無限制,即為f(n-4)。
這樣就討論完了字符串的構成情況了,得出結論為:f(n) = f(n-1) + f(n-3) + f(n-4).   )

其中1——4是已知的。

其中这个A矩阵是要构建的,一般是通过0-1阵,达到下图的目的,构阵方式不唯一。

#include<iostream>
#include<cstdio>
#include<cstring> using namespace std; int n,mod; struct Matrix{
int arr[][];
}; Matrix unit,init; Matrix Mul(Matrix a,Matrix b){
Matrix c;
for(int i=;i<;i++)
for(int j=;j<;j++){
c.arr[i][j]=;
for(int k=;k<;k++)
c.arr[i][j]=(c.arr[i][j]+a.arr[i][k]*b.arr[k][j]%mod)%mod;
c.arr[i][j]%=mod;
}
return c;
} Matrix Pow(Matrix a,Matrix b,int k){
while(k){
if(k&){
b=Mul(b,a);
}
a=Mul(a,a);
k>>=;
}
return b;
} void Init(){
for(int i=;i<;i++)
for(int j=;j<;j++){
init.arr[i][j]=;
unit.arr[i][j]=;
}
unit.arr[][]=, unit.arr[][]=, unit.arr[][]=, unit.arr[][]=; init.arr[][]=, init.arr[][]=, init.arr[][]=, init.arr[][]=,
init.arr[][]=, init.arr[][]=;
} int main(){ //freopen("input.txt","r",stdin); Init();
while(~scanf("%d%d",&n,&mod)){
if(n<=){
if(n==)
printf("");
else if(n==)
printf("%d\n",%mod);
else if(n==)
printf("%d\n",%mod);
else if(n==)
printf("%d\n",%mod);
else if(n==)
printf("%d\n",%mod);
continue;
}
Matrix res=Pow(init,unit,n-);
printf("%d\n",res.arr[][]%mod);
}
return ;
}

最新文章

  1. Yahoo14条军规-前端性能优化
  2. MySQL 常用的sql语句小结(待续)
  3. Maven基础知识(转)
  4. html5头部说明
  5. Shell脚本编程初体验
  6. JAVA 23种设计模式(转)
  7. MYSQL基础笔记(五)- 练习作业:站点统计练习
  8. 控制台程序使用MFC类的方法
  9. PHP内核探索之变量(1)变量的容器-Zval
  10. Storm流分组介绍
  11. 【转】关于Activity和Task的设计思路和方法
  12. Android编程中的实用快捷键
  13. [WC2006]水管局长(LCT)
  14. Shell中字符串的切割、拼接、比较、替换
  15. LeetCode: Gray Code [089]
  16. 关于字符串split一些用法
  17. 【Unity笔记】提示框ToolTips大小自适应,及其闪烁的问题
  18. MVC控制器传递多个Model到视图,使用ViewData, ViewBag, 部分视图, TempData, ViewModel, Tuple
  19. Python 循环退出
  20. 235.236. Lowest Common Ancestor of a Binary (Search) Tree -- 最近公共祖先

热门文章

  1. 【BZOJ】【1178】【APIO2009】convention会议中心
  2. Java集合框架2
  3. Miscellaneos:版本控制、SVN、VSS
  4. CentOS 7 开放防火墙端口命令
  5. eclipse无法解析导入 java.util
  6. 前端要给力之:URL应该有多长?
  7. .Net垃圾收集机制—了解算法与代龄
  8. 【Python】Django数据模型、级联删除、级联更新、ER图导出等
  9. cognos report在做同比时遇到的问题解决方法
  10. Sql server management studio: cannot find one or more components