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.
输入样例
3 8
4 7
4 8
输出样例
6 2 1
注意首项的选择,以及ans的赋值
附ac代码
#include<bits/stdc++.h> using namespace std; const int N=4; struct matrix{ long long ma[N][N]; }; int l,m; matrix muti(matrix a,matrix b) { matrix c; for(int i=0;i<4;++i) for(int j=0;j<4;++j) c.ma[i][j]=0; for(int i=0;i<4;++i) for(int j=0;j<4;++j) for(int z=0;z<4;++z) c.ma[i][j]+=(a.ma[i][z]%m*(b.ma[z][j]%m)%m); return c; } matrix pow_ma(matrix a,int k) { if(k==1) return a; matrix s; s=pow_ma(muti(a,a),k/2); if(k%2) s=muti(s,a); return s; } int main() { int f[5]; f[1]=2,f[2]=4,f[3]=6,f[4]=9; //f[0]不为1,为0,故f[4]需要单独讨论 while(scanf("%d%d",&l,&m)==2) { if(l<=4) {printf("%d\n",f[l]%m);continue; } matrix ans; for(int i=0;i<4;++i) for(int j=0;j<4;++j) ans.ma[i][j]=0; ans.ma[0][0]=1,ans.ma[0][2]=1,ans.ma[0][3]=1; ans.ma[1][0]=1,ans.ma[2][1]=1,ans.ma[3][2]=1; ans=pow_ma(ans,l-4);
//注意pow_ma时void函数需要再对ans赋值 long long sum=0; for(int i=0;i<4;++i) sum+=(ans.ma[0][i]%m*(f[4-i]%m))%m; printf("%lld\n",sum%m); } return 0; }
标签:hdu,matrix,int,矩阵,long,Queuing,queue,length,mod From: https://www.cnblogs.com/ruoye123456/p/17052069.html