Queuing

Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 2796    Accepted Submission(s): 1282

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
 
Author
WhereIsHeroFrom
 
 
   首先我们不考虑去模的问题:
      l = 0                            0 种
      l = 1      e的数目有 f,m: 2 种
      l = 2        ...........ff,mm,fm,mf    4种
      l = 3                                         6
      l = 4                                        9
      l =  5                                       15
      l  =  6                                      25
      f5=f4+f3+f1;
      f6=f5+f4+f2;
  ------->  fn={   fn-1+fn-3+fn-4  n>4;
由齐次方程构造矩阵.....
|fn   |    |1,0,1,1|  |fn-1|
|fn-1|    |1,0,0,0|  |fn-2|
|fn-2| = |0,1,0,0|*|fn-3|
|fn-3|    |0,0,1,0|   |fn-4|
代码:
 //#define LOCAL
#include<cstdio>
#include<cstring>
using namespace std;
//matrix --> ¾ØÕó
int mat[][];
int ans[][];
int len,m; void init()
{
int cc[][]={
{,,,},{,,,},
{,,,},{,,,}}; for(int i=;i<;i++)
{
for(int j=;j<;j++)
{
mat[i][j]=cc[i][j];
if(i==j) ans[i][j]=;
else ans[i][j]=;
}
}
}
void Matrix(int a[][],int b[][]) //¾ØÕóÏà³Ë
{
int i,j,k;
int c[][]={};
for(j=;j<;j++){
for(i=;i<;i++){
for(k=;k<;k++){
c[j][i]=(c[j][i]+a[j][k]*b[k][i])%m;
}
}
} for(j=;j<;j++)
for(i=;i<;i++)
a[j][i]=c[j][i]; } void pow(int n)
{
while(n>)
{
if(n&) Matrix(ans,mat);
n>>=;
if(n==) break;
Matrix(mat,mat);
}
}
int main()
{
#ifdef LOCAL
freopen("test.in","r",stdin);
#endif
int f[]={,,,};
while(scanf("%d%d",&len,&m)!=EOF)
{
if(len==)printf("%d\n",);
else if(len<=)printf("%d\n",f[len-]%m);
else{
init();
pow(len-);
printf("%d\n",(ans[][]*f[]+ans[][]*f[]+ans[][]*f[]+ans[][]*f[])%m);
}
}
return ;
}

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

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

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

  2. HDU 2604 Queuing 矩阵高速幂

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

  3. HDU 5667 构造矩阵快速幂

    HDU 5667 构造矩阵快速幂 题目描述 解析 我们根据递推公式 设 则可得到Q的指数关系式 求Q构造矩阵 同时有公式 其中φ为欧拉函数,且当p为质数时有 代码 #include <cstdi ...

  4. HDU 6185 Covering 矩阵快速幂

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=6185 题意:用 1 * 2 的小长方形完全覆盖 4 * n的矩形有多少方案. 解法:小范围是一个经典题 ...

  5. HDU 2157(矩阵快速幂)题解

    How many ways?? Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) ...

  6. HDU 6395 分段矩阵快速幂 HDU 6386 建虚点+dij

    http://acm.hdu.edu.cn/showproblem.php?pid=6395 Sequence Time Limit: 4000/2000 MS (Java/Others)    Me ...

  7. HDU 6470 【矩阵快速幂】

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=6470 写这道题是为了让自己不要忘记矩阵快速幂如何推出矩阵式子的. 注意 代码是TLE的!! #incl ...

  8. HDU 5607 graph 矩阵快速幂 + 快速幂

    这道题得到了学长的助攻,其实就是一个马尔科夫链,算出一步转移矩阵进行矩阵快速幂就行了,无奈手残 这是我第一回写矩阵快速幂,写的各种毛病,等到调完了已经8点44了,交了一发,返回PE,(发现是少了换行) ...

  9. HDU 1575(裸矩阵快速幂)

    emmmmm..就是矩阵快速幂,直接附代码: #include <cstdio> using namespace std; ; ; struct Matrix { int m[maxn][ ...

  10. hdu 6395Sequence【矩阵快速幂】【分块】

    Sequence Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 262144/262144 K (Java/Others) Total ...

随机推荐

  1. java读取文件多种方法

    1.按字节读取文件内容2.按字符读取文件内容3.按行读取文件内容 4.随机读取文件内容 public class ReadFromFile {     /**      * 以字节为单位读取文件,常用 ...

  2. sql server日期时间函数

    From:http://www.cnblogs.com/linzheng/archive/2010/11/17/1880208.html 1.  当前系统日期.时间  select getdate() ...

  3. hdu2037-----------贪心, 活动安排问题

    http://acm.hdu.edu.cn/showproblem.php?pid=2037(简单贪心-----活动安排) #include<iostream> #include<a ...

  4. 一个CSS中Z-index的用法

    一个CSS中Z-index的用法 CSS教程:彻底掌握Z-index属性     大多数的CSS属性都很容易使用.常常,当您对标记语言的元素使用CSS属性时,产生的结果会随着您刷新页面而立即呈现.而另 ...

  5. cocos2d-x 3.X (二)创建动起来的精灵

    [参考文章]http://www.cnblogs.com/suguoqiang/archive/2013/04/03/2997316.html 在HelloWorldScene.h中声明void ro ...

  6. iOS - Swift available 平台判断

    前言 Swift 语言中的 @available 和 #available,Swift 2.0 中,引入了可用性的概念.对于函数,类,协议等,可以使用 @available 声明这些类型的生命周期依赖 ...

  7. iOS - UIColor

    前言 NS_CLASS_AVAILABLE_IOS(2_0) @interface UIColor : NSObject <NSSecureCoding, NSCopying> @avai ...

  8. poj2354Titanic(两点的球面距离)

    链接 球面距离计算公式:d(x1,y1,x2,y2)=r*arccos(sin(x1)*sin(x2)+cos(x1)*cos(x2)*cos(y1-y2)) x1,y1是纬度\经度的弧度单位,r为地 ...

  9. ScriptX.cab打印控件的使用,控件文件里有

    1.在head里添加 <object id="factory" style="display:none;" viewastext classid=&quo ...

  10. Android notifications通知栏的使用

    app发送通知消息到通知栏中的关键代码和点击事件: package com.example.notifications; import android.os.Bundle; import androi ...