• HDU 2604 Queuing(矩阵快速幂)


    Queuing

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

    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

    找到递推关系
    s[n]=s[n-1]+s[n-3]+s[n-4];

    #include <iostream>
    #include <string.h>
    #include <stdio.h>
    #include <algorithm>
    #include <math.h>
    #include <stdlib.h>
    
    using namespace std;
    struct Node
    {
        int a[10][10];
    };
    int n,m;
    Node multiply(Node a,Node b)
    {
        Node c;
        memset(c.a,0,sizeof(c.a));
        for(int i=0;i<4;i++)
        {
            for(int j=0;j<4;j++)
            {
                if(!a.a[i][j]) continue;
                for(int k=0;k<4;k++)
                {
                    (c.a[i][k]+=(a.a[i][j]*b.a[j][k])%m)%=m;
                }
            }
        }
        return c;
    }
    Node get(Node a,int x)
    {
        Node c;
        memset(c.a,0,sizeof(c.a));
        for(int i=0;i<4;i++)
            c.a[i][i]=1;
        for(x;x;x>>=1)
        {
            if(x&1) c=multiply(c,a);
            a=multiply(a,a);
        }
        return c;
    }
    int main()
    {
        while(scanf("%d%d",&n,&m)!=EOF)
        {
        if(n==0)
        {printf("1
    ");continue;}
        else if(n==1)
        {printf("2
    ");continue;}
        else if(n==2)
        {printf("4
    ");continue;}
        else if(n==3)
        {printf("6
    ");continue;}
        else
        {
            Node a;
            a.a[0][0]=1;a.a[0][1]=0;a.a[0][2]=1;a.a[0][3]=1;
            a.a[1][0]=1;a.a[1][1]=0;a.a[1][2]=0;a.a[1][3]=0;
            a.a[2][0]=0;a.a[2][1]=1;a.a[2][2]=0;a.a[2][3]=0;
            a.a[3][0]=0;a.a[3][1]=0;a.a[3][2]=1;a.a[3][3]=0;
            Node b;
            memset(b.a,0,sizeof(b.a));
            b.a[0][0]=6;b.a[1][0]=4;b.a[2][0]=2;b.a[3][0]=1;
            a=get(a,n-3);
            a=multiply(a,b);
            printf("%d
    ",a.a[0][0]);
    
        }
    
        }
        return 0;
    
    }
  • 相关阅读:
    UltraISO写入时提升硬盘映像容量太小
    Ubuntu下查找某个文件属于哪个包
    2012 Server R2安装VMware Tool出现KB2919355的代码
    vCenter通过模板部署虚拟机
    Ubuntu配置IP地址
    记一次flex布局中子项目尺寸不受flexshrink限制的问题
    注解@Async的使用 Roy
    异步任务 :无返回值 和有返回值 Roy
    hibernate得到数据后会自动更新到数据库
    ksmtuned占用CPU高
  • 原文地址:https://www.cnblogs.com/dacc123/p/8228698.html
Copyright © 2020-2023  润新知