Queuing

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

Total Submission(s): 2773    Accepted Submission(s): 1275

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

记答案为f[n],则易得f[0]=0,f[1]=2,f[2]=4,f[3]=6;f[[4]=9;

当长度为N时,若最后一个字符为M。前N-1个字符没有限制,即为F(N-1);

当最后一个字符串为F的时候,就必须去除最后3个字符是fmf和fff的情况(倒数第二个字符为F、M均有可能会不满足情况),此时最后3个字符可能为mmf和mff。

当后3个字符为mmf时。前N-3个字符没有限制。即F(N-3);

可是当最后四个字符为mmff时,前N-4个字符无限制,即为F(N-1);

即f[n]=f[n-1]+f[n-3]+f[n-4];

转化为矩阵即为:

1 0 1 1       F(N-1)  F(N)      (即是f[n]=1*f[n-1]+0*f[n-2]+1*f[n-3]+1*f[n-4];)
   

1 0 0 0  *    F(N-2)  = F(N-1)   (以下为单位矩阵)

0 1 0 0       F(N-3)  F(N-2)

0 0 1 0       F(N-4)  F(N-3)

#include"iostream"
#include"stdio.h"
#include"string.h"
#include"algorithm"
#include"queue"
#include"vector"
using namespace std;
#define N 4
#define LL __int64
struct Mat
{
LL mat[N][N];
};
int M,n=4;
int p[5]={0,2,4,6,9};
Mat operator *(Mat a,Mat b)
{
int i,j,k;
Mat c;
memset(c.mat,0,sizeof(c.mat));
for(i=0;i<n;i++)
{
for(j=0;j<n;j++)
{
c.mat[i][j]=0;
for(k=0;k<n;k++)
{
c.mat[i][j]+=(a.mat[i][k]*b.mat[k][j])%M;
}
c.mat[i][j]%=M;
}
}
return c;
}
int fun(Mat &a,int k)
{
int i;
Mat ans;
memset(ans.mat,0,sizeof(ans.mat));
for(i=0;i<n;i++)
ans.mat[i][i]=1;
while(k)
{
if(k&1)
ans=ans*a;
k>>=1;
a=a*a;
}
LL s=0;
for(i=0;i<n;i++)
{
s+=ans.mat[0][i]*p[n-i];
s%=M;
}
return s;
}
int main()
{
int i,l;
Mat a;
while(scanf("%d%d",&l,&M)!=-1)
{
if(l<=n)
{
printf("%d\n",p[l]%M);
continue;
}
memset(a.mat,0,sizeof(a.mat));
a.mat[0][0]=a.mat[0][2]=a.mat[0][3]=1;
for(i=1;i<n;i++)
a.mat[i][i-1]=1;
printf("%d\n",fun(a,l-4));
}
return 0;
}

hdu 2604 Queuing (矩阵高速幂)的更多相关文章

  1. HDU 2604 Queuing 矩阵高速幂

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

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

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

  3. ZOJ 3690 &amp; HDU 3658 (矩阵高速幂+公式递推)

    ZOJ 3690 题意: 有n个人和m个数和一个k,如今每一个人能够选择一个数.假设相邻的两个人选择同样的数.那么这个数要大于k 求选择方案数. 思路: 打表推了非常久的公式都没推出来什么可行解,好不 ...

  4. HDU 2604 Queuing,矩阵高速幂

    题目地址:HDU 2604 Queuing 题意:  略 分析: 易推出:   f(n)=f(n-1)+f(n-3)+f(n-4) 构造一个矩阵: 然后直接上板子: /* f[i] = f[i-1] ...

  5. hdu 3221 Brute-force Algorithm(高速幂取模,矩阵高速幂求fib)

    http://acm.hdu.edu.cn/showproblem.php?pid=3221 一晚上搞出来这么一道题..Mark. 给出这么一个程序.问funny函数调用了多少次. 我们定义数组为所求 ...

  6. HDU 1575 Tr A(矩阵高速幂)

    题目地址:HDU 1575 矩阵高速幂裸题. 初学矩阵高速幂.曾经学过高速幂.今天一看矩阵高速幂,原来其原理是一样的,这就好办多了.都是利用二分的思想不断的乘.仅仅只是把数字变成了矩阵而已. 代码例如 ...

  7. HDU 2256 Problem of Precision(矩阵高速幂)

    题目地址:HDU 2256 思路: (sqrt(2)+sqrt(3))^2*n=(5+2*sqrt(6))^n; 这时要注意到(5+2*sqrt(6))^n总能够表示成an+bn*sqrt(6); a ...

  8. HDU 2254 奥运(矩阵高速幂+二分等比序列求和)

    HDU 2254 奥运(矩阵高速幂+二分等比序列求和) ACM 题目地址:HDU 2254 奥运 题意:  中问题不解释. 分析:  依据floyd的算法,矩阵的k次方表示这个矩阵走了k步.  所以k ...

  9. HDU 1588 Gauss Fibonacci(矩阵高速幂+二分等比序列求和)

    HDU 1588 Gauss Fibonacci(矩阵高速幂+二分等比序列求和) ACM 题目地址:HDU 1588 Gauss Fibonacci 题意:  g(i)=k*i+b;i为变量.  给出 ...

  10. hdu 4549 M斐波那契数列(矩阵高速幂,高速幂降幂)

    http://acm.hdu.edu.cn/showproblem.php?pid=4549 f[0] = a^1*b^0%p,f[1] = a^0*b^1%p,f[2] = a^1*b^1%p... ...

随机推荐

  1. Struts2概述及与Struts1的对照

    Struts2 概述 1,仍然是一个基于请求响应的MVC框架 2,Struts2不是Struts1的升级 3,Struts2与Struts1的体系结构差距非常大 4,Struts2採用了还有一个MVC ...

  2. JNI DETECTED ERROR IN APPLICATION: input is not valid Modified UTF-8: illegal start byte 0xfe

    JNI DETECTED ERROR IN APPLICATION: input is not valid Modified UTF-8: illegal start byte 0xfe 在使用Jni ...

  3. 基于QMP实现对qemu虚拟机进行交互

    本文详解QMP,包含qmp.hmp.qemu-guest-agent的介绍.工作原理.配置方法.范例 小慢哥的原创文章,欢迎转载 目录 ▪ QMP介绍 ▪ QMP语法 ▪ 单独使用qemu,启用QMP ...

  4. php基础知识(一)--2017-04-14

    1.Php的两种打开方式: 第一种方式:地址栏打开:http://localhost/0414/qq.php     地址栏输入localhost/  就是phpstudy下的www文件夹 第二种:新 ...

  5. ros中文术语表及消息类型表

    前言:整理一些ros常用表格,包括中文术语对照表. 一.中文术语表 二.消息类型表 -END-

  6. Java NIO(五)套接字通道

    Socket通道 Socket通道和文件通道有着不一样的特征: Socket通道类可以运行于非阻塞模式,并且是可选的.这两个特征可以激活大程序(如网络服务和中间件组件)巨大的可伸缩性和灵活性,因此再也 ...

  7. JavaScript 获取某个字符的 Unicode 码

    function getUnicode (charCode) { return charCode.charCodeAt(0).toString(16); } 获取的是 UTF-16 编码的值,不足4位 ...

  8. 基于cxf的webService服务发布及客户端开发

    学习地址: http://www.cnblogs.com/leihenqianshang/category/795140.html

  9. Pyhton二级操作题练习

    # 1.编写一个python程序,输入两个数,比较它们的大小并输出其中较大者. num1 = input('请输入数字X:') num2 = input('请输入数字Y:') if num1.isde ...

  10. HDU 2300 Crashing Robots

    Crashing Robots 题意 模拟多个机器人在四个方向的移动,检测crash robot, crash wall, OK这些状态 这是个模拟题需要注意几点: 理解转变方向后移动多少米,和转动方 ...