hdu5411 CRB and Puzzle[矩阵优化dp]
CRB and Puzzle
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)
Total Submission(s): 1177 Accepted Submission(s): 468
There are N kinds of pieces with infinite supply.
He can assemble one piece to the right side of the previously assembled one.
For each kind of pieces, only restricted kinds can be assembled with.
How many different patterns he can assemble with at most M pieces? (Two patterns P and Q are considered different if their lengths are different or there exists an integer j such that j-th piece of P is different from corresponding piece of Q.)
The first line contains two integers N, M denoting the number of kinds of pieces and the maximum number of moves.
Then N lines follow. i-th line is described as following format.
k a_{1}\ a_{2}\ ...\ a_{k}
Here k is the number of kinds which can be assembled to the right of the i-th kind. Next k integers represent each of them.
1 ≤ T ≤ 20
1 ≤ N ≤ 50
1 ≤ M ≤ 10^5
0 ≤ k ≤ N
1 ≤ a_{1} < a_{2} < … < a_{k} ≤ N
3 2
1 2
1 3
0
possible patterns are ∅, 1, 2, 3, 1→2, 2→3

同样的题:不同的写法,却得不出相同的答案。poj3233 Matrix Power Series
望大佬给蒟蒻答疑
Select Code
#include<cstdio>
#include<cstring>
using namespace std;
const int mod=2015;
struct matrix{
int s[102][102];
matrix(){
memset(s,0,sizeof s);
}
}A,F;int n,m,T;int ans;
matrix operator *(const matrix &a,const matrix &b){
matrix c;
for(int i=0;i<n;i++){
for(int j=0;j<n;j++){
for(int k=0;k<n;k++){
c.s[i][j]+=a.s[i][k]*b.s[k][j];
}
c.s[i][j]%=mod;
}
}
return c;
}
matrix fpow(matrix a,int p){
matrix res;
for(int i=0;i<n;i++) res.s[i][i]=1;
for(;p;p>>=1,a=a*a) if(p&1) res=res*a;
return res;
}
int main(){
for(scanf("%d",&T);T--;){
scanf("%d%d",&n,&m);
matrix A;
for(int i=0,k,x;i<n;i++){
scanf("%d",&k);
while(k--){
scanf("%d",&x);x--;
A.s[i][x]=1;
}
}
for(int i=0;i<n;i++) A.s[i][i+n]=A.s[i+n][i+n]=1;
n<<=1;
A=fpow(A,m);
n>>=1;
ans=1;
for(int i=0;i<n;i++){
for(int j=n;j<2*n;j++){
ans+=A.s[i][j];
}
}
ans%=mod;
printf("%d\n",ans);
}
return 0;
}
hdu5411 CRB and Puzzle[矩阵优化dp]的更多相关文章
- 矩阵优化dp
链接:https://www.luogu.org/problemnew/show/P1939 题解: 矩阵优化dp模板题 搞清楚矩阵是怎么乘的构造一下矩阵就很简单了 代码: #include < ...
- bzoj 3120 矩阵优化DP
我的第一道需要程序建矩阵的矩阵优化DP. 题目可以将不同的p分开处理. 对于p==0 || p==1 直接是0或1 对于p>1,就要DP了.这里以p==3为例: 设dp[i][s1][s2][r ...
- HDU - 2294: Pendant(矩阵优化DP&前缀和)
On Saint Valentine's Day, Alex imagined to present a special pendant to his girl friend made by K ki ...
- [六省联考2017]组合数问题 (矩阵优化$dp$)
题目链接 Solution 矩阵优化 \(dp\). 题中给出的式子的意思就是: 求 nk 个物品中选出 mod k 为 r 的个数的物品的方案数. 考虑朴素 \(dp\) ,定义状态 \(f[i][ ...
- HDU5411——CRB and Puzzle——————【矩阵快速幂优化dp】
CRB and Puzzle Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)To ...
- [Sdoi2017]序列计数 矩阵优化dp
题目 https://www.lydsy.com/JudgeOnline/problem.php?id=4818 思路 先考虑没有质数限制 dp是在同余系下的,所以\(f[i][j]\)表示前i个点, ...
- bzoj 1009 [HNOI2008]GT考试——kmp+矩阵优化dp
题目:https://www.lydsy.com/JudgeOnline/problem.php?id=1009 首先想到 确保模式串不出现 就是 确保每个位置的后缀不是该模式串. 为了dp,需要记录 ...
- HDOJ 5411 CRB and Puzzle 矩阵高速幂
直接构造矩阵,最上面一行加一排1.高速幂计算矩阵的m次方,统计第一行的和 CRB and Puzzle Time Limit: 2000/1000 MS (Java/Others) Memory ...
- 矩阵优化DP类问题应用向小结
前言 本篇强调应用,矩阵的基本知识有所省略(也许会写篇基础向...). 思想及原理 为什么Oier们能够想到用矩阵来加速DP呢?做了一些DP题之后,我们会发现,有时候DP两两状态之间的转移是定向的,也 ...
随机推荐
- 在网页中使用SVG
SVG可以作为一个独立的文件存在.但更多的时候,我们希望他能集成在某个更大的文档中,比如HTML.将SVG插入到HTML中主要有以下几种方式: 将 SVG 作为图像导入 将 SVG放入 iframe ...
- 12款优秀jQuery Ajax分页插件和教程
在这篇文章中,我为大家收集了12个基于 jQuery 框架的 Ajax 分页插件,这些插件都提供了详细的使用教程和演示.Ajax 技术的出现使得 Web 项目的用户体验有了极大的提高,如今借助优秀的 ...
- 关于Cocos2d-x很多奇怪的报错
1.说什么找不到类和命名空间,但是已经包含头文件 项目-属性-配置属性-C/C++-附加包含目录-编辑-添加新行-写上$(EngineRoot) 2.很多语句报错,但是都没问题 我是这样理解的,书上的 ...
- Semi-Supervised Classification with Graph Convolutional Networks
Kipf, Thomas N., and Max Welling. "Semi-supervised classification with graph convolutional netw ...
- Spring Tools Suite (STS) 简介
首先,sts是一个定制版的Eclipse,专为Spring开发定制的,方便创建调试运行维护Spring应用. 官方页面.下载地址(3.8.1 win x64). 其次,没什么好介绍的,用一下就明白了. ...
- hdu 5044 树区间操作最后输出/ lca+dfs
题意:一棵树,俩种操作:1 有路径上的全部点加vi,2全部边加vi. 先离线求出全部询问的lca,再遍历询问一次,点+vi,lca-2*vi ,最后dfs从叶子扫上来一次,最后再祖先点补上就可以.用了 ...
- par函数的bg参数-控制图片的背景色
bg 参数用于控制图片的背景色,默认为白色 代码示例: par(bg = "pink") plot(1:5, 1:5, main = "title", xlab ...
- Linux 域名服务器配置
cat /etc/redhat-release CentOS Linux release 7.0.1406 (Core) 使用BIND构建DNS服务器 1.BIND服务器安装 yum install ...
- CUGBACM Codeforces Tranning 1 题解
链接:http://acm.hust.edu.cn/vjudge/contest/view.action?cid=61581#overview 描写叙述:非常老的CF题,题不错,拿来训练正好. 做的时 ...
- mybatis由浅入深day01_5.3 Mapper动态代理方法
5.3 Mapper动态代理方法(程序员只需要写mapper接口(相当于dao接口)) 5.3.1 实现原理(mapper代理开发规范) 程序员还需要编写mapper.xml映射文件 程序员编写map ...