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的状态 发现奴隶主的顺序没有影响,只有生命和个数有影响,所以就可以把每个生命值的奴隶主有多少压缩成状态就可以了 然后发现无论是什么时候一个状态到另一个状态的转移都 ...
随机推荐
- 实现基于dotnetcore的扫一扫登录功能
第一次写博客,前几天看到.netcore的认证,就心血来潮想实现一下基于netcore的一个扫一扫的功能,实现思路构思大概是web端通过cookie认证进行授权,手机端通过jwt授权,web端登录界面 ...
- Python中的map_reduce
原教程地址: map/reduce-廖雪峰 将数值型字符串转换成数值,解释map, reduce的使用: #!/usr/bin/env python #-*- coding:utf-8 -*- ...
- LAMP之PHP
保持apache.mysql正在运行 [root@cairui php-]# lsof -i tcp: COMMAND PID USER FD TYPE DEVICE SIZE/OFF NODE NA ...
- CF581C Developing Skills 模拟
Petya loves computer games. Finally a game that he's been waiting for so long came out! The main cha ...
- windows 常用 命令
Hyper-v管理 virtmgmt.msc 网络 ncpa.cpl windows 远程登陆 mstsc.cpl C ...
- C语言中Extern用法
extern用在变量或函数的声明前,用来说明“此变量/函数是在别处定义的,要在此处引用”. extern修饰变量的声明. 举例:若a.c中需引用b.c中的变量int v,可以在a.c中声明extern ...
- JavaScript权威指南--立即执行函数
千万不要停下追逐梦想的脚步 (function(){ //execute this method immediatly. //content... }());
- 前端基础------jquer y学习
一. jquery是什么 快速,简洁,轻量级的JavaScript库(JavaScript框架)使用户可以快速的操作HTML document,实现动画效果,并方便的地为网站提供AJAX交互.文档全面 ...
- hive参数设置
-- 设置hive的计算引擎为spark set hive.execution.engine=spark; -- 修复分区 set hive.msck.path.validation=ignore; ...
- docker(4)使用Dockerfile文件创建镜像-对docker(3)的改进
在<docker(3)docker下的centos7下安装jdk>中,当进入容器后,执行 java命令 不能运行,需要执行source /etc/profile才能执行.如果采用Docke ...