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. unknown argument: &#39;-websockets&#39;

    找到building setting找到other link flgs里把里面'-websockets删掉

  2. Shiro 学习应用(续)

    在前面的文章中为大家介绍了 Shrio 的基础概念.可能比較笼统.没有深入到开发过程的一些问题.如今集中在本帖中归纳一下有关问题. FormAuthenticationFilter 表单过滤器 表单过 ...

  3. 最长公共子序列(Swift版本)

    class Mark {     var count: Int     var type: Int         init(count: Int, type: Int) {         self ...

  4. C9---include,编译

    //main.c //include基本概念 //include是预处理指令,翻译之前会替换,编译之前左的处理,#都是预处理指令,翻译时候会添加别的内容进来. #include <stdio.h ...

  5. 客户端通过wcf来启动或者停止服务器上的windows service

    1.设置服务器上的windows service的security,下面的命令只能用cmd.exe来运行(以管理员模式) sc sdset "LISA_43_Dev_Batch" ...

  6. ES 處於“initializing”狀態,此時主節點正在嘗試將分片分配到集群中的數據節點。 如果您看到分片仍處於初始化或未分配狀態太長時間,則可能是您的集群不穩定的警告信號。

    指標要點: Cluster status: 如果集群狀態為黃色,則至少有一個副本分片未分配或丟失. 搜索結果仍將完成,但如果更多的分片消失,您可能會丟失數據. 紅色的群集狀態表示至少有一個主分片丟失, ...

  7. Visual Studio2013下Magick++配置方法

    声明:本文系作者原创,如需转载请保持文章完整并注明出处(http://blog.csdn.net/u010281174/article/details/52224829). ImageMagick是一 ...

  8. mysql(8.0.*版本 windows10 )忘记密码解决方案

    安装完mysql-8.0.13-winx64后,一些列的安装命令过后再执行mysql -uroot -p之后 报错了 what fuck 什么鬼,就是这个错 ERROR (): Access deni ...

  9. Java中的作用域有哪些

    在Java语言中,变量的类型主要有3种:成员变量.静态变量和局部变量 首先说静态变量跟局部变量 静态变量不依赖于特定的实例,而是被所有实例共享,也就是说,只要一个类被加载,JVM就会给类的静态变量分配 ...

  10. Java 系列之spring学习--springmvc注解参数传递(六)

    一.绑定参数注解如下 @RequestParam     绑定单个请求数据,既可以是URL中的参数,也可以是表单提交的参数或上传的文件. 它有三个属性:  value    用于设置参数名. defa ...