hdu 2604 - Queuing
来源:互联网 发布:用友软件公司地址 编辑:程序博客网 时间:2024/04/30 06:52
Queuing
Time Limit: 10000/5000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 928 Accepted Submission(s): 404
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
来自 <http://acm.hdu.edu.cn/game/entry/problem/show.php?chapterid=3§ionid=1&problemid=11>
大意:
排队是一件非常常见的事情,求在L人排出的队列中不包含fmf,fff子串的序列个数模M的值.
范围:L [ 0 ~10^6 ], M [ 1~ 30 ]
分析:
想求出第n个人时符合要求的序列总个数,可以通过第n-1个人后加一个人来求得。
1.最后一位添加m的情况:
由题可知,只要n-1个人符合要求,则添m也一定符合要求,既有F(n-1)种排法
2.最后一位添加f的情况:
要使最后一位加f也符合要求,则前两位不能是fm和ff且n-3之前的是符合要求的,既在n-3符合情况的条件下添加mf或mm得到n-1,又只要n-3符合条件,则后两位添加mm的定然也符合条件,有F(n-3)种可能。而在n-3后面添加mf要符合要求,只有n-3的最后一位是m才符合,既n-4位符合要求的后面加一个m有F(n-4)种情况。
综上:F(n) = F(n-1)+ F(n-3)+ F(n-4)。
接下来只要知道(a+b)% n == ( (a%n) + (b%n) ) % n 就可以ac这题了 ^_^
代码:
#include<iostream>
using namespace std;
const int MAX = 1000001;
char L[MAX][31];
int main()
{
for(int j = 1; j < 31; j++)
{
L[0][j] = 1 % j;
L[1][j] = 2 % j;
L[2][j] = 4 % j;
L[3][j] = 6 % j;
}
for(int i = 4; i < MAX; i++)
{
for(int j = 1; j < 31; j++)
{
L[i][j] = (L[i-1][j] + L[i-3][j] + L[i-4][j]) % j;
}
}
intl,m;
while(scanf("%d%d",&l,&m) != EOF)
{
printf("%d\n",L[l][m]);
}
return0;
}
- hdu 2604 Queuing
- hdu 2604 Queuing 矩阵
- hdu 2604 Queuing
- hdu 2604 queuing
- hdu 2604 - Queuing
- hdu 2604 Queuing
- HDU 2604-Queuing
- hdu 2604 Queuing 矩阵
- HDU 2604 Queuing
- HDU 2604 Queuing
- 【HDU 2604】 Queuing
- HDU 2604 Queuing
- HDU 2604 Queuing
- HDU 2604 Queuing
- HDU:2604 Queuing
- hdu 2604 Queuing
- hdu-2604-Queuing
- hdu 2604 Queuing(矩阵乘法)
- C++dynamic_cast
- adc调试
- HDU 1598 find the most comfortable road
- ios开发 通信录AddressBook.framework
- Android -- service 利用广播调用服务的方法
- hdu 2604 - Queuing
- 市级人防信息化管理系统登陆上传至服务器
- 【Cocos2dx游戏开发】Cocos2d-x简介
- 黑马程序员---枚举类
- javascript实现页面内关键词高亮
- Intent和PendingIntent的区别
- C#VS2010连接数据库大全
- 详细分析Java中断机制
- 三种布局实现上下回弹效果(普通布局,ListView,ScrollView)