首页 > 其他 > 详细

HDU 2604 Queuing

时间:2015-07-30 20:46:17      阅读:140      评论:0      收藏:0      [点我收藏+]
#include<cstdio>
#include<cstring>
using namespace std;
int m;
int f(int x)
{
    int s1[5][5],s2[5][5],s3[5][5],i,a=0,j,k,b=0,c=0,d=0;
    memset(s1,0,sizeof(s1));
    memset(s2,0,sizeof(s2));
    s1[0][1]=1;s2[0][1]=1;
    s1[1][3]=1;s2[1][3]=1;
    s1[2][0]=1;s2[2][0]=1;
    s1[2][1]=1;s2[2][1]=1;
    s1[3][2]=1;s2[3][2]=1;
    s1[3][3]=1;s2[3][3]=1;
   while (x)
   {
          if (x&1)
          {
             memset(s3,0,sizeof(s3));
             for (i=0;i<4;i++)
             for (j=0;j<4;j++)
             for (k=0;k<4;k++)
             s3[i][j]+=s1[i][k]*s2[k][j];
             for (i=0;i<4;i++)
             for (j=0;j<4;j++)
               s2[i][j]=s3[i][j]%m;
        }
        memset(s3,0,sizeof(s3));
        for (i=0;i<4;i++)
        for (j=0;j<4;j++)
        for (k=0;k<4;k++)
        s3[i][j]+=s1[i][k]*s1[k][j];
        for (i=0;i<4;i++)
        for (j=0;j<4;j++)
        s1[i][j]=s3[i][j]%m;
        x>>=1;
    }
    for (i=0;i<4;i++)
    {
        a+=s2[i][0];
        b+=s2[i][1];
        c+=s2[i][2];
        d+=s2[i][3];
    }
    return a+b+c+d;
}
int main()
{
    int l,p;
    while (~scanf("%d%d",&l,&m))
    {
        if (l==0) {printf("0\n");continue;}
        if (l==1) {printf("%d\n",2%m);continue;}
        if (l==2) {printf("%d\n",4%m);continue;}
        l-=3;
        p=f(l)%m;
        printf("%d\n",p);
    }
    return 0;
}

 

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 2 L 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
 
       题意:L个人排队,f为男生,m为女生,队伍中没有fff和fmf的队伍有多少种。
       这是递推题,由于数据比较大,所以要先找到递推公式,再用矩阵快速幂做。
       ff[i]表示队伍长度为i时,队伍第i个为f,第i-1个也为f。fm[i],mf[i],mm[i]就不解释了。ff[i]=mf[i-1],因为在mf[i-1]后面加f就是mff。ff[i]!=ff[i-1].因为在f[i-1后面加f就是fff,不合法。同理fm[i]=ff[i-1]+mf[i-1],mf[i]=mm[i-1],mm[i]=fm[i-1]+mm[i-1];下面构造矩阵
                                                            0 1 0 0
(ff[i-1] fm[i-1]  mf[i-1]   mm[i-1]        *    0 0 0 1             =(ff[i]  fm[i]  mf[i]  mm[i])
                                                            1 1 0 0
                                                             0 0 1 1
 
 
 
 
 
 

HDU 2604 Queuing

原文:http://www.cnblogs.com/pblr/p/4690278.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!