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. ProgressBar 的使用

    ProgressBar 的使用方法 转载:http://blog.csdn.net/mad1989/article/details/38042875

  2. JS中string对象的一些方法

    原文地址(包含所有的string对象的方法):  http://www.dreamdu.com/javascript/object_string/ string.slice(startPos,endP ...

  3. MeshLab中进行点云配准

    MeshLab是一个开源.可移植和可扩展的三维几何处理系统,主要用于交互处理和非结构化编辑三维三角形网格.它支持多种文件格式: import:PLY, STL, OFF, OBJ, 3DS, COLL ...

  4. IP地址验证

    /** * 验证IP地址 * * @param 待验证的字符串 * @return 如果是符合格式的字符串,返回 <b>true </b>,否则为 <b>false ...

  5. [SAP ABAP开发技术总结]BAPI调用

    声明:原创作品,转载时请注明文章来自SAP师太技术博客( 博/客/园www.cnblogs.com):www.cnblogs.com/jiangzhengjun,并以超链接形式标明文章原始出处,否则将 ...

  6. 复制选中的listbox内容

    private void lt_log_MouseClick(object sender, MouseEventArgs e) { Clipboard.SetDataObject(lt_log.Sel ...

  7. 面向对象的JavaScript系列二,继承

    1.原型链 function SuperType(){ this.property = true; } SuperType.prototype.getSuperValue = function(){ ...

  8. Java中ArrayList相关的5道面试题

    本文参考了 <关于ArrayList的5道面试题 > 1.ArrayList的大小是如何自动增加的? 这个问题我想曾经debug过并且查看过arraylist源码的人都有印象,它的过程是: ...

  9. iOS - Plist 数据解析

    前言 NS_AVAILABLE(10_6, 4_0) @interface NSPropertyListSerialization : NSObject 如果对象是 NSArray 或 NSDicti ...

  10. nodejs学习笔记<三>关于路由(url)

    在网站开发中,路由的设置非常关键.nodejs对路由处理封装了一个比较全面的模块. 来认识下url模块 1)在命令行(cmd)可以直接 node —> url 可直接查看url模块的所有方法. ...