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. pycharm激活2018

    因为我的是Windows,所以这篇文章只针对Windows系统. 1.将“0.0.0.0 account.jetbrains.com”中的内容添加到hosts文件中,hosts路径为:C:\Windo ...

  2. filezilla server FTP 安装报错 "could not load TLS network. Aborting start of administration interface"

    filezilla server FTP 安装报错   "could not load TLS network. Aborting start of administration inter ...

  3. tomcat idea 报权限错误

    出现的错误提示如下: 下午9:11:27 All files are up-to-date下午9:11:27 All files are up-to-date下午9:11:27 Error runni ...

  4. win10下安装psql9,后无法访问数据库引擎

    1.修改安装文件兼容性,并启动安装 2.安装后 修改psql control center快捷方式的启动文件兼容性 3.修改 start workgroup engine 快捷方式的启动文件兼容性 一 ...

  5. Quartz.Net 学习之路02 初探Quartz.Net

    第二讲:简单的实例,看看Quartz.Net强在哪里? 直接上代码,代码里有注释: using System; using Quartz; using Quartz.Impl; namespace L ...

  6. DBA的40条军规

    DBA操作规范 1.涉及业务上的修改/删除数据,在得到业务方.CTO的邮件批准后方可执行,执行前提前做好备份,必要时可逆. 2.所有上线需求必须走工单系统,口头通知视为无效. 3.在对大表做表结构变更 ...

  7. [NOIP2008] 提高组 洛谷P1149 火柴棒等式

    题目描述 给你n根火柴棍,你可以拼出多少个形如“A+B=C”的等式?等式中的A.B.C是用火柴棍拼出的整数(若该数非零,则最高位不能是0).用火柴棍拼数字0-9的拼法如图所示: 注意: 加号与等号各自 ...

  8. codevs1792 分解质因数

    题目描述 Description 编写一个把整数N分解为质因数乘积的程序. 输入描述 Input Description 输入一个整数 N 输出描述 Output Description 输出 分解质 ...

  9. 【IntelliJ 】设置 IntelliJ IDEA 主题和字体的方法

    2 主题修改 2.1 界面主题修改 如上图所示,依次点击Files -> Settings,进入如下界面: 标注1:主题选择区: 标注2:Darcula.IntelliJ 和 Windows,三 ...

  10. msp430入门学习11

    msp430的定时器--看门狗 msp430入门学习