CRB and Puzzle

Time Limit: 2000/1000 MS (Java/Others)    Memory
Limit: 65536/65536 K (Java/Others)

Total Submission(s): 483    Accepted Submission(s): 198

Problem Description
CRB is now playing Jigsaw Puzzle.

There are  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  pieces?
(Two patterns  and  are
considered different if their lengths are different or there exists an integer  such
that -th
piece of 

rev=2.4-beta-2" alt="" style=""> is
different from corresponding piece of .)

 
Input
There are multiple test cases. The first line of input contains an integer 

rev=2.4-beta-2" alt="" style="">,
indicating the number of test cases. For each test case:

The first line contains two integers 

rev=2.4-beta-2" alt="" style="">,  denoting
the number of kinds of pieces and the maximum number of moves.

Then  lines
follow. -th
line is described as following format.

rev=2.4-beta-2" alt="" style="">

rev=2.4-beta-2" alt="" style="">

rev=2.4-beta-2" alt="" style="">

rev=2.4-beta-2" alt="" style="">

rev=2.4-beta-2" alt="" style="">

rev=2.4-beta-2" alt="" style="">

rev=2.4-beta-2" alt="" style="">

Here 

rev=2.4-beta-2" alt="" style=""> is
the number of kinds which can be assembled to the right of the -th
kind. Next  integers
represent each of them.

1 ≤ 

rev=2.4-beta-2" alt="" style=""> ≤
20

1 ≤  ≤
50

1 ≤  ≤ 

rev=2.4-beta-2" alt="" style="">

rev=2.4-beta-2" alt="" style="">

0 ≤  ≤ 

1 ≤  < 

rev=2.4-beta-2" alt="" style=""> <
… <  ≤
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)

解题思路:

DP方程非常easy想到 dp[i][j] = sum(dp[i-1][k] <k,j>连通) 构造矩阵用矩阵高速幂加速就可以。
#include <iostream>
#include <cstring>
#include <cstdlib>
#include <cstdio>
#include <cmath>
#include <queue>
#include <set>
#include <map>
#include <algorithm>
#define LL long long
using namespace std;
const int MAXN = 55 + 10;
const int mod = 2015;
int n, m;
struct Matrix
{
int m[MAXN][MAXN];
Matrix(){memset(m, 0, sizeof(m));}
Matrix operator * (const Matrix &b)const
{
Matrix res;
for(int i=1;i<=n+1;i++)
{
for(int j=1;j<=n+1;j++)
{
for(int k=1;k<=n+1;k++)
{
res.m[i][j] = (res.m[i][j] + m[i][k] * b.m[k][j]) % mod;
}
}
}
return res;
}
};
Matrix pow_mod(Matrix a, int b)
{
Matrix res;
for(int i=1;i<=n+1;i++) res.m[i][i] = 1;
while(b)
{
if(b & 1) res = res * a;
a = a * a;
b >>= 1;
}
return res;
}
int main()
{
int T;
scanf("%d", &T);
while(T--)
{
Matrix a, b;
scanf("%d%d", &n, &m);
for(int i=1;i<=n+1;i++) a.m[i][n+1] = 1;
for(int i=1;i<=n;i++)
{
int x, k;scanf("%d", &k);
for(;k--;)
{
scanf("%d", &x);
a.m[i][x] = 1;
}
}
a = pow_mod(a, m);
int ans = 0;
for(int i=1;i<=n+1;i++) ans = (ans + a.m[i][n+1]) % mod;
printf("%d\n", ans);
}
return 0;
}

 

HDU 5411 CRB and puzzle (Dp + 矩阵高速幂)的更多相关文章

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

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

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

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

  3. HDU 4965 Fast Matrix Calculation(矩阵高速幂)

    HDU 4965 Fast Matrix Calculation 题目链接 矩阵相乘为AxBxAxB...乘nn次.能够变成Ax(BxAxBxA...)xB,中间乘n n - 1次,这样中间的矩阵一个 ...

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

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

  5. 2015 Multi-University Training Contest 10 hdu 5411 CRB and Puzzle

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

  6. POJ3420 Quad Tiling DP + 矩阵高速幂

    题目大意是用1*2的骨牌堆积成4*N的矩形.一共同拥有多少种方法,N不超过10^9. 这题和以前在庞果网上做过的一道木块砌墙差点儿一样. 由于骨牌我们能够横着放.竖着放.我们如果以4为列,N为行这样去 ...

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

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

  8. hdu 3221 Brute-force Algorithm(高速幂取模,矩阵高速幂求fib)

    http://acm.hdu.edu.cn/showproblem.php?pid=3221 一晚上搞出来这么一道题..Mark. 给出这么一个程序.问funny函数调用了多少次. 我们定义数组为所求 ...

  9. poj 3744 Scout YYF I (可能性DP+矩阵高速功率)

    Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 5062   Accepted: 1370 Description YYF i ...

随机推荐

  1. java.lang.IllegalArgumentException: Result Maps collection already contains value for com.zhmy.businessapi.mapper.CompanyMapper.BaseResultMap

    复制mybatis的mapper.xml文件修改后,忘记将xml中的mapper标签里的namespace更换成对应类了,修改完即可

  2. 第3节 mapreduce高级:7、自定义outputformat实现输出到不同的文件夹下面

    2.1 需求 现在有一些订单的评论数据,需求,将订单的好评与差评进行区分开来,将最终的数据分开到不同的文件夹下面去,数据内容参见资料文件夹,其中数据第九个字段表示好评,中评,差评.0:好评,1:中评, ...

  3. django 标签

    django标签 {% if/for/ifequal/ifnotequal condition %} ...{{ name|first|lower}}{# interpretation:lower t ...

  4. CSS工具、CSS重置(CSS Reset)

    样式重置的目的是减少浏览器的不一致性,例如line-height,margin,标题的font-size大小等等.样式重置经常在CSS框架中出现. 这里的重置样式故意写的很一般,例如没有为body元素 ...

  5. Java中Arrays作用

    Arrays类是提供对数组进行排序.查询和修改等操作方法的工具类.(Arrays:尽可进行数组升序排序) 格式:Arrays.sort(数组名); Package Java; import java. ...

  6. CSU1022: 菜鸟和大牛

    1022: 菜鸟和大牛 Submit Page   Summary   Time Limit: 1 Sec     Memory Limit: 128 Mb     Submitted: 1390   ...

  7. 在rubymine中集成heroku插件

    先安装heroku,参见http://www.cnblogs.com/jecyhw/p/4906990.html Heroku安装之后,就自动安装上git,目录为C:\Program Files (x ...

  8. Quartz Spring分布式集群搭建Demo

    注:关于单节点的Quartz使用在这里不做详细介绍,直接进阶为分布式集群版的 1.准备工作: 使用环境Spring4.3.5,Quartz2.2.3,持久化框架JDBCTemplate pom文件如下 ...

  9. Springboot 缓存使用

    . CachingProvider . CacheManager . Cache . Entry . Expiry 1. 开启基于注解的缓存 @EnableCaching 下面列出几个核心的注解 @C ...

  10. 利用express启一个server服务

    安装express $ npm install express --save 在node.js中,我们最常用的框架就是express Express 是一个基于 Node.js 平台的极简.灵活的 w ...