现在的位置: 首页 > 综合 > 正文

HDU 2604 Queuing 递推

2017年11月22日 ⁄ 综合 ⁄ 共 1532字 ⁄ 字号 评论关闭

Queuing

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

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
/* 
HDU 2604 递推 
mf任意组合 fmf、fff不合法 
前四位我们可以算出d[1]=2,d[2]=4,
d[3]=6:3位,每位2个选择,2^3=8 两种不合法的 8-2=6 
d[4]=9. 16种 
合法的: ffmm fmmf fmmm mffm mfmm mmff mmfm mmmf mmmm 
不合法:ffff fffm ffmf mfff mfmf fmff fmfm 

总结下合法串:按结尾 
mffm mmfm  
mfmm ffmm
fmmm mmmm
mmmf fmmf
mmff 
一个合法串可以由两个较短的合法串组成
就以d[n]为例:主要注意不能重复 
1、n-1个字符的时候: +m
2、n-2: 只能+mm,会和n-1重复,所以不考虑n-2
3、n-3: +mmf
4、n-4: +mmff
5、n-5: 如果是+mmffm,会与n-1重复,+mmmff会与n-4重复,+mmmmf会与n-3重复(不考虑)

dp[n]=dp[n-1]+dp[n-3]+dp[n-4]

n<10^6  m<=30 用打表可以做  
[刚刚开始直接用int敲的代码,果断MLE了。。。改成char试试 - -就过了]
*/

#include <iostream>
using namespace std;
#define N 1000001
#define M 30

char node[N][M];//二维数组表示的是数n mod m; 
void init()
{
    int i,j;
    for(i=1;i<=M;i++)
    {
        node[1][i]=2%i;
        node[2][i]=4%i;
        node[3][i]=6%i;
        node[4][i]=9%i;
        for (j=5;j<=N;j++)
        {
            node[j][i]=(node[j-1][i]+node[j-3][i]+node[j-4][i])%i;
        }
    }
}

int main()
{
    init();
    int n,m;
    while (~scanf("%d%d",&n,&m))
    {
        printf("%d\n",node[n][m]);
    }
    return 0;
}

抱歉!评论已关闭.