CRB and Puzzle

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

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 a1 a2 ... ak
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 ≤ 105
0 ≤ k ≤ N
1 ≤ a1 < a2 < … < ak ≤ 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

 
 
题目大意:给你t组测试数据,每组给n和m分别表示有n种商品,让你排列出最长长度为m的序列,有n行,每行有k表示该种商品后边可以放k种商品,然后后边是k个数。
 
解题思路:这道题跟嫦娥那题其实比较像,都是先用dp求出转移方程,然后套用一下矩阵快速幂加速。因为是要求最长长度为m的序列,所以就需要求出长度为0 -> m的所有结果。分奇偶。当为偶:a1+a2+a3+a4....a2K 那么(a1+a2+a3....ak)*(1+ak)   。当为奇:a1+a2+a3+a4....a2K+1 那么(a1+a2+a3....ak)*(1+ak+1)+ak+1。    这就可以用dfs去处理。
 
#include<bits/stdc++.h>
using namespace std;
typedef long long INT;
const int MOD=2015;
int n;
struct Matrix{
int a[52][52];
Matrix(){
memset(a,0,sizeof(a));
}
void clr(){
memset(a,0,sizeof(a));
}
void init(){
for(int i=1;i<=n;i++){
a[i][i]=1;
}
}
Matrix operator *(const Matrix & X)const{
Matrix ret;
int i,j,k;
for(int i=1;i<=n;i++){ //可以先枚举k。再用n*n去取模,能加速。
for(int j=1;j<=n;j++){
for(int k=1;k<=n;k++){
ret.a[i][j] = ret.a[i][j] +a[i][k]*X.a[k][j];
}
ret.a[i][j]%=MOD;
}
}
return ret;
}
Matrix operator +(const Matrix & X)const {
Matrix ret;
for(int i=1;i<=n;i++){
for(int j=1;j<=n;j++){
ret.a[i][j]=a[i][j]+X.a[i][j];
}
}
return ret;
}
};
Matrix one;
Matrix Pow(Matrix A,int x){
Matrix ret;
ret.init();
while(x){ //可以换成for加速
if(x&1)
ret=ret*A;
A=A*A;
x>>=1;
}
return ret;
}
Matrix dfs(Matrix a,int k){
if(k==1)
return a;
if(k%2){
return (dfs(a,k/2)*(Pow(a,k/2+1)+one))+Pow(a,k/2+1);
}else{
return dfs(a,k/2)*(Pow(a,k/2)+one);
}
}
int main(){
int t,m,k,a;
scanf("%d",&t);
while(t--){
Matrix trans;
trans.clr();
scanf("%d%d",&n,&m);
one.init();
for(int i=1;i<=n;++i){
scanf("%d",&k);
for(int j=1;j<=k;++j){
scanf("%d",&a);
trans.a[i][a]=1;
}
}
if(m==1){
printf("%d\n",n+1);
continue;
}
Matrix ans=dfs(trans,m-1);
INT sum=0;
for(int i=1;i<=n;i++){
for(int j=1;j<=n;j++){
sum+=ans.a[i][j];
}
}
sum+=n+1;
printf("%lld\n",sum%2015);
}
return 0;
}

  

 
 

