Queuing
Time Limit: 10000/5000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 3141 Accepted Submission(s): 1431
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.

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
Source
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 | //矩阵快速幂解递推式:f(n)=f(n-1)+f(n-2)+f(n-3) #include<cstdio> #include<cstring> using namespace std; struct node{ int mat[4][4]; }; int L,M; const int n=4; node mat_multi(node a,node b) { node c; int i,j,k; memset (c.mat,0, sizeof (c.mat)); for (i=0;i<n;i++) for (j=0;j<n;j++) for (k=0;k<n;k++) { c.mat[i][j]+=a.mat[i][k]*b.mat[k][j]; c.mat[i][j]%=M; } return c; } node pow_mod(node a, int t) { node c; int i; memset (c.mat,0, sizeof (c.mat)); for (i=0;i<n;i++) c.mat[i][i]=1; for (;t>0;t>>=1) { if (t&1) c=mat_multi(a,c); a=mat_multi(a,a); } return c; } int main() { int i,t; int b[4]={9,6,4,2}; node c; while (~ scanf ( "%d%d" ,&L,&M)) { c.mat[0][0]=1; c.mat[0][1]=0; c.mat[0][2]=1; c.mat[0][3]=1; c.mat[1][0]=1; c.mat[1][1]=0; c.mat[1][2]=0; c.mat[1][3]=0; c.mat[2][0]=0; c.mat[2][1]=1; c.mat[2][2]=0; c.mat[2][3]=0; c.mat[3][0]=0; c.mat[3][1]=0; c.mat[3][2]=1; c.mat[3][3]=0; int ans=0; if (L==0) ans=0; else if (L==1) ans=2; else if (L==2) ans=4; else if (L==3) ans=6; else if (L==4) ans=9; else { t=L-4; c=pow_mod(c,t); for (i=0;i<4;i++) //构造矩阵的(L-4)次幂后 再乘以前4项就是结果 ans+=c.mat[0][i]*b[i]; } printf ( "%d\n" ,ans%M); } } |
公式是从网上找的。。。。
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 开发者必知的日志记录最佳实践
· SQL Server 2025 AI相关能力初探
· Linux系列:如何用 C#调用 C方法造成内存泄露
· AI与.NET技术实操系列(二):开始使用ML.NET
· 记一次.NET内存居高不下排查解决与启示
· 阿里最新开源QwQ-32B,效果媲美deepseek-r1满血版,部署成本又又又降低了!
· 开源Multi-agent AI智能体框架aevatar.ai,欢迎大家贡献代码
· Manus重磅发布:全球首款通用AI代理技术深度解析与实战指南
· 被坑几百块钱后,我竟然真的恢复了删除的微信聊天记录!
· 没有Manus邀请码?试试免邀请码的MGX或者开源的OpenManus吧