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): 6076    Accepted Submission(s): 2643


    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

     题目大意:给定长度L,由m、f组成的队列,如果是fmf、fff则是E队列,问长为L的队列中最多有多少E队列(mod K)

    解题思路:前几个例子不难发现F5 = F1+F3+F4。所以可以得出如下关系:

    1  0  1  1        F1            F5

    1  0  0  0        F2            F1

                     *              =     

    0  1  0  0        F3            F2

    0  0  1  0        F4       F3

    所以就是计算初始矩阵a的l次幂最后mod k即可

    代码:

    #include <cstdio>
    #include <cstring>
    #include <algorithm>
    using namespace std;
    int n,mod;
    struct matrix
    {
        long long m[4][4];
        matrix operator*(const matrix& a)const
        {
            matrix temp;
            for(int i=0;i<4;i++)
            {
                for(int j=0;j<4;j++)
                {
                    temp.m[i][j] = 0;
                    for(int k=0;k<4;k++)
                    {
                        temp.m[i][j] += m[i][k]*a.m[k][j]%mod;
                        temp.m[i][j] %= mod;
                    }
                }
            }
            return temp;
        }
    };
    
    int ks(matrix &a)
    {
        if(n<=3)
            return (2*n)%mod;
        if(n==4)
            return 9%mod;
        n -= 4;
        matrix ans;
        memset(ans.m,0,sizeof(ans));
        for(int i=0;i<4;i++)
        {
            ans.m[i][i] = 1;
        }
        while(n)
        {
            if(n%2)
                ans = ans*a;
            a = a*a;
            n /= 2;
        }
    
        int sum=0;
        sum+=ans.m[0][0]*9%mod;
        sum+=ans.m[0][1]*6%mod;
        sum+=ans.m[0][2]*4%mod;
        sum+=ans.m[0][3]*2%mod;
        sum %= mod;
        return sum;
    }
    int main()
    {
        matrix a;
        while(scanf("%d %d",&n,&mod)!=EOF)
        {
            memset(a.m,0,sizeof(a.m));
            a.m[0][0] = a.m[0][2] = a.m[0][3] = 1;
            a.m[1][0] = a.m[2][1] = a.m[3][2] = 1;
            printf("%d
    ",ks(a));
        }
    }
  • 相关阅读:
    #检查磁盘使用率超过90%,并且后台进程没有rman在跑,就运行 /data/script/del_dg_arch.sh 脚本清理归档
    linux shell数据重定向
    创建用户
    Linux HA+ Oracle 安装维护手册
    解决UnicodeEncodeError: 'ascii' codec can't encode characters in position 0-1: ordinal not in range
    Linux 文件不能被root修改与编辑原因
    python中的时间戳和格式化之间的转换
    Python-Redis-发布订阅
    Python-Redis-常用操作&管道
    Python-Redis-Set
  • 原文地址:https://www.cnblogs.com/WWkkk/p/7399306.html
Copyright © 2011-2022 走看看