HDU5411——CRB and Puzzle——————【矩阵快速幂优化dp】的更多相关文章

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

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

  2. 2018.10.23 bzoj1297: [SCOI2009]迷路(矩阵快速幂优化dp)

    传送门 矩阵快速幂优化dp简单题. 考虑状态转移方程: f[time][u]=∑f[time−1][v]f[time][u]=\sum f[time-1][v]f[time][u]=∑f[time−1 ...

  3. 2018.10.22 bzoj1009: [HNOI2008]GT考试(kmp+矩阵快速幂优化dp)

    传送门 f[i][j]f[i][j]f[i][j]表示从状态"匹配了前i位"转移到"匹配了前j位"的方案数. 这个东西单次是可以通过跳kmp的fail数组得到的 ...

  4. 2018.10.16 uoj#340. 【清华集训2017】小 Y 和恐怖的奴隶主(矩阵快速幂优化dp)

    传送门 一道不错的矩阵快速幂优化dpdpdp. 设f[i][j][k][l]f[i][j][k][l]f[i][j][k][l]表示前iii轮第iii轮还有jjj个一滴血的,kkk个两滴血的,lll个 ...

  5. 省选模拟赛 Problem 3. count (矩阵快速幂优化DP)

    Discription DarrellDarrellDarrell 在思考一道计算题. 给你一个尺寸为 1×N1 × N1×N 的长条,你可以在上面切很多刀,要求竖直地切并且且完后每块的长度都是整数. ...

  6. 【bzoj1009】[HNOI2008]GT考试(矩阵快速幂优化dp+kmp)

    题目传送门:https://www.lydsy.com/JudgeOnline/problem.php?id=1009 这道题一看数据范围:$ n<=10^9 $,显然不是数学题就是矩乘快速幂优 ...

  7. 2019.02.11 bzoj4818: [Sdoi2017]序列计数(矩阵快速幂优化dp)

    传送门 题意简述:问有多少长度为n的序列,序列中的数都是不超过m的正整数,而且这n个数的和是p的倍数,且其中至少有一个数是质数,答案对201704082017040820170408取模(n≤1e9, ...

  8. 2018.10.19 NOIP模拟 硬币(矩阵快速幂优化dp)

    传送门 不得不说神仙出题人DZYODZYODZYO出的题是真的妙. f[i][j][k]f[i][j][k]f[i][j][k]表示选的硬币最大面值为iii最小面值不小于jjj,总面值为kkk时的选法 ...

  9. LOJ2325. 「清华集训 2017」小 Y 和恐怖的奴隶主【矩阵快速幂优化DP】【倍增优化】

    LINK 思路 首先是考虑怎么设计dp的状态 发现奴隶主的顺序没有影响,只有生命和个数有影响,所以就可以把每个生命值的奴隶主有多少压缩成状态就可以了 然后发现无论是什么时候一个状态到另一个状态的转移都 ...

随机推荐

  1. UIPageViewController

    前言 iPhone 和 iPad 都是通过页控件来展示多个桌面,很多 App 在第一次使用时也会使用页控件来介绍自己的功能,页控件的交互效果非常好,适用于把几个简单的页面充分展示出来. 1.UIPag ...

  2. Python中的可迭代对象

      Python中的可迭代对象有:列表.元组.字典.字符串:常结合for循环使用: 判断一个对象是不是可迭代对象: from collections import Iterable isinstanc ...

  3. vue框架搭建的详细步骤(一)

    在这里我们先快速的搭建一个vue的脚手架: (1).在安装vue的环境之前,安装NodeJS环境是必须的.可以使用node -v指令检查,需要保证安装了4.0版本以上的nodeJS环境. 没有安装的话 ...

  4. EXTJs前后台交互 常用哦3种方式

    <1>Ajax交互方式 Ext.Ajax.request( { //被用来向服务器发起请求默认的url url : "", //请求时发送后台的参数,既可以是Json对 ...

  5. mysql远程访问被禁止

    远程连接Mysql服务器的数据库,错误代码是1130,ERROR 1130: Host xxx.xxx.xxx.xxx  is not allowed to connect to this MySQL ...

  6. Jmeter-响应结果unicode转成中文显示

    本文为转载微信公众号文章,如作者发现后不愿意,请联系我进行删除 原文链接:http://mp.weixin.qq.com/s?__biz=MjM5OTI2MTQ3OA==&mid=265217 ...

  7. luogu2522 [HAOI2011]Problem b

    luogu2522[HAOI2011]Problem b 对于给出的n个询问,每次求有多少个数对(x,y),满足a≤x≤b,c≤y≤d,且gcd(x,y) = k,gcd(x,y)函数为x和y的最大公 ...

  8. 使用原生js来操作对象dom的class属性

    之前一直都使用jquery来操作dom,今天想自己用原生写一些插件,却发现给dom增删class的时候,使用slice来截取className特别的麻烦,后来发现,原来原生JS本来就有提供api来对d ...

  9. 19.Longest Substring Without Repeating Characters(长度最长的不重复子串)

    Level:   Medium 题目描述: Given a string, find the length of the longest substring without repeating cha ...

  10. 2019-5-1 maven学习笔记

    一.maven的好处 同样的项目使用maven工程来实现,由于不需要导入很多jar包,源码很小 原理:根据坐标到项目的仓库里查找需要的依赖 二.安装步骤 1.到http://maven.apache. ...