hdu 2604 Queuing(推推推公式+矩阵快速幂)

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 2
L 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

给你一个只有f和m,长度为l的字符串,让你找出满题条件的串的个数对m取余的数。如果这个串中不含fff和fmf就说这个串是满足条件的。假设现有长度为n的串s,f(n)是s串满足条件的答案。f(n)怎么推出来捏?如果s的最后一位为m,那么就加上f(n-1),因为在长度为n-1的满足条件的串后面加上一个m所得到的串肯定也满足条件。如果s的最后一位为f,那么s的最后三位只有mmf和mff两种情况!继续讨论!如果s以mmf结尾,那么加上f(n-3),因为在长度为n-3的满足条件的串后面加上一个mmf所得到的串肯定也满足条件。那如果s以mff结尾呢?向前思考一位,倒数第四位只能是m,也就是此时s是以mmff结尾的,再加上f(n-4)岂不是美滋滋?综上f(n)=f(n-1)+f(n-3)+f(n-4)。无耻的再盗张图...OTZ...代码如下:
 1 #include <bits/stdc++.h>
 2
 3 using namespace std;
 4 const int N=4;
 5 int l,m;
 6 struct Matrix
 7 {
 8     long long int mat[N][N];
 9 }matrix;
10 void init()
11 {
12     memset(matrix.mat,0,sizeof matrix.mat);
13     matrix.mat[0][0]=1;
14     matrix.mat[0][1]=0;
15     matrix.mat[0][2]=1;
16     matrix.mat[0][3]=1;
17     for (int i=1;i<N;++i)
18     {
19         for (int j=0;j<N;++j)
20         {
21             if (i==j+1)
22             matrix.mat[i][j]=1;
23         }
24     }
25 }
26 Matrix operator * (Matrix a,Matrix b)
27 {
28     Matrix c;
29     for (int i=0;i<N;++i)
30     {
31         for (int j=0;j<N;++j)
32         {
33             c.mat[i][j]=0;
34             for (int k=0;k<N;++k)
35             c.mat[i][j]+=a.mat[i][k]*b.mat[k][j];
36             c.mat[i][j]%=m;
37         }
38     }
39     return c;
40 }
41 Matrix Pow (int n)
42 {
43     Matrix t;
44     if (n==1)
45     return matrix;
46     if (n&1)
47     return matrix*Pow(n-1);
48     else
49     {
50         Matrix temp=Pow(n>>1);
51         return temp*temp;
52     }
53 }
54 int main()
55 {
56     //freopen("de.txt","r",stdin);
57     long long int f[5];
58     f[0]=0;
59     f[1]=2;
60     f[2]=4;
61     f[3]=6;
62     f[4]=9;
63     while (~scanf("%d%d",&l,&m))
64     {
65         init();
66         if (l<=4)
67         {
68             printf("%lld\n",f[l]%m);
69             continue;
70         }
71         Matrix temp=Pow(l-4);
72         long long int ans=0;
73         for (int i=0;i<N;++i)
74         {
75             ans += temp.mat[0][i]*f[N-i];
76             ans%=m;
77         }
78         printf("%lld\n",ans);
79     }
80     return 0;
81 }

				
时间: 2024-07-29 19:08:19

hdu 2604 Queuing(推推推公式+矩阵快速幂)的相关文章

HDU 5171 GTY&#39;s birthday gift 矩阵快速幂

点击打开链接 GTY's birthday gift Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others) Total Submission(s): 225    Accepted Submission(s): 78 Problem Description FFZ's birthday is coming. GTY wants to give a gift to ZZF. He as

HDU 2256 Problem of Precision (矩阵快速幂)

HDU 2256 Problem of Precision (矩阵快速幂) ACM 题目地址:HDU 2256 Problem of Precision 题意: 给出一个式子,求值. 分析: 推起来最后那步会比较难想. 具体过程见: 表示共轭只听说过复数的和图的... 这构题痕迹好明显... 跟基友开玩笑说:如果遇到这种题,推到Xn+Yn*sqrt(6)这步时,打表最多只能打到10就爆int了,这是输出正解和Xn,说不定眼神好能发现ans = Xn * 2 - 1呢.= =... 代码: /*

HDU 4965 Fast Matrix Calculation(矩阵快速幂)

HDU 4965 Fast Matrix Calculation 题目链接 矩阵相乘为AxBxAxB...乘nn次,可以变成Ax(BxAxBxA...)xB,中间乘n n - 1次,这样中间的矩阵一个只有6x6,就可以用矩阵快速幂搞了 代码: #include <cstdio> #include <cstring> const int N = 1005; const int M = 10; int n, m; int A[N][M], B[M][N], C[M][M], CC[N]

hdu 4965 Fast Matrix Calculation(矩阵快速幂)2014多校训练第9场

Fast Matrix Calculation                                                                   Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 131072/131072 K (Java/Others) Problem Description One day, Alice and Bob felt bored again, Bob knows Ali

CCF 201312-4 有趣的数 (数位DP, 状压DP, 组合数学+暴力枚举, 推公式, 矩阵快速幂)

问题描述 我们把一个数称为有趣的,当且仅当: 1. 它的数字只包含0, 1, 2, 3,且这四个数字都出现过至少一次. 2. 所有的0都出现在所有的1之前,而所有的2都出现在所有的3之前. 3. 最高位数字不为0. 因此,符合我们定义的最小的有趣的数是2013.除此以外,4位的有趣的数还有两个:2031和2301. 请计算恰好有n位的有趣的数的个数.由于答案可能非常大,只需要输出答案除以1000000007的余数. 输入格式 输入只有一行,包括恰好一个正整数n (4 ≤ n ≤ 1000). 输

2016 pku campusH/OpenJ_POJ - C16H(推公式+矩阵快速幂)

传送门:http://poj.openjudge.cn/practice/C16H?lang=en_US 题面:描述 Wenwen has a magical ball. When put on an infinite plane, it will keep duplicating itself forever. Initially, Wenwen puts the ball on the location (x0, y0) of the plane. Then the ball starts

hdu 1757 A Simple Math Problem 矩阵快速幂

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1757 Lele now is thinking about a simple function f(x).If x < 10 f(x) = x.If x >= 10 f(x) = a0 * f(x-1) + a1 * f(x-2) + a2 * f(x-3) + …… + a9 * f(x-10);And ai(0<=i<=9) can only be 0 or 1 .Now, I w

HDU 2256 Problem of Precision(矩阵快速幂)+ HDU 4565

HDU 2256 题意: 计算?(2√+3√)2n?mod1024 思路: ∵f(n)=(2√+3√)2n=(5+26√)n=An+Bn?6√ ∴f(n?1)=An?1+Bn?1?6√ 又∵f(n)=(5+26√)?f(n?1) ∴f(n)=(5?An?1+12?Bn?1)+(2?An?1+5?Bn?1)?6√ 所以递推矩阵就是: (52125)?(An?1Bn?1)=(AnBn) A1=5,B1=2. 然后套矩阵快速幂模板即可求出An,Bn. 又∵(5+26√)n=An+Bn?6√ ∴(5?2

HDU 3292 【佩尔方程求解 &amp;&amp; 矩阵快速幂】

任意门:http://acm.hdu.edu.cn/showproblem.php?pid=3292 No more tricks, Mr Nanguo Time Limit: 3000/1000 MS (Java/Others)    Memory Limit: 65535/32768 K (Java/Others)Total Submission(s): 587    Accepted Submission(s): 400 Problem Description Now Sailormoon