矩阵快速幂(queue递推)
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): 8567    Accepted Submission(s): 3749
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.
4 7
4 8
2
1
#include <cstdio>
#include <cstring>
#include <cmath>
#include <algorithm>
#include <iostream>
#include <algorithm>
#include <iostream>
#include<cstdio>
#include<string>
#include<cstring>
#include <stdio.h>
#include <string.h>
#include <vector>
#define ME(x , y) memset(x , y , sizeof(x))
#define SF(n) scanf("%d" , &n)
#define rep(i , n) for(int i = 0 ; i < n ; i ++)
#define INF 0x3f3f3f3f
//#define mod 1000000009
using namespace std;
typedef long long ll ;
ll n , mod ;
struct node{
ll a[][];
}; node mul(node A , node B)
{
node C ;
memset(C.a , , sizeof(C.a));
for(int i = ; i < ; i++)
{
for(int j = ; j < ; j++)
{
for(int k = ; k < ; k++)
{
C.a[i][j] = (C.a[i][j] + A.a[i][k] * B.a[k][j]) % mod;
}
}
}
return C;
} node pow(node A , ll n)
{
node ans ;
memset(ans.a , ,sizeof(ans.a));
for(int i = ;i < ; i++)
ans.a[i][i] = ;
while(n)
{
if(n & )
{
ans = mul(ans , A);
}
n >>= ;
A = mul(A , A);
}
return ans ;
} int main()
{
while(~scanf("%lld%lld" , &n , &mod))
{
node A , B , C ;
memset(A.a , , sizeof(A.a));
A.a[][] = , A.a[][] = ,A.a[][] = ,A.a[][] = ;
A.a[][] = , A.a[][] = ,A.a[][] = ,A.a[][] = ;
A.a[][] = , A.a[][] = ; B.a[][] = , B.a[][] = , B.a[][] = , B.a[][] = ;
B.a[][] = ; C = mul(pow(A , n-) , B);
printf("%lld\n" , C.a[][]);
} return ;
}
不过这道题还有别的递推式:
递推方程:f(n)=f(n-1)+f(n-3)+f(n-4)。
如果第n位是f,它前面是f时(ff),再前一位必须是m(mff),再前一位还必须是m(mmff),所以有f(n-4)种;
它前面是m时(mf),再前一位必须是m(mmf),再前就任意了,所以有f(n-3)种
第n位是m,它前面可以是任意的,所以有f(n-1)种。
接下来是构造矩阵:

矩阵快速幂(queue递推)的更多相关文章
- HDU 5950 - Recursive sequence - [矩阵快速幂加速递推][2016ACM/ICPC亚洲区沈阳站 Problem C]
		
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5950 Farmer John likes to play mathematics games with ...
 - CH 3401 - 石头游戏 - [矩阵快速幂加速递推]
		
题目链接:传送门 描述石头游戏在一个 $n$ 行 $m$ 列 ($1 \le n,m \le 8$) 的网格上进行,每个格子对应一种操作序列,操作序列至多有 $10$ 种,分别用 $0 \sim 9$ ...
 - HDU 1757  矩阵快速幂加速递推
		
