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. ios假设写一个提示带动画的View,能够来引导用户行为

    先上图: 这个UIView能够这样写: -(id)initWithFrame:(CGRect)frame backImage:(UIImage*)image msgStr:(NSString*)txt ...

  2. Spring Batch(4): Job详解

    Spring Batch(4): Job详解 2016-03-26 18:46 870人阅读 评论(1) 收藏 举报  分类: Spring(6)  版权声明:本文为博主原创文章,未经博主允许不得转载 ...

  3. iOS开发之autoLayout constraint

    前言 ios设备的尺寸越来越多,针对一款app可能要适配到多种设备.多种尺寸.所以.我们期望我们的app可以autoLayout.本文主要介绍在Xcode中使用constraint.未来会不定期对此文 ...

  4. h5-爆料view

    aaarticlea/png;base64,iVBORw0KGgoAAAANSUhEUgAAAdsAAABeCAIAAADkEim8AAAWAElEQVR4nO2d+1Nb55nHPbMz+1v+g/ ...

  5. ThinkPHP5+Redis单例型购物车

    <?php /** * Redis + 单例型购物车 * param $basket 存储商品信息 * param $ins 存储实例化对象 */ namespace lib; use redi ...

  6. 27.Qt时钟

    myclock.h #ifndef MYCLOCK_H #define MYCLOCK_H #include <QObject> #include <QLCDNumber> # ...

  7. block的一些注意事项

    1,定义block时是可以同时进行赋值的 2,block中是代码块,就是里面写的是语句,需要加分号 3,在block中,允许有多条语句 4,在带有参数的block中,声明部分参数名可以省略,但是建议写

  8. surface 更新提示

  9. autocomplete for password

    <input id="userPassword" type="password" autocomplete="current-password& ...

  10. word-break属性和css换行显示

    这几天在做项目的时候,遇到了比较棘手的问题,便是在一个标签里边展示内容,如果说展示中文汉字,一点问题都没有,但是只要连续展示英文字母或者中文的标点符号(中间不带空格),那么所渲染的内容就不会换行,而是 ...