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
 

题解

突然发现这类题目又是有一个小技巧的。先说题意,一个字符串,由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 矩阵高速幂的更多相关文章

  1. HDU.2640 Queuing (矩阵快速幂)

    HDU.2640 Queuing (矩阵快速幂) 题意分析 不妨令f为1,m为0,那么题目的意思为,求长度为n的01序列,求其中不含111或者101这样串的个数对M取模的值. 用F(n)表示串长为n的 ...

  2. ZOJ 3690 &amp; HDU 3658 (矩阵高速幂+公式递推)

    ZOJ 3690 题意: 有n个人和m个数和一个k,如今每一个人能够选择一个数.假设相邻的两个人选择同样的数.那么这个数要大于k 求选择方案数. 思路: 打表推了非常久的公式都没推出来什么可行解,好不 ...

  3. HDU 2604 Queuing,矩阵高速幂

    题目地址:HDU 2604 Queuing 题意:  略 分析: 易推出:   f(n)=f(n-1)+f(n-3)+f(n-4) 构造一个矩阵: 然后直接上板子: /* f[i] = f[i-1] ...

  4. hdu 3221 Brute-force Algorithm(高速幂取模,矩阵高速幂求fib)

    http://acm.hdu.edu.cn/showproblem.php?pid=3221 一晚上搞出来这么一道题..Mark. 给出这么一个程序.问funny函数调用了多少次. 我们定义数组为所求 ...

  5. HDU 1575 Tr A(矩阵高速幂)

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

  6. HDU 2256 Problem of Precision(矩阵高速幂)

    题目地址:HDU 2256 思路: (sqrt(2)+sqrt(3))^2*n=(5+2*sqrt(6))^n; 这时要注意到(5+2*sqrt(6))^n总能够表示成an+bn*sqrt(6); a ...

  7. HDU 2254 奥运(矩阵高速幂+二分等比序列求和)

    HDU 2254 奥运(矩阵高速幂+二分等比序列求和) ACM 题目地址:HDU 2254 奥运 题意:  中问题不解释. 分析:  依据floyd的算法,矩阵的k次方表示这个矩阵走了k步.  所以k ...

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

    HDU 1588 Gauss Fibonacci(矩阵高速幂+二分等比序列求和) ACM 题目地址:HDU 1588 Gauss Fibonacci 题意:  g(i)=k*i+b;i为变量.  给出 ...

  9. 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... ...

随机推荐

  1. C实现线程池

    简介:这里使用linux下的互斥锁和条件变量实现了一个线程池.代码由一个未知作者完成,第二任作者补充优化. 本人仅仅是做了一些注释工作. 代码如下: /*! .h */ #include <st ...

  2. 聚类:(K-means)算法

    1.归类: 聚类(clustering) 属于非监督学习 (unsupervised learning) 无类别标记(class label) 2.举例: 3. K-means 算法:         ...

  3. 使用 ResponseBodyAdvice 拦截Controller方法默认返回参数,统一处理返回值/响应体

    使用 @ControllerAdvice & ResponseBodyAdvice 拦截Controller方法默认返回参数,统一处理返回值/响应体 1.Controller代码 以下是Con ...

  4. 如何在k8s集群里快速运行一个镜像?

    在docker里,快速run一个镜像,很简单的. k8s的世界,与之类似. 但要注意一下,如果镜像本身没有提供command命令,这个容器由于前台输出完成,很快就退出了. 所以,遇到这种镜像,就最好自 ...

  5. 结合Python代码介绍音符起始点检测 (onset detection)

    本文由 meelo 原创,请务必以链接形式注明 本文地址 音符起始点检测介绍 音符起始点检测(onset detection)是音乐信号处理中非常重要的一个算法.节拍和速度(tempo)的检测都会基于 ...

  6. 矩阵链乘(UVa 442)

    结构体 struct matrix 用来保存矩阵的行和列: map<string,matrix> 用来保存矩阵名和相应的行列数: stack<string> 用来保存表达式中遇 ...

  7. Linux mint 17.3系统安装及常用开发办公软件部署

    关于为什么选择linuxmint17.3作为个人办公开发系统的选择说明: 编者按]提起Linux系统,大家可能最先想到的就是 Linux Mint 和 Ubuntu 两个版本了.近来,开源界貌似激进了 ...

  8. Dijkstra-傻子也能看懂的迪杰斯特拉算法(转)

    本周来来介绍指定一个点(源点)到其余各个顶点的最短路径,也叫做“单源最短路径”.例如求下图中的1号顶点到2.3.4.5.6号顶点的最短路径.           与Floyd-Warshall算法一样 ...

  9. 微信JS-SDK之图像接口开发详解

    由于现在手头的项目中有一个上传证件照认证的功能(手机端),之前的思路是直接点击上传,然后直接将图片上传到服务器去,这篇文章有讲到(http://www.cnblogs.com/it-cen/p/453 ...

  10. Hadoop Hive概念学习系列之hive里的JDBC编程入门(二十二)

    Hive与JDBC示例 在使用 JDBC 开发 Hive 程序时, 必须首先开启 Hive 的远程服务接口.在hive安装目录下的bin,使用下面命令进行开启: hive -service hives ...