题意: 已知: 当x<10时:f(x)=x 否则:f(x) = a0 * f(x-1) + a1 * f(x-2) + a2 * f(x-3) + --+ a9 * f(x-10); 求:f(x ...
 - HDU5950 Recursive sequence (矩阵快速幂加速递推) (2016ACM/ICPC亚洲赛区沈阳站 Problem C)
		
题目链接:传送门 题目: Recursive sequence Time Limit: / MS (Java/Others) Memory Limit: / K (Java/Others) Total ...
 - AcWing   226. 233矩阵     (矩阵快速幂+线性递推)打卡
		
题目:https://www.acwing.com/problem/content/228/ 题意:有一个二维矩阵,这里只给你第一行和第一列,要你求出f[n][m],关系式有 1, f[0][ ...
 - CH3401 石头游戏(矩阵快速幂加速递推)
		
题目链接:传送门 题目: 石头游戏 0x30「数学知识」例题 描述 石头游戏在一个 n 行 m 列 (≤n,m≤) 的网格上进行,每个格子对应一种操作序列,操作序列至多有10种,分别用0~9这10个数 ...
 - BZOJ4547 Hdu5171 小奇的集合 【矩阵快速幂优化递推】
		
BZOJ4547 Hdu5171 小奇的集合 Description 有一个大小为n的可重集S,小奇每次操作可以加入一个数a+b(a,b均属于S),求k次操作后它可获得的S的和的最大值.(数据保证这个 ...
 - [bzoj1009](HNOI2008)GT考试 (kmp+矩阵快速幂加速递推)
		
Description 阿 申准备报名参加GT考试,准考证号为N位数X1X2....Xn(0<=Xi<=9),他不希望准考证号上出现不吉利的数字.他的不吉利数学 A1A2...Am(0&l ...
 - [bzoj1008](HNOI2008)越狱(矩阵快速幂加速递推)
		
Description 监狱有连续编号为1...N的N个房间,每个房间关押一个犯人,有M种宗教,每个犯人可能信仰其中一种.如果相邻房间的犯人的宗教相同,就可能发生越狱,求有多少种状态可能发生越狱 In ...
 - [HIHO1143]骨牌覆盖问题·一(矩阵快速幂,递推)
		
题目链接:http://hihocoder.com/problemset/problem/1143 这个递推还是很经典的,结果是斐波那契数列.f(i) = f(i-1) + f(i-2).数据范围太大 ...
 
随机推荐
- MongoDB与关系数据库的对比
			
MongoDB与关系数据库的对比
 - HBase HA分布式集群搭建
			
HBase HA分布式集群搭建部署———集群架构 搭建之前建议先学习好HBase基本构架原理:https://www.cnblogs.com/lyywj170403/p/9203012.html 集群 ...
 - javaweb各种框架组合案例(八):springboot+mybatis-plus+restful
			
一.介绍 1. springboot是spring项目的总结+整合 当我们搭smm,ssh,ssjdbc等组合框架时,各种配置不胜其烦,不仅是配置问题,在添加各种依赖时也是让人头疼,关键有些jar包之 ...
 - 【改】DOS文件格式转UNIX文件格式
			
windows中的文本文件的换行符是"\r\n",而linux中是"\n",dos格式文件传输到unix系统时,会在每行的结尾多一个^M,当然也有可能看不到,但 ...
 - F1函数csdn 是否支持版本
			
https://docs.microsoft.com/zh-cn/windows/desktop/api/winsock2/nf-winsock2-wsaioctl
 - alert(1) to win
			
一. function escape(s) { return '<script>console.log("'+s+'");</script>'; } 两种思 ...
 - 对getBoundingClientRect属性的研究
			
1.getBoundingClientRect用于获取某个元素相对于视窗的位置集合.集合中有top, right, bottom, left等属性. .top:元素上边到视窗上边的距离; right: ...
 - 英语单词vendors
			
vendors 来源——https://www.docker.com/products/docker-hub Share and Collaborate with Docker Hub Docker ...
 - MongoDB与阿里云达成战略合作,最新数据库独家上线阿里云!
			
11月26日,开源数据库厂商MongoDB与阿里云在北京达成战略合作,作为合作的第一步,最新版MongoDB 4.2数据库产品正式上线阿里云平台. 目前阿里云成为全球唯一可提供最新版MongoDB服务 ...
 - 持续优化云原生体验,阿里云在Serverless容器与多云上的探索
			
近日,阿里云宣布推出Serverless Kubernetes服务此举意在降低容器技术的使用门槛.简化容器平台运维.并同时发布阿里云服务对Open Service Broker API标准支持,通过一 ...