Hdoj Queuing 2604 "Matrix + fast power of Matrix"

Source: Internet
Author: User

QueuingTime limit:10000/5000 MS (java/others) Memory limit:32768/32768 K (java/others)
Total submission (s): 3528 Accepted Submission (s): 1590


Problem Descriptionqueues and priority Queues is data structures which is known to most computer scientists. The Queue occurs often in we daily life. There is many people lined up at the lunch time.

Now we define this ' F ' is short for female and ' m ' are short for male. If the queue ' s length is L, then there is 2L numbers of queues. For example, if L = 2, then they is 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.

Inputinput a length L (0 <= L <= 6) and M.
Outputoutput k mod m (1 <= m <=) where K is the number of e-queues with length L.
Sample Input
3 84 74 8

Sample Output
621

Authorwhereisherofrom
Sourcehdu 1st "Vegetable-birds Cup" programming Open Contest
Recommendlcy | We have carefully selected several similar problems for you:1588 1757 2606 2603 3117
Test Instructions: give you a length of L of M and F two letters of the string, the definition of the existence of FMF and FFF substring is not meet the requirements of the string, ask the length of the number of matching requirements of l?

Problem Solving Ideas:

If the problem is directly using a recursive relationship, it is not good to handle the hyper-memory.

First, the recursive relation is found, and the recursive relation is given: (L) = (L-1) + (L-3) + (L-4); You can try to deduce it first, and then look at the explanations below.

Consider the situation when l=n, there are two situations:

①. If the last character is M: at this point, the string with the current length n is bound to meet the requirements as long as the string with the preceding length is n-1.

②. If the last character is F: At this time, cannot be determined, because there may be non-compliant strings, continue to discuss the situation

(1). The last two characters are F, there may still be a string that does not meet the requirements, continue the discussion of the situation

1. The last third character is F, because there is FFF, so this kind of situation must not meet the requirements, to

2. The third-to-last character is M, which may still not meet the requirements, then divide

A. The last fourth character is F, there is a FMF, so this kind of situation must not meet the requirements, give up

B. The last fourth character is M, so long as the front length is n-4, the string with the current length n must also meet the requirements

(2). The last second character is M, where there may be a

1. The last third character is F, there is a FMF, at this time must not meet the requirements of the

2. The last third character is M, so long as the front length of the string of n-3 meets the requirements, then the string with the current length n is bound to meet the requirements.

Therefore, the condition that meets the requirements is summed up: (L) = (L-1) + (L-3) + (L-4);

As I've already said, if you just use the normal recursive method to hyper-memory, consider optimization here.

How to optimize? Let's look at the result of multiplying the following matrices:


How much will the X matrix get back to the matrix? We only need to consider the first line of the next matrix because the other elements are 0.

The 1th row in the 1th column of the element we need to get f (n), because F (n) =f (n-1) +f (n-3) +f (n-4); So we have to keep f (n-1), F (n-3), F (n-4) so the number multiplied by must be 1.

So the 1th column element can be determined, for 1 0 1 1, note that it is the first column instead of the first row.

Based on the second column element of the first row, we can determine the second column of the x Matrix element: 1 0 0 0.

Based on the third column element of the first row, we can determine the third column of the x Matrix element: 0 1 0 0.

Based on the fourth column element of the first row, we can determine the x matrix fourth element: 0 0 1 0.

So the X-matrix has been determined, so we can get the following matrix multiplicative:


So, multiply the X-matrix repeatedly to get the desired f (n);

So we can find out the L-4 (not L) of the X matrix, and then convert it to the fast power of matrix. Then the first column of the Matrix with F (4) F (3) F (2) F (1) is multiplied to obtain the F (n) =res[0][0]*f[4]+res[1][0]*f[3]+res[2][0]*f[2]+res[ 3[0]*F[1].

This concludes the explanation-Welcome to the error ~.

AC Code:

#include <stdio.h> #include <string.h> #include <algorithm> #define MAXN 5using namespace Std;int mat[ Maxn][maxn];int res[maxn][maxn];int f[10];void matmul (int x[maxn][maxn],int y[maxn][maxn],int Mod) {int t[maxn][maxn]= {0};for (int i=0;i<4;i++) for (int. k=0;k<4;k++) if (X[i][k]) for (int j=0;j<4;j++) t[i][j]= (T[i][j]+x[i][k]*y[k] [J]%mod)%mod;for (int i=0;i<4;i++) for (int j=0;j<4;j++) x[i][j]=t[i][j];} void Matrix (int t[maxn][maxn],int m,int Mod) {for (Int. i=0;i<4;i++) for (int j=0;j<4;j++) res[i][j]= (I==J), while (m) {if (m&1) Matmul (RES,T,MOD); Matmul (t,t,mod); m>>=1;}}    int main () {int l,m;while (scanf ("%d%d", &l,&m)!=eof) {memset (f,0,sizeof (f)); f[1]=2;f[2]=4; F[3]=6;f[4]=9;int t[maxn][maxn]={0}; T[0][0]=t[2][0]=t[3][0]=1; T[0][1]=t[1][2]=t[2][3]=1;if (l==1) {printf ("%d\n", 2%m); continue;} if (l==2) {printf ("%d\n", 4%m); continue;} if (l==3) {printf ("%d\n", 6%m); continue;} if (l==4) {printf ("%d\n", 9%m); continue;} Matrix (t,l-4,m); int ans=0;for (int i=0;i<4;i++) {ans= (ans+resI [0]*f[4-i])%M;} printf ("%d\n", ans);} return 0;}



Copyright NOTICE: This article is the original blogger articles, reproduced please indicate the source.

Hdoj Queuing 2604 "Matrix + fast power of Matrix"

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.