HDoj 2604 queuing


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    mf mm  fm  ff

2     1    1    1   1

3     1    2    2   1

令mf、mm、fm、ff分别为a,b,c,d;

可以看出a(n)=b(n-1); b(n)=b(n-1)+c(n-1); c(n)=a(n-1)+d(n-1);d(n)=a(n-1);

由于题目限制简单地递推会T,不能满足要求

这里把abcd看作一个矩阵

b  c   *   1  1    =   b+c(b‘)    b(a‘)

a  d    1  0       a+d(c‘)    a(d‘)

1  1 * b  a  = b+c(b‘)   a+d(c‘)

1  0    c  d    b(a‘)  a(d‘)

通过右乘、左乘(1,1,1,0)矩阵;达到递推的效果

然后用二分快速幂的方法解题

代码如下:

#include<stdio.h>
#define L(i) for(int i=0;i<2;i++)
int m;
void multip3(int t[2][2]){
	int x[2][2];
	L(i) L(j){
		x[i][j]=0;
		L(k) x[i][j]+=(t[i][k]*t[k][j])%m;
		x[i][j]=x[i][j]%m;
	}
	L(i)L(j) t[i][j]=x[i][j];
}
void mulleft(int a[2][2],int t[2][2]){
	int x[2][2];
	x[0][0]=a[0][0]; x[0][1]=a[1][0];
	x[1][0]=a[0][1]; x[1][1]=a[1][1];
	L(i) L(j){
			a[i][j]=0;
			L(k)a[i][j]+=(x[j][k]*t[k][i])%m;
			a[i][j]=a[i][j]%m;
	}
}
void mulright(int a[2][2],int t[2][2]){
	int x[2][2];
	x[0][0]=a[0][0]; x[0][1]=a[1][0];
	x[1][0]=a[0][1]; x[1][1]=a[1][1];
	L(i)L(j){
			a[i][j]=0;
			L(k)a[i][j]+=(x[i][k]*t[k][j])%m;
			a[i][j]=a[i][j]%m;
	}
}

int z[30][2][2];
int main(){
	int L;
	while(scanf("%d%d",&L,&m)!=EOF){
		int x=0;
		if(L==0) printf("0\n");
		else if(L==1) printf("%d\n",2%m);
		else if(L==2) printf("%d\n",4%m);
		else{
			L-=2;
			int xxx=0;
			if(L%2==1) {xxx=1;}
			L/=2;
			int t[2][2]={1,1,1,0};
			for(int i=0;i<=25;i++){
				L(j)L(k) z[i][j][k]=t[j][k];
				multip3(t);
			}
			int k=0,xx=1;
			int x1[2][2]={1,1,1,1};
			if(xxx){
				x1[0][0]=2;x1[0][1]=1;
				x1[1][0]=2;x1[1][1]=1;
			}
			while(L!=0){
				if(L%2==1) {mulright(x1,z[k]);mulleft(x1,z[k]);}
				L/=2;k++;
			}
			xx=(x1[0][0]+x1[1][0]+x1[0][1]+x1[1][1])%m;
			printf("%d\n",xx);
		}
	}
	return 0;
}

 第一次写这种题目

很开心~~~

时间: 2024-12-19 04:02:04

HDoj 2604 queuing的相关文章

HDU 2604 Queuing (矩阵快速幂)

HDU 2604 Queuing (矩阵快速幂) ACM 题目地址:HDU 2604 Queuing 题意: n个人排队,f表示女,m表示男,包含子串'fmf'和'fff'的序列为O队列,否则为E队列,有多少个序列为E队列. 分析: 矩阵快速幂入门题. 下面引用巨巨解释: 用f(n)表示n个人满足条件的结果,那么如果最后一个人是m的话,那么前n-1个满足条件即可,就是f(n-1): 如果最后一个是f那么这个还无法推出结果,那么往前再考虑一位:那么后三位可能是:mmf, fmf, mff, fff

HDU 2604 Queuing 矩阵高速幂

QueuingTime Limit: 10000/5000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 2483    Accepted Submission(s): 1169 Problem Description Queues and Priority Queues are data structures which are known to most computer s

HDU 2604 Queuing,矩阵快速幂

题目地址:HDU 2604 Queuing 题意: 略 分析: 易推出:   f(n)=f(n-1)+f(n-3)+f(n-4) 构造一个矩阵: 然后直接上板子: /* f[i] = f[i-1] + f[i-3] + f[i-4] */ #include<cstdio> #include<cstring> using namespace std; const int N = 4; int L, M; struct mtx { int x[N+1][N+1]; mtx(){ mem

HDU 2604 Queuing,矩阵高速幂

题目地址:HDU 2604 Queuing 题意: 略 分析: 易推出:   f(n)=f(n-1)+f(n-3)+f(n-4) 构造一个矩阵: 然后直接上板子: /* f[i] = f[i-1] + f[i-3] + f[i-4] */ #include<cstdio> #include<cstring> using namespace std; const int N = 4; int L, M; struct mtx { int x[N+1][N+1]; mtx(){ mem

【HDOJ】2604 Queuing

递推,推得f(n) = f(n-1) + f(n-3) + f(n-4).然后转换成矩阵相乘,如下f(n-1)  f(n-2)  f(n-3)  f(n-4)   *    1   1   0   0   =  f(n)   f(n-1)  f(n-2)  f(n-3)0         0        0         0               0   0   1   0       0         0        0         00         0        0

HDU - 2604 Queuing(矩阵快速幂或直接递推)

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2604 题意:给出字符串长度L,并且字符串只由'f','m'构成,有2^L种情况,问在其中不包含'fmf','fff'的字符串有多少个. 1.直接递推,虽然过了,但是数据稍微大点就很可能TLE,因为代码上交上去耗时还是比较长的(感觉数据有点水)╭(′▽`)╭(′▽`)╯( 1 #include <iostream> 2 #include <cstdio> 3 #include <c

hdu 2604 Queuing dp找规律 然后矩阵快速幂。坑!!

http://acm.hdu.edu.cn/showproblem.php?pid=2604 这题居然O(9 * L)的dp过不了,TLE,  更重要的是找出规律后,O(n)递推也过不了,TLE,一定要矩阵快速幂.然后立马GG. 用2代表m,1代表f.设dp[i][j][k]表示,在第i位,上一位站了的人是j,这一位站的人是k,的合法情况. 递推过去就是,如果j是1,k是2,那么这一位就只能放一个2,这个时猴dp[i][k][2] += dp[i - 1][j][k]; 其他情况分类下就好,然后

HDU 2604 Queuing

Queuing Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 4638    Accepted Submission(s): 2045 Problem Description Queues and Priority Queues are data structures which are known to most computer

hdoj 4062 Queuing 【矩阵快速幂优化递推公式】

Queuing Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 3609    Accepted Submission(s): 1629 Problem Description Queues and Priority Queues are data structures which are known to most computer