[hdu 2604] Queuing 递推 矩阵快速幂
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.
4 7
4 8
2
1

直接递推:
#include <iostream>
#include <stdio.h>
#include <cstring>
#include <algorithm>
using namespace std; int L, M; int solve()
{
int a[];
a[] = ; a[] = ;
a[] = ; a[] = ;
for (int i = ; i <= L; i++) {
a[i] = a[i-]+a[i-]+a[i-];
a[i] %= M;
}
return a[L];
} int main()
{
//freopen("1.txt", "r", stdin);
while (~scanf("%d%d", &L, &M)) {
printf("%d\n", solve());
} return ;
}
矩阵快速幂
#include <iostream>
#include <cstring>
#include <stdio.h>
#include <algorithm>
#include <math.h>
using namespace std;
#define LL long long
const int Max = ;
int L, M;
struct Mat
{
LL m[Max][Max]; void clear() {
memset(m, , sizeof(m));
} void Init() {
clear();
for (int i = ; i < Max; i++)
m[i][i] = ;
} }; Mat operator * (Mat a, Mat b)
{
Mat c;
c.clear();
for (int i = ; i < Max; i++)
for (int j = ; j < Max; j++)
for (int k = ; k < Max; k++) {
c.m[i][j] += (a.m[i][k]*b.m[k][j])%M;
c.m[i][j] %= M;
}
return c;
} Mat quickpow(Mat a, int k)
{
Mat ret;
ret.Init();
while (k) {
if (k & )
ret = ret*a;
a = a*a;
k >>= ;
}
return ret;
} int main()
{
//freopen("1.txt", "r", stdin);
Mat a, b, c;
a.clear(); b.clear(); c.clear();
a.m[][] = ; a.m[][] = ;
a.m[][] = ; a.m[][] = ; b.m[][] = b.m[][] = b.m[][] =
b.m[][] = b.m[][] = b.m[][] = ; while (~scanf("%d%d", &L, &M)) {
LL ret;
if (L == )
ret = ;
else if (L <= )
ret = a.m[-L][]%M;
else {
c = quickpow(b, L-);
c = c*a;
ret = c.m[][]%M;
}
printf("%lld\n", ret);
} return ;
}
[hdu 2604] Queuing 递推 矩阵快速幂的更多相关文章
- HDU 2604 Queuing(递推+矩阵)
Queuing [题目链接]Queuing [题目类型]递推+矩阵 &题解: 这题想是早就想出来了,就坑在初始化那块,只把要用的初始化了没有把其他的赋值为0,调了3,4个小时 = = 本题是可 ...
- HDU 2604 Queuing( 递推关系 + 矩阵快速幂 )
链接:传送门 题意:一个队列是由字母 f 和 m 组成的,队列长度为 L,那么这个队列的排列数为 2^L 现在定义一个E-queue,即队列排列中是不含有 fmf or fff ,然后问长度为L的E- ...
- hdu 2604 Queuing(动态规划—>矩阵快速幂,更通用的模版)
题目 最早不会写,看了网上的分析,然后终于想明白了矩阵是怎么出来的了,哈哈哈哈. 因为边上的项目排列顺序不一样,所以写出来的矩阵形式也可能不一样,但是都是可以的 //愚钝的我不会写这题,然后百度了,照 ...
- HDU Queuing(递推+矩阵快速幂)
Queuing Time Limit: 10000/5000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Su ...
- hdu 2604 递推 矩阵快速幂
HDU 2604 Queuing (递推+矩阵快速幂) 这位作者讲的不错,可以看看他的 #include <cstdio> #include <iostream> #inclu ...
- HDU 5950 Recursive sequence 【递推+矩阵快速幂】 (2016ACM/ICPC亚洲区沈阳站)
Recursive sequence Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Other ...
- HDU - 2604 Queuing(递推式+矩阵快速幂)
Queuing Time Limit: 10000/5000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Su ...
- HDU 2842 (递推+矩阵快速幂)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2842 题目大意:棒子上套环.第i个环能拿下的条件是:第i-1个环在棒子上,前i-2个环不在棒子上.每个 ...
- 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] 类似于斐波那契数列的递推式子吧, 但 ...
随机推荐
- Angular5学习笔记 - 创建组件(四)
一.创建组件 ng generate component component-name #简写 ng g c component-name ng generate directive|pipe|ser ...
- java代码继承------多层继承
总结:继承.方法的重要性, 运行结果显示: class A is callingclass B is callingclass C is calling package com.addd; //jav ...
- #pragma pack 在BITMAP结构体定义中的使用
BITMAP位图文件主要分为如下3个部分: 块名称 对应Windows结构体定义 大小(Byte) 文件信息头 BITMAPFILEHEADER 14 位图信息头 BITMAPINFOHEADER 4 ...
- 如何同时打开两个UltraEdit
高级—配置—应用程序布局—其它,勾上资源管理器选的文件将以独立的UltraEdit打开和允许多个UltraEdit
- Laravel 在 with 查询中只查询个别字段
在使用 Laravel 的关联查询中,我们经常使用 with 方法来避免 N+1 查询,但是 with 会将目标关联的所有字段全部查询出来,对于有强迫症的我们来说,当然是不允许的. 这时候我们可以使用 ...
- python 函数相关定义
1.为什么要使用函数? 减少代码的冗余 2.函数先定义后使用(相当于变量一样先定义后使用) 3.函数的分类: 内置函数:python解释器自带的,直接拿来用就行了 自定义函数:根据自己的需求自己定义的 ...
- 服务器启动时Webapp的web.xml中配置的加载顺序
一 1.启动一个WEB项目的时候,WEB容器会去读取它的配置文件web.xml,读取<listener>和<context-param>两个结点. 2.紧急着,容创建一个Ser ...
- Eclipse: “The import java.io cannot be resolved”
检查一下选项: 重点看jdk的绑定 43down voteaccepted Check your Eclipse preferences: Java -> Installed JREs. The ...
- NDK 编译报错:request for member 'FindClass' in something not a structure or union
ndk编译 xx.c文件时一直报下面的错误: ”request for member 'FindClass' in something not a structure or union ...” 原因 ...
- (转)Linux网络协议栈(三)——网络设备(1)
网络设备(network device)是内核对网络适配器(硬件)的抽象与封装,并为各个协议实例提供统一的接口,它是硬件与内核的接口,它有两个特征:(1) 作为基于硬件的网络适配器与基于软件的协 ...