题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2604

Queuing

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

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
 
Source
 
 
注释:在思考一个递推公式的时候几乎都可以分类讨论一下然后写成一个矩阵乘的形式,然后递推得到结果。
 
题意:

用f(n)表示n个人满足条件的结果,那么如果最后一个人是m的话,那么前n-1个满足条件即可,就是f(n-1); 
如果最后一个是f那么这个还无法推出结果,那么往前再考虑一位:那么后三位可能是:mmf, fmf, mff, fff,其中fff和fmf不满足题意所以我们不考虑,但是如果是 
mmf的话那么前n-3可以找满足条件的即:f(n-3);如果是mff的话,再往前考虑一位的话只有mmff满足条件即:f(n-4) 
所以f(n)=f(n-1)+f(n-3)+f(n-4),递推会跪,可用矩阵快速幂 
构造一个矩阵。

用f(n)表示n个人满足条件的结果,那么如果最后一个人是m的话,那么前n-1个满足条件即可,就是f(n-1); 
如果最后一个是f那么这个还无法推出结果,那么往前再考虑一位:那么后三位可能是:mmf, fmf, mff, fff,其中fff和fmf不满足题意所以我们不考虑,但是如果是 
mmf的话那么前n-3可以找满足条件的即:f(n-3);如果是mff的话,再往前考虑一位的话只有mmff满足条件即:f(n-4) 
所以f(n)=f(n-1)+f(n-3)+f(n-4),递推会跪,可用矩阵快速幂 
构造一个矩阵: 

 //快速幂矩阵
#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
struct Mat
{
int mat[][];
};
int m;
Mat operator *(Mat a, Mat b)
{
Mat c;
memset(c.mat,,sizeof(c.mat));
int i, j, k;
//这儿的顺序按照
for(i = ; i < ; i++)
for(j = ; j < ; j++)
for(k = ; k < ; k++)
c.mat[i][j] = (c.mat[i][j] + a.mat[i][k]*b.mat[k][j])%m;
return c;
}
Mat multi(int n)//计算一个已知矩阵的n次方%m
{
Mat ans;
for(int i = ; i < ; i++)
{
for(int j = ; j < ; j++)
{
if(i==j)
ans.mat[i][j] = ;
else ans.mat[i][j] = ;
} }
Mat a;
memset(a.mat,,sizeof(a.mat));
a.mat[][] = a.mat[][] = a.mat[][] = a.mat[][] = a.mat[][] = a.mat[][] = ; while(n>)
{
if(n&) ans = ans*a;
a = a*a;
n>>=;
}
return ans;
}
int main()
{
int n;
while(~scanf("%d%d",&n,&m))
{
if(n==) printf("%d\n",%m);
else if(n==) printf("%d\n",%m);
else if(n==) printf("%d\n",%m);
else if(n==) printf("%d\n",%m);
else
{
Mat t;
t = multi(n-);
int sol = ((t.mat[][]*)%m+(t.mat[][]*)%m+(t.mat[][]*)%m+(t.mat[][]*)%m)%m;
printf("%d\n",sol);
}
}
return ;
}

最新文章

  1. Sql server函数的学习1(系统变量、错误函数、转换函数)
  2. 编译安装或者mysql启动时遇到的错误小记
  3. .NET MVC AjaxHelper
  4. HDU1853 Cyclic Tour(最小费用最大流)
  5. exec命令
  6. Effective C++学习笔记 条款04:确定对象被使用前已先被初始化
  7. 记录一下跟Python有关的几个拓展名
  8. NET工厂模式架构
  9. Linux入门(一)常见虚拟机及Linux系统安装、xshell连接虚拟机
  10. IOS开发-UI学习-delegate(代理)的使用,键盘消失
  11. Visual Studio2017数据库架构比较
  12. composer安装laravel指定版本
  13. goland 文件头自动注释
  14. [数据结构] 希尔排序 C语言程序
  15. 关于try catch finally 三者之间的关系(JDK 1.8)
  16. Python_关于多线程下变量赋值取值的一点研究
  17. [转帖]dd命令详解
  18. POJ1611:The Suspects(模板题)
  19. 【bzoj2555】 SubString
  20. Java中final修饰符深入研究

热门文章

  1. 创建、设置和安装Windows服务
  2. 【Hdu2089】不要62(数位DP)
  3. wincc flexable变量组态
  4. vmstat 命令详解
  5. HtmlTestRunner无法生成HTML报告问题
  6. 【http转https】其之三 IIS_URL重写_http重定向到https
  7. python自动安装mysql5.7
  8. 关于 const 的一点小研究
  9. thinkphp-&gt;add方法错误
  10. 【Python3之模块及包的导入】