zoukankan      html  css  js  c++  java
  • 9.3.2 Queuing

    Queuing

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

    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

    思路:矩阵乘法,转移就行了

    用 f->1 m->0

    an 表示 mm

    bn 表示 mf

    cn 表示 fm

    dn 表示 ff

    那么很明显 an=cn-1+an-1

    bn=an-1 因为不能由 cn-1 fm->fmf

    cn=bn-1+dn

    dn=bn;

    构造出矩阵即可!

      1 /*
      2 Author:wuhuajun
      3 */
      4 #include <cmath>
      5 #include <cstdio>
      6 #include <algorithm>
      7 #include <cstring>
      8 #include <string>
      9 #include <cstdlib>
     10 using namespace std;
     11 
     12 typedef long long ll;
     13 typedef double dd;
     14 const int maxn=210;
     15 
     16 struct matrix
     17 {
     18     ll m[5][5];
     19 } c,base,ans;
     20 ll ans1,mod,r,t;
     21 int n,l;
     22 
     23 void close()
     24 {
     25 exit(0);
     26 }
     27 
     28 matrix mul(matrix a,matrix b)
     29 {
     30     memset(c.m,0,sizeof(c.m));
     31     for (int i=1;i<=4;i++)
     32         for (int j=1;j<=4;j++)
     33             for (int k=1;k<=4;k++)
     34             {
     35                 c.m[i][j]+=a.m[i][k]*b.m[k][j];
     36                 c.m[i][j] %= mod;
     37             }
     38     return c;
     39 }
     40 /*
     41 void print(matrix a)
     42 {
     43     for (int i=1;i<=4;i++)
     44     {
     45         for (int j=1;j<=4;j++)
     46             printf("%lld ",a.m[i][j]);
     47         puts("");
     48     }
     49 }
     50 */
     51 void ass(int x,int y,matrix &base)
     52 {
     53     base.m[x][y]=1;
     54 }
     55 
     56 void work()
     57 {
     58     memset(base.m,0,sizeof(base.m));
     59     ass(1,2,base);ass(1,3,base);
     60     ass(2,4,base);
     61     ass(3,2,base);
     62     ass(4,1,base);ass(4,4,base);
     63     memset(ans.m,0,sizeof(ans.m));
     64     for (int i=1;i<=4;i++)
     65         ans.m[i][i]=1;
     66     n=l; n-=2;
     67     while (n!=0)
     68     {
     69         if (n & 1)
     70             ans=mul(base,ans);
     71         n/=2;
     72         base=mul(base,base);
     73     }
     74     for (int i=1;i<=4;i++)
     75         for (int j=1;j<=4;j++)
     76             ans1+=ans.m[i][j];
     77     /*
     78     t=0;
     79     for (int i=1;i<=4;i++)
     80         t+=ans.m[6][i];
     81     t %= mod;
     82     ans1-=t;
     83     for (int i=1;i<=5;i++)
     84         ans1+=ans.m[5][i];
     85         */
     86 }
     87 
     88 void init()
     89 {
     90     while (scanf("%d %I64d",&l,&mod)!=EOF)
     91     {
     92         ans1=0;
     93         if (n==1 && n==2)
     94             ans1=0;
     95         /*
     96         else
     97         {
     98             while (n!=0)
     99             {
    100                 if (n & 1)
    101                     ans1=(ans1 * r) % mod;
    102                 n/=2;
    103                 r=(r * r) % mod;
    104             }
    105             work();
    106         }
    107         */
    108         work();
    109         ans1+=mod;
    110         ans1 %= mod;
    111         printf("%I64d
    ",ans1);
    112     }
    113 }
    114 
    115 int main ()
    116 {
    117     init();
    118     close();
    119     return 0;
    120 }
  • 相关阅读:
    Android WebView 获取网页的标题
    Android APK 文件自动安装
    DownloadManager 的使用
    Android Java 自定义异常
    Android NDK
    android assets文件夹资源的访问
    GitHub 基本常用知识解答2
    hbuilder egit插件的安装使用--项目文件丢失的教训
    微软收购跨平台移动应用开发商Xamarin
    Android Study 之 初识ButterKnife(8.5.1)及简单运用
  • 原文地址:https://www.cnblogs.com/cssystem/p/3335191.html
Copyright © 2011-2022 走看看