zoukankan      html  css  js  c++  java
  • HDU 2604 Queuing

     

    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
     
            还有一种关系是a[i]=a[i-1]+a[i-3]+a[i-4],a[i]表示长度为i时合法的种数,为什么这样,我就懒得推了。
     
     
     
     
    第一种
     
     
     
    #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
    ");continue;}
            if (l==1) {printf("%d
    ",2%m);continue;}
            if (l==2) {printf("%d
    ",4%m);continue;}
            l-=3;
            p=f(l)%m;
            printf("%d
    ",p);
        }
        return 0;
    }
    

    第二种

    #include<cstdio>
    #include<cstring>
    using namespace std;
    int m,a[10];
    int s1[5][5],s2[5][5],s3[5][5];
    int f(int x)
    {
        int i,j,k;
        x-=4;
        memset(s1,0,sizeof(s1));
        s1[0][3]=1;
        s1[1][0]=1;
        s1[1][3]=1;
        s1[2][1]=1;
        s1[3][2]=1;
        s1[3][3]=1;
        for (i=0;i<4;i++)
        for (j=0;j<4;j++)
        {
            if (i==j) s2[i][j]=1;
            else s2[i][j]=0;
        }
        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])%m;
                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])%m;
            for (i=0;i<4;i++)
            for (j=0;j<4;j++)
                s1[i][j]=s3[i][j]%m;
            x>>=1;
        }
        return a[1]*s2[0][3]+a[2]*s2[1][3]+s2[2][3]*a[3]+a[4]*s2[3][3];
    }
    int main()
    {
        int l,p;
        while (~scanf("%d%d",&l,&m))
        {
            a[1]=2;a[2]=4;a[3]=6;a[4]=9;
            if (l<=4) {printf("%d
    ",a[l]%m);continue;}
            p=f(l);
            printf("%d
    ",p%m);
        }
        return 0;
    }
    
     
  • 相关阅读:
    曾经写的一些文章,与技术无关,整理出来怀旧,:)
    在VS.NET2003中使用XHTML的插件HTML TIDY 及 MindManger
    把机器退出了域,造成无法启动 MSSQLSERVER ,晕
    hello php!
    今天又看了一下存储过程
    [转]PAGEII携手极速网爱情电影经典对白
    关键词:2005年,世乒赛.上海,乒乓的胜地
    一本SharePoint方面的书,Special Edition Using Microsoft® SharePoint Portal Server
    keo计划
    关于xp_cmdshell 。。注意安全!
  • 原文地址:https://www.cnblogs.com/pblr/p/4690278.html
Copyright © 2011-2022 走看看