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. Cocos2d-x 3.0 红孩儿私家必修 - 第一章 初识Cocos2d-x 3.0project

    第一章    初识Cocos2d-x 3.0project Cocos2d-x 3.0出来了,听说与之前版本号相比修改较大 做为一个游戏开发人员.我们应该欢迎Cocos2d-x持续的更新和强大,Coc ...

  2. 40.DOM读取XML

    main.cpp #include <QtGui> #include <iostream> #include "domparser.h" int main( ...

  3. 前后端分离之接口登陆权限token

    随着业务的需求普通的springmvc+jsp已经不能满足我们的系统了,会逐渐把后台和前端展示分离开来,下面我们就来把普通的springmvc+jsp分为 springmvc只提供rest接口,前端用 ...

  4. Redis的配置文件详解

    daemonize:如需要在后台运行,把该项的值改为yes pdifile:把pid文件放在/var/run/redis.pid,可以配置到其他地址 bind:指定redis只接收来自该IP的请求,如 ...

  5. ES6的基本语法

    ES6 详细参考页面 简介 ECMAScript和JavaScript的关系是,前者是后者的规格,后者是前者的一种实现.一般来说,这两个词是可以互换的. let命令 ES6新增了let命令,用来声明变 ...

  6. javascript中client()兼容性封装

    function client() { var clientWidth = window.innerWidth || document.documentElement.clientWidth || d ...

  7. 「JavaSE 重新出发」05.02 泛型数组列表、包装类

    泛型数组列表 ArrayList 是一个采用类型参数(type parameter)的泛型类(generic class). java ArrayList<Employee> staff ...

  8. bootstrap-导航条

    默认样式的导航条 导航条是在您的应用或网站中作为导航页头的响应式基础组件.它们在移动设备上可以折叠(并且可开可关),且在视口(viewport)宽度增加时逐渐变为水平展开模式. 两端对齐的导航条导航链 ...

  9. Lambda 表达式-即匿名函数

    拉姆达值(Lambda),希腊字母表示为Λ,指与真空的空间有关的能量或暗能量.   代表转换的常量.或者转换本身.   Lambda 表达式 Lambda 表达式”是一个匿名函数,可以包含表达式和语句 ...

  10. jquery获取自定义属性的值

    //获取属性值 1 <div id="text" value="黑哒哒的盟友"><div> jQuery取值: $("#tex ...