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): 558 Accepted Submission(s): 227
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 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
possible patterns are ∅, 1, 2, 3, 1→2, 2→3
#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】的更多相关文章
- hdu 5411 CRB and Puzzle (矩阵高速幂优化dp)
题目:http://acm.hdu.edu.cn/showproblem.php?pid=5411 题意:按题目转化的意思是,给定N和M,再给出一些边(u,v)表示u和v是连通的,问走0,1,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 ...
- 2018.10.22 bzoj1009: [HNOI2008]GT考试(kmp+矩阵快速幂优化dp)
传送门 f[i][j]f[i][j]f[i][j]表示从状态"匹配了前i位"转移到"匹配了前j位"的方案数. 这个东西单次是可以通过跳kmp的fail数组得到的 ...
- 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个 ...
- 省选模拟赛 Problem 3. count (矩阵快速幂优化DP)
Discription DarrellDarrellDarrell 在思考一道计算题. 给你一个尺寸为 1×N1 × N1×N 的长条,你可以在上面切很多刀,要求竖直地切并且且完后每块的长度都是整数. ...
- 【bzoj1009】[HNOI2008]GT考试(矩阵快速幂优化dp+kmp)
题目传送门:https://www.lydsy.com/JudgeOnline/problem.php?id=1009 这道题一看数据范围:$ n<=10^9 $,显然不是数学题就是矩乘快速幂优 ...
- 2019.02.11 bzoj4818: [Sdoi2017]序列计数(矩阵快速幂优化dp)
传送门 题意简述:问有多少长度为n的序列,序列中的数都是不超过m的正整数,而且这n个数的和是p的倍数,且其中至少有一个数是质数,答案对201704082017040820170408取模(n≤1e9, ...
- 2018.10.19 NOIP模拟 硬币(矩阵快速幂优化dp)
传送门 不得不说神仙出题人DZYODZYODZYO出的题是真的妙. f[i][j][k]f[i][j][k]f[i][j][k]表示选的硬币最大面值为iii最小面值不小于jjj,总面值为kkk时的选法 ...
- LOJ2325. 「清华集训 2017」小 Y 和恐怖的奴隶主【矩阵快速幂优化DP】【倍增优化】
LINK 思路 首先是考虑怎么设计dp的状态 发现奴隶主的顺序没有影响,只有生命和个数有影响,所以就可以把每个生命值的奴隶主有多少压缩成状态就可以了 然后发现无论是什么时候一个状态到另一个状态的转移都 ...
随机推荐
- Win10下Tensorflow+GPU的环境配置
不得不说,想要为深度学习提前打好框架确实需要花费一番功夫.本文主要记录了Win10下,Cuda9.0.Cudnn7.3.1.Tensorflow-gpu1.13.1.python3.6.8.Keras ...
- uint8_t / uint16_t / uint32_t /uint64_t 是什么数据类型
在nesc的代码中,你会看到很多你不认识的数据类型,比如uint8_t等.咋一看,好像是个新的数据类型,不过C语言(nesc是C的扩展)里面好像没有这种数据类型啊!怎么又是u又是_t的?很多人有这样的 ...
- javascript小菜单—demo
<!DOCTYPE html><html><head> <title></title></head><body>&l ...
- ArcGIS-各类问题
arcgis 10.4破解方法*注意!Desktop,Engine,Server必须为同一版本 1.先安装License10.4 2.再安装Desktop10.4 3.再安装Engine10.4 4. ...
- C++的一种业务分发方案(另类的工厂模式)
在C++中,传统的业务分发.总要写一大串的switch-case,并且每次添加新业务时.都要在原有的switch-case里加一个分支,这就违反了设计模式中的开放封闭原则. 下面这样的方案,就全然去除 ...
- Java 标准IO高可用类
输出: 使用: * <pre> * public class TestStdOut { * public static void main(String[] args) { * int a ...
- Solr学习笔记(2) —— Solr管理索引库
一.维护索引 1.1 添加/更新文档 1.2 批量导入数据(使用dataimport) 第一步:把mysql的数据驱动.以及dataimport插件依赖的jar包添加到solrcore(collect ...
- Educational Codeforces Round 11 B
Description Consider 2n rows of the seats in a bus. n rows of the seats on the left and n rows of th ...
- Java使用Array类创建多维数组
1.创建一维数组 import java.lang.reflect.Array; public class ArrayTest { public static void main(String[] a ...
- 免费的mysql数据库
https://blog.csdn.net/kernel_/article/details/53320498