CRB and Puzzle

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)
Total Submission(s): 301    Accepted Submission(s): 127

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 \leq T \leq 20$
$1 \leq N \leq 50$
$1 \leq M \leq 10^5$
$0 \leq k \leq N$
$1 \leq a1 < a2 < … < ak \leq 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)

解题:矩阵快速幂+动态规划

$$\begin{bmatrix} ret & dp1 & dp2 & dp3 \\ \end{bmatrix} \times \begin{bmatrix} 1 & 0 & 0 & 0 \\ 1 & 0 & 0 & 0 \\ 1 & 1 & 0 & 0 \\ 1 & 0 & 1 & 0 \\ \end{bmatrix} $$

 #include <bits/stdc++.h>
using namespace std;
const int maxn = ;
const int mod = ;
int n,m;
struct Matrix {
int m[maxn][maxn];
void init() {
memset(m,,sizeof m);
}
void setOne() {
init();
for(int i = ; i < maxn; ++i) m[i][i] = ;
}
Matrix() {
init();
}
Matrix operator*(const Matrix &t)const {
Matrix ret;
for(int k = ; k <= n; ++k) {
for(int i = ; i <= n; ++i)
for(int j = ; j <= n; ++j)
ret.m[i][j] = (ret.m[i][j] + m[i][k]*t.m[k][j])%mod;
}
return ret;
}
};
Matrix a,b,c;
int main() {
int kase;
scanf("%d",&kase);
while(kase--) {
scanf("%d%d",&n,&m);
b.init();
a.init();
c.setOne();
for(int i = ,t,k; i <= n; ++i) {
scanf("%d",&t);
while(t--) {
scanf("%d",&k);
b.m[k][i] = ;
}
}
for(int i = ; i <= n; ++i) {
a.m[][i] = ;
b.m[i][] = ;
}
a.m[][] = ;
while(m) {
if(m&) c = c*b;
m >>= ;
b = b*b;
}
a = a*c;
printf("%d\n",(a.m[][]+)%mod);
}
return ;
}

2015 Multi-University Training Contest 10 hdu 5411 CRB and Puzzle的更多相关文章

  1. 2015 Multi-University Training Contest 10 hdu 5406 CRB and Apple

    CRB and Apple Time Limit: 12000/6000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)To ...

  2. 2015 Multi-University Training Contest 10 hdu 5412 CRB and Queries

    CRB and Queries Time Limit: 12000/6000 MS (Java/Others)    Memory Limit: 131072/131072 K (Java/Other ...

  3. 2015 Multi-University Training Contest 10 hdu 5407 CRB and Candies

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

  4. HDU 5411 CRB and puzzle (Dp + 矩阵高速幂)

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

  5. HDU 5411 CRB and Puzzle (2015年多校比赛第10场)

    1.题目描写叙述:pid=5411">点击打开链接 2.解题思路:本题实际是是已知一张无向图.问长度小于等于m的路径一共同拥有多少条. 能够通过建立转移矩阵利用矩阵高速幂解决.当中,转 ...

  6. 2016 Multi-University Training Contest 10 || hdu 5860 Death Sequence(递推+单线约瑟夫问题)

    题目链接:http://acm.split.hdu.edu.cn/showproblem.php?pid=5860 题目大意:给你n个人排成一列编号,每次杀第一个人第i×k+1个人一直杀到没的杀.然后 ...

  7. 2016 Multi-University Training Contest 10 [HDU 5861] Road (线段树:区间覆盖+单点最大小)

    HDU 5861 题意 在n个村庄之间存在n-1段路,令某段路开放一天需要交纳wi的费用,但是每段路只能开放一次,一旦关闭将不再开放.现在给你接下来m天内的计划,在第i天,需要对村庄ai到村庄bi的道 ...

  8. hdu 5411 CRB and Puzzle (矩阵高速幂优化dp)

    题目:http://acm.hdu.edu.cn/showproblem.php?pid=5411 题意:按题目转化的意思是,给定N和M,再给出一些边(u,v)表示u和v是连通的,问走0,1,2... ...

  9. hdu 5411 CRB and Puzzle 矩阵高速幂

    链接 题解链接:http://www.cygmasot.com/index.php/2015/08/20/hdu_5411/ 给定n个点 常数m 以下n行第i行第一个数字表示i点的出边数.后面给出这些 ...

随机推荐

  1. Linux USB 驱动开发(一)—— USB设备基础概念【转】

    本文转载自:http://blog.csdn.net/zqixiao_09/article/details/50984074 在终端用户看来,USB设备为主机提供了多种多样的附加功能,如文件传输,声音 ...

  2. hdoj--1045--Fire Net(二分图)

    Fire Net Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total S ...

  3. [AHOI 2008] 聚会

    [题目链接] https://www.lydsy.com/JudgeOnline/problem.php?id=1832 [算法] 最近公共祖先 [代码] #include<bits/stdc+ ...

  4. Java-JDK:JDK清单

    ylbtech-Java-JDK:JDK清单 1.返回顶部   2.返回顶部   3.返回顶部   4.返回顶部   5.返回顶部     6.返回顶部   作者:ylbtech出处:http://y ...

  5. python 下串口数据的读取,解析,和保存-

    #!/usr/bin/python # -*-coding: utf-8 -*- import serial import threading import binascii from datetim ...

  6. 学习笔记:javascript中的Generator函数

    最近在学习redux-saga,由于redux-saga需要使用Generator函数,所以下来就回顾了一下Generator Generator 函数是 ES6 提供的一种异步编程解决方案,语法行为 ...

  7. Scrapy中的UA池,代理池,以及selenium的应用

    UA池 代理池 selenium在Scrapy中的应用 UA池 - 下载中间件: - 下载中间件(Downloader Middlewares) 位于scrapy引擎和下载器之间的一层组件. - 作用 ...

  8. (转载)Activity-数据状态的保存

    由于手机是便捷式移动设备,掌握在用户的手中,它的展示方向我们是无法预知的,具有不确定性.平时我们拿着手机多数为竖屏,但有时候我们感觉累了也会躺着去使用手机,那么这时手机屏幕的展示方向可能已经被用户切换 ...

  9. 3) 十分钟学会android--建立第一个APP,建立简单的用户界面

    在本小节里,我们将学习如何用 XML 创建一个带有文本输入框和按钮的界面.下一节课将学会使 APP 对按钮做出响应——按钮被按下时,文本框里的内容被发送到另外一个 Activity. Android ...

  10. mysql数据库索引原理及其常用引擎对比

    索引原理 树数据结构及其算法简介 B+/-树: - 多路搜索树; - 时间复杂度O(logdN);h为节点出度,d为深度 红黑树: - 节点带有颜色的平衡二叉树 - 时间复杂度O(log2N);h节点 ...