hdu 2604 Queuing(矩阵快速幂乘法)
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 a length L ( <= L <= ) and M.
Output K mod M( <= M <= ) where K is the number of E-queues with length L.
题意:
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(矩阵快速幂乘法)的更多相关文章
- HDU.2640 Queuing (矩阵快速幂)
HDU.2640 Queuing (矩阵快速幂) 题意分析 不妨令f为1,m为0,那么题目的意思为,求长度为n的01序列,求其中不含111或者101这样串的个数对M取模的值. 用F(n)表示串长为n的 ...
- HDU 2604 Queuing 矩阵高速幂
Queuing Time Limit: 10000/5000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Total S ...
- HDU 5667 构造矩阵快速幂
HDU 5667 构造矩阵快速幂 题目描述 解析 我们根据递推公式 设 则可得到Q的指数关系式 求Q构造矩阵 同时有公式 其中φ为欧拉函数,且当p为质数时有 代码 #include <cstdi ...
- HDU 6185 Covering 矩阵快速幂
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=6185 题意:用 1 * 2 的小长方形完全覆盖 4 * n的矩形有多少方案. 解法:小范围是一个经典题 ...
- HDU 2157(矩阵快速幂)题解
How many ways?? Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) ...
- HDU 6395 分段矩阵快速幂 HDU 6386 建虚点+dij
http://acm.hdu.edu.cn/showproblem.php?pid=6395 Sequence Time Limit: 4000/2000 MS (Java/Others) Me ...
- HDU 6470 【矩阵快速幂】
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=6470 写这道题是为了让自己不要忘记矩阵快速幂如何推出矩阵式子的. 注意 代码是TLE的!! #incl ...
- HDU 5607 graph 矩阵快速幂 + 快速幂
这道题得到了学长的助攻,其实就是一个马尔科夫链,算出一步转移矩阵进行矩阵快速幂就行了,无奈手残 这是我第一回写矩阵快速幂,写的各种毛病,等到调完了已经8点44了,交了一发,返回PE,(发现是少了换行) ...
- HDU 1575(裸矩阵快速幂)
emmmmm..就是矩阵快速幂,直接附代码: #include <cstdio> using namespace std; ; ; struct Matrix { int m[maxn][ ...
随机推荐
- inet address example(socket)
package com.opensource.socket; import java.net.Inet4Address; import java.net.Inet6Address; import ja ...
- wireshark 过滤条件汇总
原文地址:http://blog.const.net.cn/a/9340.htm 一.针对wireshark最常用的自然是针对IP地址的过滤.其中有几种情况: (1)对源地址为192.168.0.1的 ...
- hdu4331 Image Recognition 就暴力啊。。啊。。
题意: 给一个1和0组成的正方形矩阵,求 四条边都由1构成的正方形的个数. 方法: 先统计矩阵中每一点,向四个方向,最多有多少个连续的1,这里用dp做也 与此同时,顺便求下 能向右下和 左上 两个方向 ...
- gmapping 学习
为解决斜坡下gmapping定位的问题,开始关注gmapping. 先看到EAIPOT博客里关于gmapping的一个参数文件 <launch> <arg name="sc ...
- sort(水题)
sort Time Limit: 6000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) Total Submi ...
- 依赖注入及AOP简述(三)——依赖注入的原理
3. “依赖注入”登场 于是诸多优秀的IT工程师开始想出了更加轻量便利.更加具有可测试性和可维护性的设计模式——IoC模式.IoC,即Inversion of Control的缩写,中文里被称 ...
- SecureCRT 绝佳配色方案
终端有一个好的配色,不仅能保护自己的眼睛,也能给人一个好心情,本配色方案适合任意一种SSH客户端软件. 设置背景颜色 Options => Sessions options => Term ...
- c#创建带参数的线程
1.无参数线程的创建 Thread thread = new Thread(new ThreadStart(getpic)); thread.Start(); private void showmes ...
- javascript 里找元素操作元素
javascript 一.找到元素. var d = document.getElementById("") var d = document.getElementsByNa ...
- mongodb高可用集群搭建
集群构架图如下: 集群大致文件结构:(192.168.137.101节点) 先搭建3个副本集 rs1/mongod.conf rs1/start.sh rs2/mongod.conf 后面类似.... ...