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. Anaconda 环境中使用pip安装时候出现的一些问题

    author:pprp date:18/8/12 --- 1. AttributeError: Module Pip has no attribute 'main' solution:降低pip的版本 ...

  2. gdb 调试coredump文件过程:

    第一步:首先需要一个进程的coredump文件,怎么搞出coredump文件呢? 1. ps -fax|grep                 进程名称 找到进程的pid 2.gdb -p pid ...

  3. 比较好的Json 格式数据

    { "81040753986": [{ "order_info": { "unique_package_reference": " ...

  4. mongodb复制集开启安全认证

    之前我有一篇博客写的是“node.js通过权限验证连接MongoDB”,这篇博客上提到如何在启动文件中通过配置auth参数来开启权限认证,但这种认证方式只适合单机节点,当我们使用复制集时应该怎么开启权 ...

  5. java中使用Lambda表达式的5种语法

    1,标准写法 思考下述情况: String[] arr = {"program", "creek", "is", "a" ...

  6. jdk动态代理使用及原理

    jdk动态代理的使用 1.创建实现InvocationHandler接口的类,实现invoke(Object proxy, Method method, Object[] args)接口,其中invo ...

  7. Zeratul的完美区间(线段树||RMQ模板题)

    原题大意:原题链接 给定元素无重复数组,查询给定区间内元素是否连续 解体思路:由于无重复元素,所以如果区间内元素连续,则该区间内的最大值和最小值之差应该等于区间长度(r-l) 解法一:线段树(模板题) ...

  8. opencv颜色提取color filting

    # -*- coding: utf-8 -* import numpy as np import cv2 cap = cv2.VideoCapture(0) while True: _ , frame ...

  9. 记录selenium操作

    # -*- coding: utf-8 -*- # coding:utf-8 必须在第一行才能支持中文注释 #!/usr/bin/python # android-build.py # Build a ...

  10. 使用XMLHttpRequest对象完成原生的AJAX请求

    1.大家眼中的Ajax 说到Ajax,只要有过前端开发经验的童鞋一定都不陌生,大都知道它就是一种与后端之间的通信技术,通过这个神奇的家伙,我们不用像传统表单那样填完信息一点提交就呼啦呼啦跳转了.Aja ...