1. 程式人生 > >【遞推+矩陣快速冪】【HDU2604】【Queuing】

【遞推+矩陣快速冪】【HDU2604】【Queuing】

Queuing

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


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
Author WhereIsHeroFrom FFF FMF

一.找遞推公式

考慮最後若直接加M 顯然可以成立 所以F[n]=F[n-1]+X

若要在後面加F 考慮N-1是沒辦法的

考慮N-2 FF MF 依舊沒辦法

考慮N-3 FFF FMF MMF MFF (FFF,FMF 已經不可能 不考慮)

顯然新增MMF 前面N-3無論是什麼都不可能會出現FFF,FMF了

所以F[n]=F[n-1]+F[n-3]+X

但是 若新增MFF 可能會出現FMFF

考慮N-4 FMFF MMFF 顯然MMFF可以取了

所以 F[n]=F[n-1]+F[n-3]+F[n-4]

二.設計矩陣

注:矩陣十分好設計  第一排即為遞推公式的係數

      以後幾排類似於單位矩陣

三.矩陣快速冪

類似非遞迴快速冪

node kuaisumi(node A,int N,int mod)
{
	node di=e;
	while(N>0)
	{
		if(N&1)
		{
			di=MatrixMult(di,A,mod);
		}
		A=MatrixMult(A,A,mod);
		N=N>>1;
	}
	return di;
}

最後程式碼如下:
#include <cstdio>  
#include <cstdlib>  
#include <cmath>  
#include <cstring>  
#include <ctime>  
#include <algorithm>  
#include <iostream>
#include <sstream>
#include <string>
#define oo 0x13131313   
using namespace std;
int L,M;
struct node
{
	int mat[5][5];
}a,e,ans;
int mat2[5];
void CSH()
{
	mat2[1]=9;
	mat2[2]=6;
	mat2[3]=4;
	mat2[4]=2;
	for(int i=1;i<=4;i++)
{
		e.mat[i][i]=1;
		mat2[i]=mat2[i]%M;
}
	memset(a.mat,0,sizeof(a.mat));
	a.mat[1][1]=a.mat[1][3]=a.mat[1][4]=1;
	a.mat[2][1]=a.mat[3][2]=a.mat[4][3]=1;
}
node MatrixMult(node A,node B,int mod)
{
	node p;
	memset(p.mat,0,sizeof(p.mat));
	for(int i=1;i<=4;i++)
	 for(int j=1;j<=4;j++)
	 {
	 	 for(int k=1;k<=4;k++)
	 	p.mat[i][j]+=A.mat[i][k]*B.mat[k][j];
	 	p.mat[i][j]=p.mat[i][j]%mod;
	 }
	 return p;
}
node kuaisumi(node A,int N,int mod)
{
	node di=e;
	while(N>0)
	{
		if(N&1)
		{
			di=MatrixMult(di,A,mod);
		}
		A=MatrixMult(A,A,mod);
		N=N>>1;
	}
	return di;
}
void solve()
{
	int ANS=0;
	for(int i=1;i<=4;i++)
	{
		ANS+=ans.mat[1][i]*mat2[i];
		ANS=ANS%M;
	}
	printf("%d\n",ANS);
}
int main()
{
	while(cin>>L>>M)
	{
		CSH();
		if(L>4)
		ans=kuaisumi(a,L-4,M);
		if(L>4)
		solve();
		else 
		printf("%d\n",mat2[L]);
	}
	return 0;
}
  


相關推薦

+矩陣快速HDU2604Queuing

Queuing Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 3032    Accepted Subm

