#include <cstdio> #include <cstring>using namespace std;int m;int f (int x) {int s1[5][5],s2[5][5],s3[5][5],i , A=0,j,k,b=0,c=0,d=0; memset (s1,0,sizeof (S1)); memset (s2,0,sizeof (S2)); S1[0][1]=1;s2[0][1]=1; S1[1][3]=1;s2[1][3]=1; S1[2][0]=1;s2[2][0]=1; S1[2][1]=1;s2[2][1]=1; S1[3][2]=1;s2[3][2]=1; S1[3][3]=1;s2[3][3]=1; while (x) {if (x&1) {memset (s3,0,sizeof (S3)); for (i=0;i<4;i++) for (j=0;j<4;j++) for (k=0;k<4;k++) s3[i][j]+=s1[i][k]*s2[k ][J]; for (i=0;i<4;i++) for (j=0;j<4;j++) s2[i][j]=s3[i][j]%m; } memset (S3,0,sizeof (S3)); for (i=0;i<4;i++) for (j=0;j<4;j++) for (k=0;k<4;k++) s3[i][j]+=s1[i][k]*s1[k][j]; for (i=0;i<4;i++) for (j=0;j<4;j++) s1[i][j]=s3[i][j]%m; x>>=1; } for (i=0;i<4;i++) {a+=s2[i][0]; B+=S2[I][1]; C+=S2[I][2]; D+=S2[I][3]; } return a+b+c+d;} int main () {int l,p; while (~SCANF ("%d%d", &l,&m)) {if (l==0) {printf ("0\n"); continue;} if (l==1) {printf ("%d\n", 2%m); continue;} if (l==2) {printf ("%d\n", 4%m); continue;} l-=3; P=f (l)%m; printf ("%d\n", p); } return 0;}
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 <= 106) and M.
Output
Output K mod m (1 <= m <=) where K is the number of e-queues with length L.
Sample Input
3 8 4) 74 8
Sample Output
6 Test Instructions: L individual queue, F for boys, M for girls, there are no FFF and FMF team There are how many kinds. This is a recursive problem, because the data is relatively large, so we must first find the recursive formula, and then use the matrix to do a fast power. Ff[i] indicates that the queue length is I, the team I is F, the i-1 is also F. Fm[i],mf[i],mm[i] will not explain. Ff[i]=mf[i-1], because after mf[i-1], add F is mff. Ff[i]!=ff[i-1]. Because after f[i-1, adding f is FFF, illegal. similarly fm[i]=ff[i-1]+mf[i-1],mf[i]=mm[i-1],mm[i]=fm[i-1]+mm[i-1]; The following tectonic matrix 0 1 0 0 (ff[ I-1] fm[i-1] mf[i-1] mm[i-1] * 0 0 0 1 = (ff[i] fm[i] mf[i] Mm[i]) 1 1 0 0 0 0 1 1
HDU 2604 Queuing