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两两状态之间的转移是定向的,也 ...
随机推荐
- 部署zookeeper集群
1.把zookeeper.tar.gz解压之后,移动到/usr目录下 2.首先要给zookeeper之间的每个节点的ssh设置无密码登陆 3.在zookeeper目录下编辑zoo.cfg,复制zoo_ ...
- anroid 广播
广播接收者(BroadcastReceiver)用于接收广播Intent,广播Intent的发送是通过调用Context.sendBroadcast().Context.sendOrderedBroa ...
- thinkphp 第一个设计总结
1.thinkphp的无限级分类不是万能的... 2.感觉先看前台(根据前台设计数据库)后写控制代码(后台),速度或许会快一点,思路明确...
- selenium测试(Java)--下载文件(十六)
下载文件需要在Firefox 的profile属性中配置一些参数,如下面的代码: package com.test.download; import java.io.File; import org. ...
- try except 异常处理
1.捕获指定异常 2.捕获所有异常
- QTableView修改数据后弹出是否保存的提示框。
自定义CustomDelegate继承自QStyledItemDelegate,重写setModelData(self, editor, model, index)方法 def setModelDat ...
- 天线增益英文名称:antenna gain
天线增益是指:在输入功率相等的条件下,实际天线与理想的辐射单元在空间同一点处所产生的信号的功率密度之比.它定量地描述一个天线把输入功率集中辐射的程度.增益显然与天线方向图有密切的关系,方向图主瓣越窄, ...
- js for form
//表单填充 表单填充 , formDataLoad: function (domId, obj) { for (var property in obj) { ...
- PHP中替换换行符方法总结
<?php header("content-type:text/html;charset=utf-8"); $str = "aaaa bbbb cccc dddd& ...
- CentOS7忘记root密码的解决方法
开机启动centos 7.0,看到如下画面,选择下图选单,按"e"键 在下图linux16行中,将ro这两个字母修改为rw init=/sysroot/bin/sh 修改结果如下图 ...