Queuing
Time Limit: 10000/5000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 6076 Accepted Submission(s): 2643
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
Recommend
lcy
题目大意:给定长度L,由m、f组成的队列,如果是fmf、fff则是E队列,问长为L的队列中最多有多少E队列(mod K)
解题思路:前几个例子不难发现F5 = F1+F3+F4。所以可以得出如下关系:
1 0 1 1 F1 F5
1 0 0 0 F2 F1
* =
0 1 0 0 F3 F2
0 0 1 0 F4 F3
所以就是计算初始矩阵a的l次幂最后mod k即可
代码:
#include <cstdio> #include <cstring> #include <algorithm> using namespace std; int n,mod; struct matrix { long long m[4][4]; matrix operator*(const matrix& a)const { matrix temp; for(int i=0;i<4;i++) { for(int j=0;j<4;j++) { temp.m[i][j] = 0; for(int k=0;k<4;k++) { temp.m[i][j] += m[i][k]*a.m[k][j]%mod; temp.m[i][j] %= mod; } } } return temp; } }; int ks(matrix &a) { if(n<=3) return (2*n)%mod; if(n==4) return 9%mod; n -= 4; matrix ans; memset(ans.m,0,sizeof(ans)); for(int i=0;i<4;i++) { ans.m[i][i] = 1; } while(n) { if(n%2) ans = ans*a; a = a*a; n /= 2; } int sum=0; sum+=ans.m[0][0]*9%mod; sum+=ans.m[0][1]*6%mod; sum+=ans.m[0][2]*4%mod; sum+=ans.m[0][3]*2%mod; sum %= mod; return sum; } int main() { matrix a; while(scanf("%d %d",&n,&mod)!=EOF) { memset(a.m,0,sizeof(a.m)); a.m[0][0] = a.m[0][2] = a.m[0][3] = 1; a.m[1][0] = a.m[2][1] = a.m[3][2] = 1; printf("%d ",ks(a)); } }