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. css 权威指南笔记

    部分属性选择: 选择class 属性中包含warning的元素 [class~="warning"]{font-weight:bold} 子串匹配属性选择器: 在现代浏览器中得到支 ...

  2. LInux50个基本命令

    cd:(切换)vim:(创建文件)   vi:编辑文件bc:(计算器)quit:退出计算器mkdir:(创建目录)   mkdir -p:递归建立目录rmdir:(删除目录)arch:(显示处理器X8 ...

  3. Apache的三种工作模式及相关配置

    Apache的三种工作模式 作为老牌服务器,Apache仍在不断地发展,就目前来说,它一共有三种稳定的MPM(Multi-Processing Module,多进程处理模块).它们分别是 prefor ...

  4. 18.一篇文章,从源码深入详解ThreadLocal内存泄漏问题

    1. 造成内存泄漏的原因? threadLocal是为了解决对象不能被多线程共享访问的问题,通过threadLocal.set方法将对象实例保存在每个线程自己所拥有的threadLocalMap中,这 ...

  5. spring boot 中logback多环境配置

    spring boot 配置logback spring boot自带了log打印功能,使用的是Commons logging 具体可以参考spring boot log 因此,我们只需要在resou ...

  6. centos7 iptables替换firewall

    Disable Firewalld Service. [root@rhel-centos7-tejas-barot-linux ~]# systemctl mask firewalld Stop Fi ...

  7. C# 实现QQ式截图功能

    这个功能一共有两部分组成,第一部分是窗体代码,另外的一部分是一个辅助方法.直接贴出代码,以供大家参考: using System; using System.Collections.Generic; ...

  8. Github上的iOS App源码 (中文)

    Github版英文App地址 中文 : TeamTalk 蘑菇街. 开源IM. 电商强烈推荐. MyOne-iOS 用OC写的<一个> iOS 客户端 zhihuDaily 高仿知乎日报 ...

  9. Fiddler进行模拟Post提交json数据,总为null解决方式(转)

    原文链接:http://www.cnblogs.com/jys509/p/3550598.html Request Headers: User-Agent: FiddlerHost: localhos ...

  10. 剑指offer--51.表示数值的字符串

    正则好舒服, ------------------------------------------------------------------------------------------ 时间 ...