poj3233Matrix Power Series(+矩陣快速

題目描述 傳送門 題解 用樸素的矩陣快速冪的話時間無法承受。其實這道題的思路挺奇特的,不是自己想出來的很不爽。 這裡有一個遞推的思路:考慮k能不能從之前的狀態轉移過來。答案是肯定的。 當k%2

hdu 2604 矩陣快速

scan ems while href sca class stdin tdi %d HDU 2604 Queuing (遞推+矩陣快速冪) 這位作者講的不錯,可以看看他的 #include <cstdio> #include <iostream

hdu-2604 Queuing---+矩陣快速

其中 sin 一位 strong DC net name 思路 eof 題目鏈接: https://vjudge.net/problem/HDU-2604 題目大意: n個人排隊,f表示女,m表示男,包含子串‘fmf’和‘fff’的序列為O隊列,否則為E隊列,有多少個序列為

HDU6185 Covering (+矩陣快速

esp over () n-1 告訴 matrix \n nbsp 答案 大致題意:讓你用1*2規格的地毯去鋪4*n規格的地面,告訴你n,問有多少種不同的方案使得地面恰好被鋪滿且地毯不重疊。答案對1000000007取模 遞推得f(n)=f(n-1)+5*f(n-2)+

HDU 2604 Queuing (+矩陣快速)

【題目大意】: n個人排隊,f表示女,m表示男,包含子串‘fmf’和‘fff’的序列為O佇列,否則為E佇列,有多少個序列為E佇列。 【思路】: 用f(n)表示n個人滿足條件的結果,那麼如果最後一個人

POJ3734Blocks(+矩陣快速

ace pro view display \n 方塊 namespace 圖片 個數 題目鏈接:http://poj.org/problem?id=3734 題意:給出n個排成一列的方塊,用紅、藍、綠、黃四種顏色給它們染色,求染成紅、綠的方塊個數同時為偶數的方案數模1000

hdu 2604 Queuing +矩陣快速

#include<cstdio> #include<cstring> #include<algorithm> using namespace std; const i

HDU 6030 Happy Necklace (+矩陣快速

思路: 首先我們發現,當滿足素數區間2,素數區間3的條件之後,下一個素數區間5乃至於之後的所有都會滿足。(因為滿足素數區間2,素數區間3的條件更強) 然後我們假設現在有一個數目為n的方案數為f(n)

概率DP+矩陣快速 POJ - 3744 Scout YYF I

Scout YYF I  POJ - 3744  YYF is a couragous scout. Now he is on a dangerous mission which is to penetrate into the enemy's base. Afte

AC自動機+矩陣快速POJ - 2778 - DNA Sequence & HDU - 2243 - 考研路茫茫——單詞情結

POJ - 2778 - DNA Sequence 題目連結<http://poj.org/problem?id=2778> 題意: DNA序列只包含ACTG四個字元,已知一些病毒的DNA序列,問你序列長度為n(1 <= n <=2000000000)且不

HDU 6198 number number number找規律+矩陣快速

題目連結 題意:從含0的斐波那契數列中可重複的任取K個數,求這K個數的和無法形成的最小整數。 寫了幾個之後大膽的猜測i的答案是F[2∗(i+1)+1]−1… 於是矩陣快速冪…… #includ

POJ 2778 DNA SequenceAC自動機+矩陣快速

題意:給m個病毒字串,問長度為n的DNA片段有多少種沒有包含病毒串的。 首先解決這個問題:給定一個有向圖,問從A點恰好走k步(允許重複經過邊)到達B點的方案數mod p的值     把給定的圖轉為鄰接矩陣,即A(i,j)=1當且僅當存在一條邊i->j。令C=A*A,

AC自動機+矩陣快速 POJ 2778 DNA Sequence

先構建AC自動機,然後通過AC自動機構建矩陣,最後矩陣快速冪即可。。。 #include <iostream> #include <queue> #include <stack> #include <map>

矩陣乘法(乘,快速)學習筆記

自己 mod ... 遞推 str class clu 矩陣 快速 矩陣,是一個好東西。 大家都知道,斐波那契數列是滿足如下性質的一個數列: • f(1) = 1 • f(2) = 1 • f(n) = f(n-1) + f(n-

POJ 3233 Matrix Power Series(求矩陣的和——分塊矩陣快速 or 二分迴+矩陣快速)

Matrix Power Series Time Limit: 3000MS Memory Limit: 131072K Total Submissions: 21451 Accepted:

HDU2604Queuing矩陣快速+

題目連結 Queuing Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(

POJ3070 Fibonacci(矩陣快速加速模板題

題目連結:傳送門 題目大意:   求斐波那契數列第n項F(n)。   (F(0) = 0, F(1) = 1, 0 ≤ n ≤ 109) 思路:   用矩陣乘法加速遞推。 演算法競賽進階指南的模板: #include <iostream> #include &l

洛谷 P1939 模板矩陣加速(數列):優化式的方法——矩陣快速

在大多數情況下,O(n)的效率都是值得驕傲的,然而,有時候並不是,比如如何在一秒鐘內算出一個遞推式的第1e9項,很明顯O(n)不行了。 然而常數級又不太現實,除非你的數學非常好,這題又比較簡單,你推了一個特徵方程的通項公式…… 所以考慮log的做法:矩陣快速冪 如果你還不知

NYOJ 301 求值矩陣快速取模

遞推求值 時間限制:1000 ms  |  記憶體限制:65535 KB 難度:4 描述 給你一個遞推公式: f(x)=a*f(x-2)+b*f(x-1)+c 並給你f(1),f(2