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 2 L 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. 

InputInput a length L (0 <= L <= 10 6) and M.OutputOutput 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 下图是对矩阵的理解,对左边每一个f(n),需要m个f(x)就在第x排记录m。
如图,f(n)=x*f(n-1)+y*f(n-2)+z*f(n-3)...
本题的公式是f(n)=f(n-1)+f(n-3)+f(n-4),分别对最后一位是f或者讨论即可得出。
#include<cstdio>
#include<cstdlib>
#include<algorithm>
#include<memory>
#include<cstring>
#include<cmath>
using namespace std;
const int maxn=4;
int Mod;
struct mat
{
int m[maxn][maxn],len;
mat(){memset(m,0,sizeof(m));len=maxn;}
mat friend operator * (mat a,mat b){
mat d;
for(int i=0;i<a.len;i++)
for(int j=0;j<a.len;j++){
d.m[i][j]=0;
for(int k=0;k<a.len;k++)
d.m[i][j]+=(a.m[i][k]*b.m[k][j])%Mod;
}
return d;
}
mat friend operator^(mat a,int k) {
mat c;
for(int i=0;i<c.len;i++) c.m[i][i]=1;
while(k){
if(k&1) c=a*c;
a=a*a;
k>>=1;
}
return c;
}
};
int main()
{
int n,k;
mat ans,x,c;
x.m[0][0]=x.m[0][2]=x.m[0][3]=x.m[1][0]=x.m[2][1]=x.m[3][2]=1;
ans.m[0][0]=9;
ans.m[1][0]=6;
ans.m[2][0]=4;
ans.m[3][0]=2;
while(~scanf("%d%d",&n,&Mod)){
if (n<=4){
printf("%d\n",ans.m[4-n][0]%Mod);
}
else {
c=x^(n-4);
c=c*ans;
printf("%d\n",c.m[0][0]%Mod);
}
}
return 0;
}

HDU2604 Queuing 矩阵初识的更多相关文章

  1. hdu---(2604)Queuing(矩阵快速幂)

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

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

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

  3. HDU2604:Queuing(矩阵快速幂+递推)

    传送门 题意 长为len的字符串只由'f','m'构成,有2^len种情况,问在其中不包含'fmf','fff'的字符串有多少个,此处将队列换成字符串 分析 矩阵快速幂写的比较崩,手生了,多练! 用f ...

  4. HDU 2604 Queuing 矩阵高速幂

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

  5. [HDU2604]Queuing

    题目:Queuing 链接:http://acm.hdu.edu.cn/showproblem.php?pid=2604 分析: 1)将当前格和上一格合并当作一个状态,考虑下一个格子放0(m)还是1( ...

  6. HDU2604—Queuing

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2604 题目意思:n个人排队,f表示女,m表示男,包含子串‘fmf’和‘fff’的序列为O队列,否则为E ...

  7. HDU2604【矩阵快速幂】

    思路: 把fm看成01,f-1,m-0: 不能存在101,111; dp[i]代表第i结尾的方案数: ①:结尾是0一定行:只要i-1序列里添个0就好了,dp[i]+=dp[i-1]: ②:结尾是1   ...

  8. 【UE4】GAMES101 图形学作业0:矩阵初识

    作业描述 给定一个点P=(2,1), 将该点绕原点先逆时针旋转45◦,再平移(1,2), 计算出变换后点的坐标(要求用齐次坐标进行计算). UE4 知识点 主要矩阵 FMatrix FBasisVec ...

  9. hdu1575 Tr A 矩阵初识

    A为一个方阵,则Tr A表示A的迹(就是主对角线上各项的和),现要求Tr(A^k)%9973.  Input数据的第一行是一个T,表示有T组数据. 每组数据的第一行有n(2 <= n <= ...

随机推荐

  1. codeforces GYM 100971F 公式题或者三分

    F. Two Points time limit per test 2 seconds memory limit per test 256 megabytes input standard input ...

  2. Vue 备

    <div id="app"> <span :class='{red:addClass}'>jam</span> </div> < ...

  3. 新浪云连接数据库php

    一般数据库连接$con = mysql_connect("localhost", "root", ""); 而新浪云共享数据库 <?p ...

  4. ACM ICPC Central Europe Regional Contest 2013 Jagiellonian University Kraków

    ACM ICPC Central Europe Regional Contest 2013 Jagiellonian University Kraków Problem A: Rubik’s Rect ...

  5. SPOJ-ANDROUND -线段树/与操作

    ANDROUND - AND Rounds #tree You are given a cyclic array A having N numbers. In an AND round, each e ...

  6. 玲珑oj 1128 RMQ模板

    1128 - 咸鱼拷问 Time Limit:3s Memory Limit:128MByte Submissions:380Solved:118 DESCRIPTION 给你两个序列A,B.每个序列 ...

  7. Shell awk文本处理,shell脚本编写

    Shell awk文本处理,shell脚本编写 一:内容包含awk.变量.运算符.if多分支 <a>语法糖: awk [options] 'commands' files option - ...

  8. opengl:初次接触

    1.概述 OpenGL(Open Graphics Library),开放的图形程序接口,是编程接口的规范,并不是像OpenCV那样是库.GLFW是开源的基于opengl标准的库,并且是跨平台的.其开 ...

  9. laravel5.5中查询构造器的使用

    //查询构造器新增数据: public function query1() { /* $bool=DB::table('student')->insert( ['name'=>'小李',' ...

  10. 【转】netlink socket编程实例

    [转]netlink socket编程实例 转自:http://blog.chinaunix.net/uid-14753126-id-2983915.html 关于Netlink IPC方式的介绍,请 ...