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. P4942小凯的数字

    给定一个序列,如12345 56789 1011121314等,输出对其取余9的结果. 那么我们需要明白一个定理,一个序列对一个数的取余结果等于它各位之和取余那个数的结果.证明似乎是这样∑i=0n​a ...

  2. 解决mac下brew install报错

    Error: Another active Homebrew update process is already in progress.Please wait for it to finish or ...

  3. [转]Opcode是啥以及如何使用好Opcache

    转载链接:Opcode是啥以及如何使用好Opcache 啥是Opcode? 我们在日常的PHP开发过程中,应该经常会听见Opcache这个词,那么啥是Opcode呢? Opcache 的前生是 Opt ...

  4. Spring Cloud,Docker

    Spring Cloud 先决条件 Spring cloud 基于spring boot,spring,java Spring Cloud解决的问题 分布式微服务架构和微服务监控.注册于发现.跟踪等一 ...

  5. C# 过滤字典中的数据 并将过滤后的数据转成新的字典对象

    Dictionary<string, object> dic = new Dictionary<string, object>(); dic.Add("); dic. ...

  6. pat (B)_1002

    1002 写出这个数 (20 分) 读入一个正整数 n,计算其各位数字之和,用汉语拼音写出和的每一位数字. 输入格式: 每个测试输入包含 1 个测试用例,即给出自然数 n 的值.这里保证 n 小于 1 ...

  7. openstack mitaka开启三层网络vxlan

    在这之前,先把之前基于flat模式创建的虚机,全部删除 控制节点: 配置 修改/etc/neutron/neutron.conf的[DEFAULT]区域 将 core_plugin = ml2 ser ...

  8. zabbix 启到不起来:active check configuration update from [127.0.0.1:10051] started to fail (cannot connect to [[127.0.0.1]:10051]: [111] Connection refused)

    cat /var/log/zabbix_agent_log 查看日记出现报错:active check configuration update from [127.0.0.1:10051] star ...

  9. parfile解决exp时tables过多问题

    parfile 一般用于表数据过大.使用导出.导入命令参数过多等场景: 在对oracle数据库使用exp命令导出数据时,如果tables=后面跟的表比较多,就是导致命令行放不下,从而不能导出.百度一把 ...

  10. Daily Affirmations 每天对自己大声说:我很棒

    I was 18 the first time a therapist2) tried to get me to embrace the idea of daily affirmations. I w ...