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): 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;
    
    }
  • 相关阅读:
    Asp.net获取客户端的IP地址排除::1
    EF 筛选列包含NULL会报错
    layUI关于table编辑列支持方向键功能
    .NET CORE 发布IIS问题收集
    VS2019最新源代码管理工具+附下载地址
    关于Mysql可视化工具Navicat Premium12激活使用【亲测】
    经典SQL 语句
    事务的四种隔离级别 [转载]
    HTML 特殊符号编码对照表
    github本地文件Push到仓库
  • 原文地址:https://www.cnblogs.com/dacc123/p/8228698.html
Copyright © 2011-2022 走看看