HDU 2604 Queuing 矩阵高速幂

Queuing

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

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

HDU 1st “Vegetable-Birds Cup” Programming Open Contest

题解


突然发现这类题目又是有一个小技巧的。先说题意,一个字符串,由f和m两种字符构成。如今的问题是,当中的子串,不出现“fff”和"fmf"的长度为L的串有多少个。

相同的,我们考虑一个充分长的串,确定他的最后两位之后,看看倒数第三位的字符是什么:

这里的x代表的是倒数第三位,能够看到,事实上这个是有规律可循的。我们仅仅要把生成fff和fmf的那种情况规避掉即可了。所以整个矩阵就是:

最后把矩阵中的所有的值所有加起来取模就可以。

代码演示样例

/****
*@author Shen
*@title HDU 2604
*/
#include <cstdio>
#include <string>
#include <cstring>
#include <iostream>
#include <algorithm>
using namespace std;
typedef long long int64;

const int MAXN = 4;
const int MAXM = 4;
const int Mod = 1000000007;

struct Matrax{
int n, m;
int64 mat[MAXN][MAXM];
Matrax(): n(-1), m(-1){}
Matrax(int _n, int _m): n(_n), m(_m){
memset(mat, 0, sizeof(mat));
}
void Unit(int _s){
n = _s; m = _s;
for (int i = 0; i < n; i++){
for (int j = 0; j < n; j++){
mat[i][j] = (i == j)? 1: 0;
}
}
}
void print(){
printf("n = %d, m = %d\n", n, m);
for (int i = 0; i < n; i++){
for (int j = 0; j < m; j++)
printf("%16d", mat[i][j]);
printf("\n");
}
}
};

Matrax add_mod(const Matrax& a,const Matrax& b,const int64 mod){
Matrax ans(a.n, a.m);
for (int i = 0; i < a.n; i++){
for (int j = 0; j < a.m; j++){
ans.mat[i][j] = (a.mat[i][j] + b.mat[i][j]) % mod;
}
}
return ans;
}

Matrax mul(const Matrax& a,const Matrax& b){
Matrax ans(a.n, b.m);
for (int i = 0; i < a.n; i++){
for (int j = 0; j < b.m; j++){
int64 tmp = 0;
for (int k = 0; k < a.m; k++){
tmp += a.mat[i][k] * b.mat[k][j];
}
ans.mat[i][j] = tmp;
}
}
return ans;
}

Matrax mul_mod(const Matrax& a, const Matrax& b, const int mod){
Matrax ans(a.n, b.m);
for (int i = 0; i < a.n; i++){
for (int j = 0; j < b.m; j++){
int64 tmp = 0;
for (int k = 0; k < a.m; k++){
tmp += (a.mat[i][k] * b.mat[k][j]) % mod;
}
ans.mat[i][j] = tmp % mod;
}
}
return ans;
}

Matrax pow_mod(const Matrax& a, int64 k, const int mod){
Matrax p(a.n, a.m), ans(a.n, a.m);
p = a; ans.Unit(a.n);
if (k == 0) return ans;
else if (k == 1) return a;
else {
while (k){
if (k & 1){
ans = mul_mod(ans, p, mod);
k--;
}
else {
k /= 2;
p = mul_mod(p, p, mod);
}
}
return ans;
}
}

int l, m;

void solve(){
if (l <= 2)
{
int root = 1;
for (int i = 0; i < l; i++)
root *= 2;
cout << root % m << endl;
return;
}

Matrax ans(1, 1);

//tmp = cef ^ (l - 2);
//ans = vct * tmp;
//ans = ans * beg;
//res = ans.mat[0][0] % m;

Matrax cef(4, 4), tmp(4, 4);
cef.mat[0][0] = 1; cef.mat[0][3] = 1;
cef.mat[1][2] = 1;
cef.mat[2][0] = 1;
cef.mat[3][1] = 1; cef.mat[3][2] = 1;
//cef.print();

Matrax beg(4, 1), vct(1, 4);
for (int i = 0; i < 4; i++)
beg.mat[i][0] = vct.mat[0][i] = 1;

tmp = pow_mod(cef, l - 2, m);
//tmp.print();

vct = mul_mod(vct, tmp, m);
ans = mul_mod(vct, beg, m);
//ans.print();

int res = ans.mat[0][0];
cout << res % m << endl;
}

