hdu_2604Queuing(快速幂矩阵)

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2604

Queuing

Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 4428    Accepted Submission(s): 1961


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.
 
Sample Input
3 8 4 7 4 8
 
Sample Output
6 2 1
 
Author
WhereIsHeroFrom
 
Source
 
 
注释:在思考一个递推公式的时候几乎都可以分类讨论一下然后写成一个矩阵乘的形式,然后递推得到结果。
 
题意:

用f(n)表示n个人满足条件的结果,那么如果最后一个人是m的话,那么前n-1个满足条件即可,就是f(n-1); 
如果最后一个是f那么这个还无法推出结果,那么往前再考虑一位:那么后三位可能是:mmf, fmf, mff, fff,其中fff和fmf不满足题意所以我们不考虑,但是如果是 
mmf的话那么前n-3可以找满足条件的即:f(n-3);如果是mff的话,再往前考虑一位的话只有mmff满足条件即:f(n-4) 
所以f(n)=f(n-1)+f(n-3)+f(n-4),递推会跪,可用矩阵快速幂 
构造一个矩阵。

用f(n)表示n个人满足条件的结果,那么如果最后一个人是m的话,那么前n-1个满足条件即可,就是f(n-1); 
如果最后一个是f那么这个还无法推出结果,那么往前再考虑一位:那么后三位可能是:mmf, fmf, mff, fff,其中fff和fmf不满足题意所以我们不考虑,但是如果是 
mmf的话那么前n-3可以找满足条件的即:f(n-3);如果是mff的话,再往前考虑一位的话只有mmff满足条件即:f(n-4) 
所以f(n)=f(n-1)+f(n-3)+f(n-4),递推会跪,可用矩阵快速幂 
构造一个矩阵: 
pic

 1 //快速幂矩阵
 2 #include<cstdio>
 3 #include<cstring>
 4 #include<algorithm>
 5 using namespace std;
 6 struct Mat
 7 {
 8     int mat[4][4];
 9 };
10 int m;
11 Mat operator *(Mat a, Mat b)
12 {
13     Mat c;
14     memset(c.mat,0,sizeof(c.mat));
15     int i, j, k;
16     //这儿的顺序按照
17     for(i = 0; i < 4; i++)
18         for(j = 0; j < 4; j++)
19             for(k = 0; k < 4; k++)
20                 c.mat[i][j] = (c.mat[i][j] + a.mat[i][k]*b.mat[k][j])%m;
21     return c;
22 }
23 Mat multi(int n)//计算一个已知矩阵的n次方%m
24 {
25     Mat ans;
26     for(int i = 0; i < 4; i++)
27     {
28         for(int j = 0; j < 4; j++)
29         {
30             if(i==j)
31                 ans.mat[i][j] = 1;
32             else ans.mat[i][j] = 0;
33         }
34 
35     }
36     Mat a;
37     memset(a.mat,0,sizeof(a.mat));
38     a.mat[0][0] = a.mat[0][2] = a.mat[0][3] = a.mat[1][0] = a.mat[2][1] = a.mat[3][2] = 1;
39 
40     while(n>0)
41     {
42         if(n&1) ans = ans*a;
43         a = a*a;
44         n>>=1;
45     }
46     return ans;
47 }
48 int main()
49 {
50     int n;
51     while(~scanf("%d%d",&n,&m))
52     {
53         if(n==1) printf("%d
",2%m);
54         else if(n==2) printf("%d
",4%m);
55         else if(n==3) printf("%d
",6%m);
56         else if(n==4) printf("%d
",9%m);
57         else
58         {
59             Mat t;
60             t = multi(n-4);
61             int sol = ((t.mat[0][0]*9)%m+(t.mat[0][1]*6)%m+(t.mat[0][2]*4)%m+(t.mat[0][3]*2)%m)%m;
62             printf("%d
",sol);
63         }
64     }
65     return 0;
66 }
原文地址:https://www.cnblogs.com/shanyr/p/5668983.html