Queuing

Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)

Total Submission(s): 3032    Accepted Submission(s): 1379

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
 

FFF FMF

一.找递推公式

考虑最后若直接加M 显然可以成立 所以F[n]=F[n-1]+X

若要在后面加F 考虑N-1是没办法的

考虑N-2 FF MF 依旧没办法

考虑N-3 FFF FMF MMF MFF (FFF,FMF 已经不可能 不考虑)

显然添加MMF 前面N-3无论是什么都不可能会出现FFF,FMF了

所以F[n]=F[n-1]+F[n-3]+X

但是 若添加MFF 可能会出现FMFF

考虑N-4 FMFF MMFF 显然MMFF可以取了

所以 F[n]=F[n-1]+F[n-3]+F[n-4]

二.设计矩阵

注:矩阵十分好设计  第一排即为递推公式的系数

以后几排类似于单位矩阵

三.矩阵快速幂

类似非递归快速幂

node kuaisumi(node A,int N,int mod)
{
node di=e;
while(N>0)
{
if(N&1)
{
di=MatrixMult(di,A,mod);
}
A=MatrixMult(A,A,mod);
N=N>>1;
}
return di;
}

最后代码如下:

#include <cstdio>
#include <cstdlib>
#include <cmath>
#include <cstring>
#include <ctime>
#include <algorithm>
#include <iostream>
#include <sstream>
#include <string>
#define oo 0x13131313
using namespace std;
int L,M;
struct node
{
int mat[5][5];
}a,e,ans;
int mat2[5];
void CSH()
{
mat2[1]=9;
mat2[2]=6;
mat2[3]=4;
mat2[4]=2;
for(int i=1;i<=4;i++)
{
e.mat[i][i]=1;
mat2[i]=mat2[i]%M;
}
memset(a.mat,0,sizeof(a.mat));
a.mat[1][1]=a.mat[1][3]=a.mat[1][4]=1;
a.mat[2][1]=a.mat[3][2]=a.mat[4][3]=1;
}
node MatrixMult(node A,node B,int mod)
{
node p;
memset(p.mat,0,sizeof(p.mat));
for(int i=1;i<=4;i++)
for(int j=1;j<=4;j++)
{
for(int k=1;k<=4;k++)
p.mat[i][j]+=A.mat[i][k]*B.mat[k][j];
p.mat[i][j]=p.mat[i][j]%mod;
}
return p;
}
node kuaisumi(node A,int N,int mod)
{
node di=e;
while(N>0)
{
if(N&1)
{
di=MatrixMult(di,A,mod);
}
A=MatrixMult(A,A,mod);
N=N>>1;
}
return di;
}
void solve()
{
int ANS=0;
for(int i=1;i<=4;i++)
{
ANS+=ans.mat[1][i]*mat2[i];
ANS=ANS%M;
}
printf("%d\n",ANS);
}
int main()
{
while(cin>>L>>M)
{
CSH();
if(L>4)
ans=kuaisumi(a,L-4,M);
if(L>4)
solve();
else
printf("%d\n",mat2[L]);
}
return 0;
}

【递推+矩阵快速幂】【HDU2604】【Queuing】的更多相关文章

  1. hdu 2604 递推 矩阵快速幂

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

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

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

  3. hdu2604(递推,矩阵快速幂)

    题目链接:hdu2604 这题重要的递推公式,找到公式就很easy了(这道题和hdu1757(题解)类似,只是这道题需要自己推公式) 可以直接找规律,推出递推公式,也有另一种找递推公式的方法:(PS: ...

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

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

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

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

  6. 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] 类似于斐波那契数列的递推式子吧, 但 ...

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

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

  8. 五校联考R1 Day1T3 平面图planar(递推 矩阵快速幂)

    题目链接 我们可以把棱柱拆成有\(n\)条高的矩形,尝试递推. 在计算的过程中,第\(i\)列(\(i\neq n\))只与\(i-1\)列有关,称\(i-1\)列的上面/下面为左上/左下,第\(i\ ...

  9. LightOJ 1244 - Tiles 猜递推+矩阵快速幂

    http://www.lightoj.com/volume_showproblem.php?problem=1244 题意:给出六种积木,不能旋转,翻转,问填充2XN的格子有几种方法.\(N < ...

随机推荐

  1. log4net使用简介

    平常我们在开发网站时,有一些比较重要的地方需要添加日志记录.一般日志记录分为两种:1)在数据库中添加一张日志表,用来记录用户操作并给用户提醒(用户可以看到). 2)在系统中添加一个日志文件,用来记录一 ...

  2. ios9基础知识(UI)总结

    UIWindow.UILabel.UIColor.UIScreen.UIViewController.UIView.UIControl.UIButton.IBOutlet.IBAction.UISte ...

  3. iOS 性能优化:Instruments

    对于每位 iOS 开发者来说,代码性能是个避不开的话题.随着项目的扩大和功能的增多,没经过认真调试和优化的代码,要么任性地卡顿运行,要么低调地崩溃了之……结果呢,大家用着不高兴,开发者也不开心. 其实 ...

  4. iOS学习笔记-死锁deadlock理解

    1.首先看一下官方文档的解释,这个block的队列是同步执行的,不像异步,这个方法直到block执行完毕才会返回 2.主线程一旦开启,就要先把自己的代码执行完成之后,才去执行加入到主队列中的任务 De ...

  5. HDU 5724 - Chess

    题意:    一个n行20列的棋盘. 每一行有若干个棋子.     两人轮流操作, 每人每次可以将一个棋子向右移动一个位置, 如果它右边有一个棋子, 就跳过这个棋子, 如果有若干个棋子, 就将这若干个 ...

  6. Oprofile安装与使用探索

    本文分别尝试了oprofile在x86平台和龙芯平台上的安装 一:oprofile的安装与配置(intel+ubuntu12.04) I. Oprofile 安装 Oprofile 包含在 Linux ...

  7. [转载]字符编码笔记:ASCII,Unicode和UTF-8

    [转载] :http://www.ruanyifeng.com/blog/2007/10/ascii_unicode_and_utf-8.html 1. ASCII码 在计算机内部,所有的信息最终都表 ...

  8. centos 7 epel地址

    wget http://dl.fedoraproject.org/pub/epel/7/x86_64/e/epel-release-7-5.noarch.rpm

  9. 关于微信小程序的一些思考

    ### 怎么样理解小程序? * 微信的重点产品* 一个事实OS,目前并不知道小程序的入口在哪里?* 小程序的入口可能在如下三个地方: 1. 发现入口 2. 扫码 3. 搜索框 * 小程序没有关注, 意 ...

  10. 解决ERROR C2011: 'FD_SET' : 'STRUCT' TYPE REDEFINITION问题

    在socket编程的过程中头文件中 #include <windows.h> #include "stdafx.h" #include "WinSock2.h ...