int main(){
while (cin >> l >> m) solve();
return 0;
}

HDU 2604 Queuing 矩阵高速幂,布布扣,bubuko.com

时间: 2024-08-26 15:06:31

HDU 2604 Queuing 矩阵高速幂的相关文章

HDU - 2604 Queuing(矩阵快速幂或直接递推)

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2604 题意:给出字符串长度L,并且字符串只由'f','m'构成,有2^L种情况,问在其中不包含'fmf','fff'的字符串有多少个. 1.直接递推,虽然过了,但是数据稍微大点就很可能TLE,因为代码上交上去耗时还是比较长的(感觉数据有点水)╭(′▽`)╭(′▽`)╯( 1 #include <iostream> 2 #include <cstdio> 3 #include <c

HDU 2604 Queuing,矩阵高速幂

题目地址:HDU 2604 Queuing 题意: 略 分析: 易推出:   f(n)=f(n-1)+f(n-3)+f(n-4) 构造一个矩阵: 然后直接上板子: /* f[i] = f[i-1] + f[i-3] + f[i-4] */ #include<cstdio> #include<cstring> using namespace std; const int N = 4; int L, M; struct mtx { int x[N+1][N+1]; mtx(){ mem

HDU 2604 Queuing (矩阵快速幂)

HDU 2604 Queuing (矩阵快速幂) ACM 题目地址:HDU 2604 Queuing 题意: n个人排队,f表示女,m表示男,包含子串'fmf'和'fff'的序列为O队列,否则为E队列,有多少个序列为E队列. 分析: 矩阵快速幂入门题. 下面引用巨巨解释: 用f(n)表示n个人满足条件的结果,那么如果最后一个人是m的话,那么前n-1个满足条件即可,就是f(n-1): 如果最后一个是f那么这个还无法推出结果,那么往前再考虑一位:那么后三位可能是:mmf, fmf, mff, fff

HDU 2604 Queuing,矩阵快速幂

题目地址:HDU 2604 Queuing 题意: 略 分析: 易推出:   f(n)=f(n-1)+f(n-3)+f(n-4) 构造一个矩阵: 然后直接上板子: /* f[i] = f[i-1] + f[i-3] + f[i-4] */ #include<cstdio> #include<cstring> using namespace std; const int N = 4; int L, M; struct mtx { int x[N+1][N+1]; mtx(){ mem

HDU 1588 Gauss Fibonacci(矩阵高速幂+二分等比序列求和)

HDU 1588 Gauss Fibonacci(矩阵高速幂+二分等比序列求和) ACM 题目地址:HDU 1588 Gauss Fibonacci 题意: g(i)=k*i+b;i为变量. 给出k,b,n,M,问( f(g(0)) + f(g(1)) + ... + f(g(n)) ) % M的值. 分析: 把斐波那契的矩阵带进去,会发现这个是个等比序列. 推倒: S(g(i)) = F(b) + F(b+k) + F(b+2k) + .... + F(b+nk) // 设 A = {1,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 5318 The Goddess Of The Moon 矩阵高速幂

链接:http://acm.hdu.edu.cn/showproblem.php?pid=5318 The Goddess Of The Moon Time Limit: 6000/3000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others) Total Submission(s): 438    Accepted Submission(s): 150 Problem Description Chang'e (嫦娥) is

hdu 4549 M斐波那契数列(矩阵高速幂,高速幂降幂)

http://acm.hdu.edu.cn/showproblem.php?pid=4549 f[0] = a^1*b^0%p,f[1] = a^0*b^1%p,f[2] = a^1*b^1%p.....f[n] = a^fib[n-1] * b^fib[n-2]%p. 这里p是质数,且a,p互素,那么我们求a^b%p,当b非常大时要对b降幂. 由于a,p互素,那么由费马小定理知a^(p-1)%p = 1.令b = k*(p-1) + b'.a^b%p = a^(k*(p-1)+b')%p =

HDU 1575 Tr A(矩阵高速幂)

题目地址:HDU 1575 矩阵高速幂裸题. 初学矩阵高速幂.曾经学过高速幂.今天一看矩阵高速幂,原来其原理是一样的,这就好办多了.都是利用二分的思想不断的乘.仅仅只是把数字变成了矩阵而已. 代码例如以下: #include <iostream> #include <cstdio> #include <string> #include <cstring> #include <stdlib.h> #include <math.h> #i