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

Problem Description
CRB is now playing Jigsaw Puzzle.
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.)
 
Input
There are multiple test cases. The first line of input contains an integer T, indicating the number of test cases. For each test case:
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

 
Output
For each test case, output a single integer - number of different patterns modulo 2015.
 
Sample Input
1
3 2
1 2
1 3
0
 
Sample Output
6

Hint

possible patterns are ∅, 1, 2, 3, 1→2, 2→3

 
Author
KUT(DPRK)
 
Source
 
Recommend
wange2014

同样的题:不同的写法,却得不出相同的答案。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]的更多相关文章

  1. 矩阵优化dp

    链接:https://www.luogu.org/problemnew/show/P1939 题解: 矩阵优化dp模板题 搞清楚矩阵是怎么乘的构造一下矩阵就很简单了 代码: #include < ...

  2. bzoj 3120 矩阵优化DP

    我的第一道需要程序建矩阵的矩阵优化DP. 题目可以将不同的p分开处理. 对于p==0 || p==1 直接是0或1 对于p>1,就要DP了.这里以p==3为例: 设dp[i][s1][s2][r ...

  3. HDU - 2294: Pendant(矩阵优化DP&前缀和)

    On Saint Valentine's Day, Alex imagined to present a special pendant to his girl friend made by K ki ...

  4. [六省联考2017]组合数问题 (矩阵优化$dp$)

    题目链接 Solution 矩阵优化 \(dp\). 题中给出的式子的意思就是: 求 nk 个物品中选出 mod k 为 r 的个数的物品的方案数. 考虑朴素 \(dp\) ,定义状态 \(f[i][ ...

  5. HDU5411——CRB and Puzzle——————【矩阵快速幂优化dp】

    CRB and Puzzle Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)To ...

  6. [Sdoi2017]序列计数 矩阵优化dp

    题目 https://www.lydsy.com/JudgeOnline/problem.php?id=4818 思路 先考虑没有质数限制 dp是在同余系下的,所以\(f[i][j]\)表示前i个点, ...

  7. bzoj 1009 [HNOI2008]GT考试——kmp+矩阵优化dp

    题目:https://www.lydsy.com/JudgeOnline/problem.php?id=1009 首先想到 确保模式串不出现 就是 确保每个位置的后缀不是该模式串. 为了dp,需要记录 ...

  8. HDOJ 5411 CRB and Puzzle 矩阵高速幂

    直接构造矩阵,最上面一行加一排1.高速幂计算矩阵的m次方,统计第一行的和 CRB and Puzzle Time Limit: 2000/1000 MS (Java/Others)    Memory ...

  9. 矩阵优化DP类问题应用向小结

    前言 本篇强调应用,矩阵的基本知识有所省略(也许会写篇基础向...). 思想及原理 为什么Oier们能够想到用矩阵来加速DP呢?做了一些DP题之后,我们会发现,有时候DP两两状态之间的转移是定向的,也 ...

随机推荐

  1. Missing artifact jdk.tools:jdk.tools:jar:1.8 pom.xml

    在maven项目中出现下面错误信息: Description Resource Path Location Type Missing artifact jdk.tools:jdk.tools:jar: ...

  2. pip使用国内镜像,豆瓣、清华

    pip使用国内镜像,豆瓣.清华 2017年01月18日 22:27:44 阅读数:4416 Python开发的时候需要安装各种模块,而pip是很强大的模块安装工具,但是由于国外官方pypi经常被墙,导 ...

  3. js学习笔记16----父节点的操作

    1.元素.parentNode : 只读属性,获取当前元素的父节点. 2.元素.offsetParent : 只读属性,获取离当前元素最近的一个有定位属性(position为relative或者abs ...

  4. json_decode() expects parameter 1 to be string, object given

    $data = Weann\Socialite\Facades\Socialite::driver('wechat')->user();//是字符串 $data=json_encode($dat ...

  5. R语言学习笔记之<在Linux上遇到的问题集锦>

    Standalone模式:Standalone模式运行的Spark集群对不同的应用程序采用先进先出(FIFO)的顺序进行调度.默认情况下每个应用程序会独占所有可用节点的资源. 现在版本的SparkR只 ...

  6. android Fragment 笔记

    Fragment多用于平板中,Fragment当成Activity的一个界面的一个组成部分,Fragment有自己的生命周期,但是必须依托在Activity中. 参考链接 https://develo ...

  7. PHP实现金额数字转换成大写函数

    <?php header("Content-Type:text/html;charset=utf-8"); function num_to_upper($num) { $d ...

  8. CentOS7怎么修改命令行启动

    root用户下直接执行命令: systemctl set-default multi-user.target 然后reboot即可.

  9. Spring-profile设置

    开发环境和生产环境通常采用不同的数据库连接方式,开发环境可以采用侵入式,而生产环境中采用jndi连接池,所以要根据不同环境配置不同的bean,Spring中提供了profile来实现动态生成相应的be ...

  10. iftop、ifstat详解

    ifstat 介绍 ifstat工具是个网络接口监测工具,比较简单看网络流量 实例 默认使用 #ifstat eth0 eth1 KB/s in KB/s out KB/s in KB/s out 0 ...