Queuing

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

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
题意:一个长度为L的串,只能由f和m组成,且子串中不能出现fff和fmf,输出有几种组合方式
解题思路:对于f(n),如果该串最后一个字母是m,那么前面n-1个字母组成的串只要符合组合方式即可,那么+f(n-1),如果最后一个字母是f,最后三个字母能组成mmf,mff才有可能符合组合方式,mmf对于他前面n-3个字母同样只要满足组合方式就行,+f(n-3),对于mff,前面一个字母肯定不能是f,那么就是最后四个字母为mmff,对于前面n-4个字母满足组合方式即可,+f(n-4),至此推出f(n)=f(n-1)+f(n-3)+f(n-4)
建立转移矩阵
0 1 0 0    
0 0 1 0 
0 0 0 1
1 1 0 1
建立初始矩阵
f(1)
f(2)
f(3)
f(4)
 #include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
#define INF 0X3f3f3f3f
const ll MAXN = ;
// const ll mod = 10000;
int mod;
//矩阵的大小 模数
ll n;
struct MAT
{
int mat[MAXN][MAXN];
MAT operator*(const MAT &a) const
{
//重载矩阵乘法
MAT b;
memset(b.mat, , sizeof(b.mat));
for (int i = ; i < MAXN; i++)
{
for (int j = ; j < MAXN; j++)
{
for (int k = ; k < MAXN; k++)
b.mat[i][j] = (b.mat[i][j] + mat[i][k] * a.mat[k][j]);
b.mat[i][j] += mod;
b.mat[i][j] %= mod;
}
}
return b;
}
} start, ans;
MAT Mqpow(MAT base, int b)
{
MAT r;
memset(r.mat, , sizeof(r.mat));
r.mat[][] = , r.mat[][] = , r.mat[][] = , r.mat[][] = ;
//初始状态
while (b)
{
if (b & )
r = base * r;
base = base * base;
b >>= ;
}
return r;
}
int main()
{ start.mat[][] = , start.mat[][] = , start.mat[][] = , start.mat[][] = ;
start.mat[][] = , start.mat[][] = , start.mat[][] = , start.mat[][] = ;
start.mat[][] = , start.mat[][] = , start.mat[][] = , start.mat[][] = ;
start.mat[][] = , start.mat[][] = , start.mat[][] = , start.mat[][] = ;
//建立转移矩阵
int f[] = {, , , , };
while (~scanf("%d%d", &n, &mod))
{
if (n <= )
printf("%d\n", f[n] % mod);
else
printf("%d\n", Mqpow(start, n - ).mat[][]);
}
return ;
}

HDU Queuing(递推+矩阵快速幂)的更多相关文章

  1. hdu 2604 递推 矩阵快速幂

    HDU 2604 Queuing (递推+矩阵快速幂) 这位作者讲的不错,可以看看他的 #include <cstdio> #include <iostream> #inclu ...

  2. [hdu 2604] Queuing 递推 矩阵快速幂

    Problem Description Queues and Priority Queues are data structures which are known to most computer ...

  3. HDU 2842 (递推+矩阵快速幂)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2842 题目大意:棒子上套环.第i个环能拿下的条件是:第i-1个环在棒子上,前i-2个环不在棒子上.每个 ...

  4. Recursive sequence HDU - 5950 (递推 矩阵快速幂优化)

    题目链接 F[1] = a, F[2] = b, F[i] = 2 * F[i-2] + F[i-1] + i ^ 4, (i >= 3) 现在要求F[N] 类似于斐波那契数列的递推式子吧, 但 ...

  5. hdu 6185 递推+矩阵快速幂

    思路:考虑全部铺满时,前2列的放法.有如下5种情况:(转自http://blog.csdn.net/elbadaernu/article/details/77825979 写的很详细 膜一下)  假设 ...

  6. HDU 5950 Recursive sequence 【递推+矩阵快速幂】 (2016ACM/ICPC亚洲区沈阳站)

    Recursive sequence Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Other ...

  7. HDU - 6185 Covering(暴搜+递推+矩阵快速幂)

    Covering Bob's school has a big playground, boys and girls always play games here after school. To p ...

  8. 【递推+矩阵快速幂】【HDU2604】【Queuing】

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

  9. HDU6030 Happy Necklace(递推+矩阵快速幂)

    传送门:点我 Little Q wants to buy a necklace for his girlfriend. Necklaces are single strings composed of ...

随机推荐

  1. F#周报2019年第51&52期

    新闻 介绍DataFrame 介绍System.Threading.Channels ConfigureAwait常见问题 第三次年度C#降临节 .NET会议--2020年1月14日 如何找到F#的工 ...

  2. 页面显示jsp源码问题

    问题错误在于WEB.XML 将/*改为*即可

  3. spring boot 中事物的使用

    一.什么是事务? 事务,通俗的说就是,同时做多个事,要么全做,要么不做,也是其特性.举个例子来说,好比你在某宝.某东.某多上购物,在你提交订单的时候,库存也会相应减少,不可能是钱付了,库存不减少,或者 ...

  4. hexo+next 详细搭建

    安装node node下载地址:http://nodejs.cn/download/ 具体安装方法,这里不做详写 安装完成可以通过node -v 查看安装是否生效和node的版本 我这里使用的是v10 ...

  5. $Noip2012/Luogu1083$ 借教室

    $Luogu$ $Sol$ 区间整体加减? 差分+树状数组维护前缀和! 那每给一个人借完教室之后都要判断一下现在合不合法?那复杂度比暴力还不如些... 注意到这里的单调性,假设给前$x$个人借完教室之 ...

  6. java做插入时ID为自增获取到ID

    <selectKey keyProperty="id" resultType="int" order="AFTER"> sele ...

  7. SpringBoot基础架构篇1(SpringBoot、MyBatis-Plus与Thymeleaf)

    show me the code and talk to me,做的出来更要说的明白 我是布尔bl,你的支持是我分享的动力! 1 引入 使用 MyBatis-Plus 以及 thymeleaf 实现增 ...

  8. 洛谷P1832 A+B Problem(再升级) 题解 完全背包方案计数

    题目链接:https://www.luogu.com.cn/problem/P1832 题目大意: 给定一个正整数n,求将其分解成若干个素数之和的方案总数. 解题思路: 首先找到所有 \(\le n\ ...

  9. spring cloud微服务快速教程之(四)熔断器(Hystrix)及其工具(Dashboard、Turbine)

    0-为什么需要熔断器 在分布式系统中,各个服务相互调用相互依赖,如果某个服务挂了,很可能导致其他调用它的一连串服务也挂掉或者在不断等待中耗尽服务器资源,这种现象称之为雪崩效应: 未来防止系统雪崩,熔断 ...

  10. 使用vue-baidu-map解析geojson

    这是后台给我的gejson: {"type":"FeatureCollection","features":[{"type&quo ...