• Queuing(以前写的没整理)


    Queuing

    Time Limit: 10000/5000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
    Total Submission(s): 62 Accepted Submission(s): 46
     
    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
    HDU 1st “Vegetable-Birds Cup” Programming Open Contest
     
    Recommend
    lcy
     
    /*
    以前做的没整理的
    */
    #include<cstdio>
    using namespace std;
    
    int n,m;
    int f[5]={0,2,4,6,9};
    int a[4][4],b[4][4],c[4][4];
    
    void multi(int (*x)[4],int (*y)[4])
    {
      int i,j,k;
      for(i=0;i<4;i++)
        for(j=0;j<4;j++)
          for(c[i][j]=k=0;k<4;k++)
            c[i][j]+=x[i][k]*y[k][j];
      for(i=0;i<4;i++)
        for(j=0;j<4;j++)
          x[i][j]=c[i][j]%m;
    }
    
    int main()
    {
      //freopen("1.in","r",stdin);  
      
      int i,j;
      while(scanf("%d%d",&n,&m)==2)
        {
          if(n<=4){printf("%d
    ",f[n]%m);continue;}
          a[0][0]=a[0][2]=a[0][3]=1,a[0][1]=0;
          a[1][0]=1,a[1][1]=a[1][2]=a[1][3]=0;
          a[2][0]=a[2][2]=a[2][3]=0,a[2][1]=1;
          a[3][0]=a[3][1]=a[3][3]=0,a[3][2]=1;
          
          for(i=0;i<4;i++)
            for(j=0;j<4;j++)
              b[i][j]=(i==j);
          for(n-=4;n;multi(a,a),n>>=1)if(n&1)multi(b,a);
          for(j=0,i=0;i<4;i++)j+=b[0][i]*f[4-i];
          printf("%d
    ",j%m);
        }
      return 0;
    }
  • 相关阅读:
    斯坦福ML公开课笔记15—隐含语义索引、神秘值分解、独立成分分析
    [cocos2d-x]屏幕自适应解决的方法
    【机器学习实验】学习Python来分类现实世界的数据
    按花生酱,赞不绝口——敏捷12准则的敏捷解释
    linux命令笔记之ls
    HTML5 Canvas动画效果实现原理
    互联网+时代的人生必修课—逆商
    WebService的相关使用
    将一个链表中的结点依照奇偶分成两个链表
    web开发性能优化---用户体验篇
  • 原文地址:https://www.cnblogs.com/wuwangchuxin0924/p/6404118.html
Copyright © 2020-2023  润新知