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

    Queuing

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


    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
     
    Recommend
    lcy
     
    对于长度为n的队列q(n),做以下讨论。
    图中 ,".....m"表示以字符'm'结尾的queue,其它类似。E_q表示E_queue。
     
    定义f(n)为队列q(n)之中满足E_queue的队列个数。
    可得f(n)=f(n-1)+f(n-3)+f(n-4)
    转换为矩阵,运用矩阵快速幂运算进行求解。
    即,
    n>=4,
     
     
     
     1 #include <iostream>
     2 #include <algorithm>
     3 #include <map>
     4 #include <vector>
     5 #include <functional>
     6 #include <string>
     7 #include <cstring>
     8 #include <queue>
     9 #include <set>
    10 #include <cmath>
    11 #include <cstdio>
    12 using namespace std;
    13 #define IOS ios_base::sync_with_stdio(false)]
    14 typedef long long LL;
    15 const int INF=0x3f3f3f3f;
    16 
    17 const int maxn=6;
    18 int modnum;
    19 typedef struct matrix{
    20     int v[maxn][maxn];
    21     void init(){memset(v,0,sizeof(v));}
    22 }M;
    23 M mul_mod(const M &a,const M &b,int L,int m,int n)
    24 {
    25     M c; c.init();
    26     for(int i=0;i<L;i++){
    27         for(int j=0;j<n;j++){
    28             for(int k=0;k<m;k++)//注意j,k范围
    29                 c.v[i][j]+=a.v[i][k]*b.v[k][j]%modnum;
    30             c.v[i][j]%=modnum;
    31         }
    32     }
    33     return c;
    34 }
    35 M power(M x,int L,int p)
    36 {
    37     M tmp; tmp.init();
    38     for(int i=0;i<L;i++)
    39         tmp.v[i][i]=1;
    40     while(p){
    41         if(p&1) tmp=mul_mod(x,tmp,L,L,L);
    42         x=mul_mod(x,x,L,L,L);
    43         p>>=1;
    44     }
    45     return tmp;
    46 }
    47 int main()
    48 {
    49     int L;
    50     M a,b;
    51     while(scanf("%d%d",&L,&modnum)!=EOF){
    52         b.init();
    53         b.v[0][0]=9%modnum;
    54         b.v[1][0]=6;
    55         b.v[2][0]=4;
    56         b.v[3][0]=2;
    57         if(L==0){printf("1
    "); continue;}
    58         if(L<5){printf("%d
    ",b.v[4-L][0]); continue;}
    59         a.init();
    60         a.v[0][0]=a.v[0][2]=a.v[0][3]=a.v[1][0]=a.v[2][1]=a.v[3][2]=1;
    61         a=power(a,4,L-4);
    62         a=mul_mod(a,b,4,4,1);
    63         printf("%d
    ",a.v[0][0]);
    64     }
    65 }
    View Code

    网上有介绍用trie图得到递推式的方法,根节点为字符串的最后一个字符,根据前一个字符进行状态转移。

    不是非常理解,暂且记录下来。

     
     
  • 相关阅读:
    大数据学习之路(持续更新中...)
    大数据之Yarn——Capacity调度器概念以及配置
    大数据之Oozie——源码分析(一)程序入口
    《结网》—— 读后总结
    [大数据之Yarn]——资源调度浅学
    Java程序员的日常—— FileUtils工具类的使用
    Oracle 11g透明网关连接Sqlserver 2000
    Python3中通过fake_useragent生成随机UserAgent
    Python导入 from lxml import etree 导入不了
    ModuleNotFoundError: No module named 'pymysql'
  • 原文地址:https://www.cnblogs.com/cumulonimbus/p/5692903.html
Copyright © 2011-2022 走看看