我是靠谱客的博主 眼睛大朋友,最近开发中收集的这篇文章主要介绍hdu2604 Queuing (矩阵快速幂+动态规划) Queuing,觉得挺不错的,现在分享给大家,希望可以做个参考。

概述

Queuing

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


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 2 L 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
HDU 1st “Vegetable-Birds Cup” Programming Open Contest
 

Recommend
lcy   |   We have carefully selected several similar problems for you:   1588  1757  2606  2276  2603 
 


解析:(以下为引用,具体来源未知)

用f(n)表示n个人满足条件的结果,那么如果最后一个人是m的话,那么前n-1个满足条件即可,就是f(n-1); 
如果最后一个是f那么这个还无法推出结果,那么往前再考虑一位:那么后三位可能是:mmf, fmf, mff, fff,其中fff和fmf不满足题意所以我们不考虑,但是如果是 
mmf的话那么前n-3可以找满足条件的即:f(n-3);如果是mff的话,再往前考虑一位的话只有mmff满足条件即:f(n-4) 
所以f(n)=f(n-1)+f(n-3)+f(n-4),递推会跪,可用矩阵快速幂 
构造一个矩阵: 
pic


代码:

#include<cstdio>
using namespace std;

int n,m;
int f[5]={0,2,4,6,9};
int a[4][4],b[4][4],c[4][4];

void multi(int (*x)[4],int (*y)[4])
{
  int i,j,k;
  for(i=0;i<4;i++)
    for(j=0;j<4;j++)
      for(c[i][j]=k=0;k<4;k++)
        c[i][j]+=x[i][k]*y[k][j];
  for(i=0;i<4;i++)
    for(j=0;j<4;j++)
      x[i][j]=c[i][j]%m;
}

int main()
{
  //freopen("1.in","r",stdin);  
  
  int i,j;
  while(scanf("%d%d",&n,&m)==2)
    {
      if(n<=4){printf("%dn",f[n]%m);continue;}
      a[0][0]=a[0][2]=a[0][3]=1,a[0][1]=0;
      a[1][0]=1,a[1][1]=a[1][2]=a[1][3]=0;
      a[2][0]=a[2][2]=a[2][3]=0,a[2][1]=1;
      a[3][0]=a[3][1]=a[3][3]=0,a[3][2]=1;
      
      for(i=0;i<4;i++)
        for(j=0;j<4;j++)
          b[i][j]=(i==j);
      for(n-=4;n;multi(a,a),n>>=1)if(n&1)multi(b,a);
      for(j=0,i=0;i<4;i++)j+=b[0][i]*f[4-i];
      printf("%dn",j%m);
	}
  return 0;
}



最后

以上就是眼睛大朋友为你收集整理的hdu2604 Queuing (矩阵快速幂+动态规划) Queuing的全部内容,希望文章能够帮你解决hdu2604 Queuing (矩阵快速幂+动态规划) Queuing所遇到的程序开发问题。

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

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

评论列表共有 0 条评论

立即
投稿
返回
顶部