Description
Queues 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 2 L 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.
Input
Input a length l (0 <= L <= 6) and M.
Output
Output 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
Give you a string with only F and M, length l, and let you find out the number of strings of the full problem condition to M. If the string does not contain FFF and FMF, the string is said to satisfy the condition.
Suppose that the existing string s,f (n) of length n is the answer to the condition that s string satisfies. F (n) How to roll out to pinch?
If the last digit of S is M, then add f (n-1), because the string obtained by adding a m to the string that satisfies the condition of the length of n-1 must also satisfy the condition.
If the last digit of S is F, then the last three bits of s are only MMF and MFF two cases! Keep talking!
If s ends with MMF, then add f (n-3), because the string obtained by adding a MMF to the string with a length of n-3 satisfies the condition.
What if s ends with MFF? Thinking forward, the fourth place can only be m, that is when S is MMFF end, plus f (n-4) is not flattered?
Synthetic f (N) =f (n-1) +f (n-3) +f (n-4). Shameless re-stealing a picture ... Otz ...
The code is as follows:
1#include <bits/stdc++.h>2 3 using namespacestd;4 Const intn=4;5 intl,m;6 structMatrix7 {8 Long Long intMat[n][n];9 }matrix;Ten voidInit () One { Amemset (Matrix.mat,0,sizeofMatrix.mat); -matrix.mat[0][0]=1; -matrix.mat[0][1]=0; thematrix.mat[0][2]=1; -matrix.mat[0][3]=1; - for(intI=1; i<n;++i) - { + for(intj=0; j<n;++j) - { + if(i==j+1) Amatrix.mat[i][j]=1; at } - } - } -Matrixoperator*(Matrix A,matrix b) - { - Matrix C; in for(intI=0; i<n;++i) - { to for(intj=0; j<n;++j) + { -c.mat[i][j]=0; the for(intk=0; k<n;++k) *c.mat[i][j]+=a.mat[i][k]*B.mat[k][j]; $c.mat[i][j]%=m;Panax Notoginseng } - } the returnC; + } AMatrix Pow (intN) the { + Matrix t; - if(n==1) $ returnMatrix; $ if(n&1) - returnMATRIX*POW (n1); - Else the { -Matrix Temp=pow (n>>1);Wuyi returntemp*temp; the } - } Wu intMain () - { About //freopen ("De.txt", "R", stdin); $ Long Long intf[5]; -f[0]=0; -f[1]=2; -f[2]=4; Af[3]=6; +f[4]=9; the while(~SCANF ("%d%d",&l,&m)) - { $ init (); the if(l<=4) the { theprintf"%lld\n", f[l]%m); the Continue; - } inMatrix Temp=pow (L-4); the Long Long intans=0; the for(intI=0; i<n;++i) About { theAns + = temp.mat[0][i]*f[n-i]; theans%=m; the } +printf"%lld\n", ans); - } the return 0;Bayi}
HDU 2604 Queuing (push-push formula + matrix fast power)