我是靠谱客的博主 鲜艳世界,最近开发中收集的这篇文章主要介绍ACM_HDUOJ_2604_Queuing排队问题Queuing,觉得挺不错的,现在分享给大家,希望可以做个参考。

概述

Queuing

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


 

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

问题描述

两个参数L(队列的长度)和M(结果对M取余)。计算队列中不含有fff且不含有fmf的队列个数对M取余。

问题分析

l=0->0;l=1->2;l=2->4;l=3->6;l=4->9;l=5->15;

找规律可知F(l)=F(l-1)+F(l-3)+F(l-4);

可构造矩阵 |1,0,1,1|与矩阵|F(n-1)|相乘得|F(n) |由此可知

|1,0,0,0| |F(n-2)| |F(n-1)|

|0,1,0,0| |F(n-3)| |F(n-2)|

|0,0,1,0| |F(n-4)| |F(n-3)|

F(n)是矩阵|1,0,1,1|的(n-4)次幂与矩阵|F(4)|相乘的第一行第一列元素

|1,0,0,0| |F(3)|

|0,1,0,0| |F(2)|

|0,0,1,0| |F(1)|

用快速幂对矩阵求n次幂即可;

AC代码

#include <cmath>
#include<iostream>
//#include<algorithm>
#include<cstring>
using namespace std;
int m;
struct Matrix{
    long long int m_matrix[4][4];
    Matrix operator*(const Matrix& rhs)const{
        Matrix temp;
        for(int i = 0 ; i < 4 ; i++){
            for(int j = 0 ; j < 4 ; j++){
                temp.m_matrix[i][j] = 0;
                for(int k = 0 ; k < 4 ; k++){
                    temp.m_matrix[i][j] += (m_matrix[i][k])*(rhs.m_matrix[k][j])%m;
                    temp.m_matrix[i][j] %= m;
                }
            }
        }
        return temp;
    }
};

int get(int l){
    int n =l- 4;
    Matrix rhs={
         1,0,1,1,
         1,0,0,0,
         0,1,0,0,
         0,0,1,0};
    Matrix ans;
    memset(ans.m_matrix , 0 , sizeof(ans.m_matrix));
    for(int i = 0 ; i < 4 ; i++)
        ans.m_matrix[i][i] = 1;
    while(n){
        if(n&1)
            ans = ans*rhs;
        n >>= 1;
        rhs = rhs*rhs;
    }
    int sum = 0;
    sum += ans.m_matrix[0][0]*9%m;
    sum += ans.m_matrix[0][1]*6%m;
    sum += ans.m_matrix[0][2]*4%m;
    sum += ans.m_matrix[0][3]*2%m;
    return sum%m;
}

int main()
{
    int l,s;
    while(cin>>l>>m){
        switch(l){
        case 0:s=0;break;
        case 1:s=2;break;
        case 2:s=4;break;
        case 3:s=6;break;
        case 4:s=9;break;
        default:break;
    }
    
     if(l>=5){
         
         s=get(l);
     }
        cout<<s%m<<endl;
    }
    return 0;
}

 

最后

以上就是鲜艳世界为你收集整理的ACM_HDUOJ_2604_Queuing排队问题Queuing的全部内容,希望文章能够帮你解决ACM_HDUOJ_2604_Queuing排队问题Queuing所遇到的程序开发问题。

如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。

本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
点赞(55)

评论列表共有 0 条评论

立即
投稿
返回
顶部