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 = , 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 ( <= L <=  ) and M.
Output
Output K mod M( <= M <= ) where K is the number of E-queues with length L.
Sample Input

 
Sample Output

 
Source
 

题意: 
n个人排队,f表示女,m表示男,包含子串‘fmf’和‘fff’的序列为O队列,否则为E队列,有多少个序列为E队列。

分析: 
矩阵快速幂入门题。 

用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),递推会跪,可用矩阵快速幂 
构造一个矩阵: 

 #pragma comment(linker, "/STACK:1024000000,1024000000")
#include<iostream>
#include<cstdio>
#include<cstring>
#include<cmath>
#include<math.h>
#include<algorithm>
#include<queue>
#include<set>
#include<bitset>
#include<map>
#include<vector>
#include<stdlib.h>
#include <stack>
using namespace std;
#define PI acos(-1.0)
#define max(a,b) (a) > (b) ? (a) : (b)
#define min(a,b) (a) < (b) ? (a) : (b)
#define ll long long
#define eps 1e-10
#define MOD 1000000007
#define N 1000006
#define inf 1e12
int L,M;
struct Matrix{
int mp[][];
};
Matrix Mul(Matrix a,Matrix b){
Matrix res;
for(int i=;i<;i++){
for(int j=;j<;j++){
res.mp[i][j]=;
for(int k=;k<;k++){
res.mp[i][j]=(res.mp[i][j]+(a.mp[i][k]*b.mp[k][j])%M+M)%M;
}
}
}
return res;
}
Matrix fastm(Matrix a,int b){
Matrix res;
memset(res.mp,,sizeof(res.mp));
for(int i=;i<;i++){
res.mp[i][i]=;
}
while(b){
if(b&){
res=Mul(res,a);
}
a=Mul(a,a);
b>>=;
}
return res;
}
int main()
{
while(scanf("%d%d",&L,&M)==){ if(L==){
printf("%d\n",%M);
continue;
}
if(L==){
printf("%d\n",%M);
continue;
}
if(L==){
printf("%d\n",%M);
continue;
}
if(L==){
printf("%d\n",%M);
continue;
} Matrix tmp;
for(int i=;i<;i++){
for(int j=;j<;j++){
tmp.mp[i][j]=;
}
} tmp.mp[][]=;
tmp.mp[][]=;
tmp.mp[][]=;
tmp.mp[][]=;
tmp.mp[][]=;
tmp.mp[][]=;
Matrix cnt=fastm(tmp,L-); Matrix g;
g.mp[][]=;
g.mp[][]=;
g.mp[][]=;
g.mp[][]=;
cnt=Mul(cnt,g);
printf("%d\n",cnt.mp[][]%M);
} return ;
}

hdu 2604 Queuing(矩阵快速幂乘法)的更多相关文章

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

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

  2. HDU 2604 Queuing 矩阵高速幂

    Queuing Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total S ...

  3. HDU 5667 构造矩阵快速幂

    HDU 5667 构造矩阵快速幂 题目描述 解析 我们根据递推公式 设 则可得到Q的指数关系式 求Q构造矩阵 同时有公式 其中φ为欧拉函数,且当p为质数时有 代码 #include <cstdi ...

  4. HDU 6185 Covering 矩阵快速幂

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=6185 题意:用 1 * 2 的小长方形完全覆盖 4 * n的矩形有多少方案. 解法:小范围是一个经典题 ...

  5. HDU 2157(矩阵快速幂)题解

    How many ways?? Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) ...

  6. HDU 6395 分段矩阵快速幂 HDU 6386 建虚点+dij

    http://acm.hdu.edu.cn/showproblem.php?pid=6395 Sequence Time Limit: 4000/2000 MS (Java/Others)    Me ...

  7. HDU 6470 【矩阵快速幂】

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=6470 写这道题是为了让自己不要忘记矩阵快速幂如何推出矩阵式子的. 注意 代码是TLE的!! #incl ...

  8. HDU 5607 graph 矩阵快速幂 + 快速幂

    这道题得到了学长的助攻,其实就是一个马尔科夫链,算出一步转移矩阵进行矩阵快速幂就行了,无奈手残 这是我第一回写矩阵快速幂,写的各种毛病,等到调完了已经8点44了,交了一发,返回PE,(发现是少了换行) ...

  9. HDU 1575(裸矩阵快速幂)

    emmmmm..就是矩阵快速幂,直接附代码: #include <cstdio> using namespace std; ; ; struct Matrix { int m[maxn][ ...

随机推荐

  1. ASP.NET C# 有程序集加不了解决办法

    在项目中添加app.config 获取在 web.config 添加 <?xml version="1.0"?> <configuration> <s ...

  2. 阿里云CentOS 7.1使用yum安装MySql5.6.24

    正确的安装方法: 众所周知,Linux系统自带的repo是不会自动更新每个软件的最新版本(基本都是比较靠后的稳定版),所以无法通过yum方式安装MySQL的高级版本.所以我们需要先安装带有当前可用的m ...

  3. AprioriTID algorithm

    What is AprioriTID? AprioriTID is an algorithm for discovering frequent itemsets (groups of items ap ...

  4. Firebug Command Line

    http://michaelsync.net/2007/09/15/firebug-tutorial-commandline-api

  5. 常用433MHZ无线芯片性能对比表分享

    常用433M芯片性能对比: 芯片型号 SI4432 CC1101 NRF905 A7102 A7108 输出功率 20dBm 10dBm 10dBm 15dBm 20dBm 功耗 TX:85mA RX ...

  6. grok 正则解析日志例子<1>

    <pre name="code" class="html">下面是日志的样子 55.3.244.1 GET /index.html 15824 0. ...

  7. lucas模板

    ll PowMod(ll a,ll b,ll MOD){ ll ret=; while(b){ ) ret=(ret*a)%MOD; a=(a*a)%MOD; b>>=; } return ...

  8. UIPageViewController-浅析

    一.UIPageViewController概念   控件为我们提供了一种像翻书效果的一种控件.我们可以通过使用UIPageViewController控件,来完成类似图书一样的翻页控制方式.   二 ...

  9. Apache Lens —— 统计数据分析查询接口

    Lens 提供了一个统一数据分析接口.通过提供一个跨多个数据存储的单一视图来实现数据分析任务切分,同时优化了执行的环境.无缝的集成 Hadoop 实现类似传统数据仓库的功能. 该项目主要特性: 简单元 ...

  10. CentOS mini版安装后增加gcc编译环境

    使用如下命令即可: sudo yum install gcc gcc-c++ make -y