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 2
L 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和m,长度为l的字符串,让你找出满题条件的串的个数对m取余的数。如果这个串中不含fff和fmf就说这个串是满足条件的。
假设现有长度为n的串s,f(n)是s串满足条件的答案。f(n)怎么推出来捏?
如果s的最后一位为m,那么就加上f(n-1),因为在长度为n-1的满足条件的串后面加上一个m所得到的串肯定也满足条件。
如果s的最后一位为f,那么s的最后三位只有mmf和mff两种情况!继续讨论!
如果s以mmf结尾,那么加上f(n-3),因为在长度为n-3的满足条件的串后面加上一个mmf所得到的串肯定也满足条件。
那如果s以mff结尾呢?向前思考一位,倒数第四位只能是m,也就是此时s是以mmff结尾的,再加上f(n-4)岂不是美滋滋?
综上f(n)=f(n-1)+f(n-3)+f(n-4)。无耻的再盗张图...OTZ...

代码如下:
 #include <bits/stdc++.h>

 using namespace std;
const int N=;
int l,m;
struct Matrix
{
long long int mat[N][N];
}matrix;
void init()
{
memset(matrix.mat,,sizeof matrix.mat);
matrix.mat[][]=;
matrix.mat[][]=;
matrix.mat[][]=;
matrix.mat[][]=;
for (int i=;i<N;++i)
{
for (int j=;j<N;++j)
{
if (i==j+)
matrix.mat[i][j]=;
}
}
}
Matrix operator * (Matrix a,Matrix b)
{
Matrix c;
for (int i=;i<N;++i)
{
for (int j=;j<N;++j)
{
c.mat[i][j]=;
for (int k=;k<N;++k)
c.mat[i][j]+=a.mat[i][k]*b.mat[k][j];
c.mat[i][j]%=m;
}
}
return c;
}
Matrix Pow (int n)
{
Matrix t;
if (n==)
return matrix;
if (n&)
return matrix*Pow(n-);
else
{
Matrix temp=Pow(n>>);
return temp*temp;
}
}
int main()
{
//freopen("de.txt","r",stdin);
long long int f[];
f[]=;
f[]=;
f[]=;
f[]=;
f[]=;
while (~scanf("%d%d",&l,&m))
{
init();
if (l<=)
{
printf("%lld\n",f[l]%m);
continue;
}
Matrix temp=Pow(l-);
long long int ans=;
for (int i=;i<N;++i)
{
ans += temp.mat[][i]*f[N-i];
ans%=m;
}
printf("%lld\n",ans);
}
return ;
}


hdu 2604 Queuing(推推推公式+矩阵快速幂)的更多相关文章

  1. CCF 201312-4 有趣的数 (数位DP, 状压DP, 组合数学+暴力枚举, 推公式, 矩阵快速幂)

    问题描述 我们把一个数称为有趣的,当且仅当: 1. 它的数字只包含0, 1, 2, 3,且这四个数字都出现过至少一次. 2. 所有的0都出现在所有的1之前,而所有的2都出现在所有的3之前. 3. 最高 ...

  2. HDU6050: Funny Function(推公式+矩阵快速幂)

    传送门 题意 利用给出的式子求\(F_{m,1}\) 分析 直接推公式(都是找规律大佬) \(n为偶数,F_{m,1}=\frac{2(2^n-1)^{m-1}}3\) \(n为奇数,F_{m,1}= ...

  3. HDU 2855 斐波那契+矩阵快速幂

    http://acm.hdu.edu.cn/showproblem.php?pid=2855 化简这个公式,多写出几组就会发现规律 d[n]=F[2*n] 后面的任务就是矩阵快速幂拍一个斐波那契模板出 ...

  4. HDU 5950:Recursive sequence(矩阵快速幂)

    http://acm.hdu.edu.cn/showproblem.php?pid=5950 题意:给出 a,b,n,递推出 f(n) = f(n-1) + f(n-2) * 2 + n ^ 4. f ...

  5. HDU 3292 【佩尔方程求解 && 矩阵快速幂】

    任意门:http://acm.hdu.edu.cn/showproblem.php?pid=3292 No more tricks, Mr Nanguo Time Limit: 3000/1000 M ...

  6. 【66测试20161115】【树】【DP_LIS】【SPFA】【同余最短路】【递推】【矩阵快速幂】

    还有3天,今天考试又崩了.状态还没有调整过来... 第一题:小L的二叉树 勤奋又善于思考的小L接触了信息学竞赛,开始的学习十分顺利.但是,小L对数据结构的掌握实在十分渣渣.所以,小L当时卡在了二叉树. ...

  7. HDU 2256 Problem of Precision 数论矩阵快速幂

    题目要求求出(√2+√3)2n的整数部分再mod 1024. (√2+√3)2n=(5+2√6)n 如果直接计算,用double存值,当n很大的时候,精度损失会变大,无法得到想要的结果. 我们发现(5 ...

  8. HDU 2256 Problem of Precision (矩阵快速幂)(推算)

    Problem of Precision Time Limit: 1000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Oth ...

  9. hdu3483 A Very Simple Problem 非线性递推方程2 矩阵快速幂

    题目传送门 题目描述:给出n,x,mod.求s[n]. s[n]=s[n-1]+(x^n)*(n^x)%mod; 思路:这道题是hdu5950的进阶版.大家可以看这篇博客hdu5950题解. 由于n很 ...

随机推荐

  1. GrapeCity Documents for Excel 与 Apache POI 功能对比

    GrapeCity Documents for Excel 与 Apache POI 功能对比 GrapeCity Documents for Excel 是什么? GrapeCity Documen ...

  2. job创建之后,不运行

    创建job的时候要commit,然后还要看job是否分派进程 加上之后还是没有执行,后来发现 show parameter job_queue_process; 的结果为0,没有为job分配进程,所以 ...

  3. 关于springcloud hystrix 执行 hystrix.stream 跳转失败的问题

    经过观看网友的总结:应该时版本的问题.某些版本没有对/hystrix.stream进行配置 所以解决方案(网友答案): 需要配置类配置下面 @Bean public ServletRegistrati ...

  4. JavaScript使用readAsDataURL读取图像文件

    JavaScript使用readAsDataURL读取图像文件 FileReader对象的readAsDataURL方法可以将读取到的文件编码成Data URL.Data URL是一项特殊的技术,可以 ...

  5. vscode 插件 配置

    第一页 第二页 第三页 settings.json配置 { "editor.fontSize": 20, "files.autoSave": "off ...

  6. golang中格式化符号说明

    %v 值的默认格式表示 %+v 类似%v,但输出结构体时会添加字段名 %#v 值的Go语法表示 %T 值的类型的Go语法表示 %% 百分号 布尔值: %t 单词true或false 整数: %b 表示 ...

  7. 管理.MD

    ```` 对于水平低点的我一般是:讲解任务 -> 他复述任务 ->提出解决思路 -> 他复述思路 -> 认他思考一段时间,他提出他的意见和想法 -> 我再确定 -> ...

  8. 03 python3常见内置函数

    数学相关 abs(a) : 求取绝对值.abs(-1) max(list) : 求取list最大值.max([1,2,3]) min(list) : 求取list最小值.min([1,2,3]) su ...

  9. String,到底创建了多少个对象?

      String str=new String("aaa"); <span style="font-size:14px;">String str=n ...

  10. 点击切换的JS

    $(function(){ var tabnav = $("#tab-nav ul li"); tabnav.click(function(){ $(this).addClass( ...