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

  1. 3 8
  2. 4 7
  3. 4 8

Sample Output

  1. 6
  2. 2
  3. 1
  4.  
  5. 下图是对矩阵的理解,对左边每一个f(n),需要mf(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或者讨论即可得出。
  6.  
  1. #include<cstdio>
  2. #include<cstdlib>
  3. #include<algorithm>
  4. #include<memory>
  5. #include<cstring>
  6. #include<cmath>
  7. using namespace std;
  8. const int maxn=4;
  9. int Mod;
  10. struct mat
  11. {
  12. int m[maxn][maxn],len;
  13. mat(){memset(m,0,sizeof(m));len=maxn;}
  14. mat friend operator * (mat a,mat b){
  15. mat d;
  16. for(int i=0;i<a.len;i++)
  17. for(int j=0;j<a.len;j++){
  18. d.m[i][j]=0;
  19. for(int k=0;k<a.len;k++)
  20. d.m[i][j]+=(a.m[i][k]*b.m[k][j])%Mod;
  21. }
  22. return d;
  23. }
  24. mat friend operator^(mat a,int k) {
  25. mat c;
  26. for(int i=0;i<c.len;i++) c.m[i][i]=1;
  27. while(k){
  28. if(k&1) c=a*c;
  29. a=a*a;
  30. k>>=1;
  31. }
  32. return c;
  33. }
  34. };
  35. int main()
  36. {
  37. int n,k;
  38. mat ans,x,c;
  39. 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;
  40. ans.m[0][0]=9;
  41. ans.m[1][0]=6;
  42. ans.m[2][0]=4;
  43. ans.m[3][0]=2;
  44. while(~scanf("%d%d",&n,&Mod)){
  45. if (n<=4){
  46. printf("%d\n",ans.m[4-n][0]%Mod);
  47. }
  48. else {
  49. c=x^(n-4);
  50. c=c*ans;
  51. printf("%d\n",c.m[0][0]%Mod);
  52. }
  53. }
  54. return 0;
  55. }

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. 【eclipse】Multiple annotations found at this line:——解决方法

    问题截图: 就是eclipse的maven插件太旧了 用新插件新建的maven项目就没有报错 用软件对比了一下这两个pom文件 只有项目名有区别 所以就是插件的问题 一个简单安装离线maven插件的方 ...

  2. DataTable扩展:转化实体ToList

    直接上代码: 根据属性进行映射:DataTable转化成实体List public static class DataTableExtension { public static List<T& ...

  3. wamp升级php7

    原文:http://blog.csdn.net/cheng6251/article/details/50730441 1.下载php7   http://windows.PHP.net/downloa ...

  4. ANDROID教程目录

    html5 如何打包成apk,将H5封装成android应用APK文件的几种方法    

  5. IDEA.环境

    1.下载: https://www.jetbrains.com/idea/download/#section=windows 安装的版本是:Ultimate:ideaIU-2017.3.3.exe 2 ...

  6. 初识HTML和CSS2

    上节作业问题: 1.css重用 <style> 如果整个页面的宽度 > 900px时: { .c{ 共有 } .c1{ 独有 } } .c2{ 独有 } </style> ...

  7. iPhone 和Android应用,特殊的链接:打电话,短信,email

    下面的这篇文章主要是说,网页中的链接如何写,可以激活电话的功能. 例如,页面中展示的是一个电话号码,当用户在手机浏览器里面点击这个电话号码的时候,手机会弹出拨号的面板,或者是短信程序会启动等. 1. ...

  8. bzoj1007: [HNOI2008]水平可见直线 单调栈维护凸壳

    在xoy直角坐标平面上有n条直线L1,L2,...Ln,若在y值为正无穷大处往下看,能见到Li的某个子线段,则称Li为可见的,否则Li为被覆盖的.例如,对于直线:L1:y=x; L2:y=-x; L3 ...

  9. 河南省多校联盟二-A

    1279: 简单的背包问题 时间限制: 1 秒  内存限制: 32 MB提交: 361  解决: 20 题目描述 相信大家都学过背包问题了吧,那么现在我就考大家一个问题.有n个物品,每个物品有它的重量 ...

  10. clientWidth、offsetWidth等介绍

    网页可见区域宽:document.body.clientWidth网页可见区域高:document.body.clientHeight网页可见区域宽:document.body.offsetWidth ...