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;
    
    }
  • 相关阅读:
    二分查找
    bracketed-paste-magic:zle:41: not enough arguments for -U
    逗号表达式返回值
    requestAnimationFrame实现一帧的函数节流
    phaser常用API总结
    table表头固定问题
    接口防刷的方法
    雪碧图background-position的rem用法
    sphinx 增量索引与主索引使用测试
    msysgit ls 中文显示
  • 原文地址:https://www.cnblogs.com/dacc123/p/8228698.html
Copyright © 2011-2022 走看看