HDU 5411 CRB and puzzle (Dp + 矩阵高速幂)
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
There are

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

(Two patterns


considered different if their lengths are different or there exists an integer

that

piece of
rev=2.4-beta-2" alt="" style=""> is
different from corresponding piece of .)
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.
k
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
1
3 2
1 2
1 3
0
6Hintpossible patterns are ∅, 1, 2, 3, 1→2, 2→3
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 + 矩阵高速幂)的更多相关文章
- hdu 5411 CRB and Puzzle 矩阵高速幂
链接 题解链接:http://www.cygmasot.com/index.php/2015/08/20/hdu_5411/ 给定n个点 常数m 以下n行第i行第一个数字表示i点的出边数.后面给出这些 ...
- HDU 5411 CRB and Puzzle (2015年多校比赛第10场)
1.题目描写叙述:pid=5411">点击打开链接 2.解题思路:本题实际是是已知一张无向图.问长度小于等于m的路径一共同拥有多少条. 能够通过建立转移矩阵利用矩阵高速幂解决.当中,转 ...
- HDU 4965 Fast Matrix Calculation(矩阵高速幂)
HDU 4965 Fast Matrix Calculation 题目链接 矩阵相乘为AxBxAxB...乘nn次.能够变成Ax(BxAxBxA...)xB,中间乘n n - 1次,这样中间的矩阵一个 ...
- HDU5411——CRB and Puzzle——————【矩阵快速幂优化dp】
CRB and Puzzle Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)To ...
- 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 ...
- POJ3420 Quad Tiling DP + 矩阵高速幂
题目大意是用1*2的骨牌堆积成4*N的矩形.一共同拥有多少种方法,N不超过10^9. 这题和以前在庞果网上做过的一道木块砌墙差点儿一样. 由于骨牌我们能够横着放.竖着放.我们如果以4为列,N为行这样去 ...
- hdu 5411 CRB and Puzzle (矩阵高速幂优化dp)
题目:http://acm.hdu.edu.cn/showproblem.php?pid=5411 题意:按题目转化的意思是,给定N和M,再给出一些边(u,v)表示u和v是连通的,问走0,1,2... ...
- hdu 3221 Brute-force Algorithm(高速幂取模,矩阵高速幂求fib)
http://acm.hdu.edu.cn/showproblem.php?pid=3221 一晚上搞出来这么一道题..Mark. 给出这么一个程序.问funny函数调用了多少次. 我们定义数组为所求 ...
- poj 3744 Scout YYF I (可能性DP+矩阵高速功率)
Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 5062 Accepted: 1370 Description YYF i ...
随机推荐
- 递归删除N天前的文件夹及子文件夹下的特定文件
@echo offrem 设置被删除文件夹路径set SrcDir=D:\tmp\test\rem 设置文件保存天数set Days=2rem /p指定搜索文件的路径 /s 在子目录中搜索 /m 指定 ...
- java中检测-在运行时指定对象是否是特定类的一个实例---关键字 instanceof
java 中的instanceof 运算符是用来在运行时指出对象是否是特定类的一个实例.instanceof通过返回一个布尔值来指出,这个对象是否是这个特定类或者是它的子类的一个实例. if(requ ...
- POJ 3264 RMQ问题 用dp解决
#include <cstdio> #include <cstring> #include <iostream> using namespace std; ; #d ...
- 接龙游戏(codevs 1051)
1051 接龙游戏 时间限制: 1 s 空间限制: 128000 KB 题目等级 : 钻石 Diamond 题解 题目描述 Description 给出了N个单词,已经按长度排好了序 ...
- [NOIP2004] 提高组 洛谷P1092 虫食算
题目描述 所谓虫食算,就是原先的算式中有一部分被虫子啃掉了,需要我们根据剩下的数字来判定被啃掉的字母.来看一个简单的例子: 43#9865#045 +8468#6633 44445509678 其中# ...
- POJ 2186 tarjan+缩点 基础题
Popular Cows Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 37111 Accepted: 15124 De ...
- hdu - 2066 一个人的旅行(基础最短路)
http://acm.hdu.edu.cn/showproblem.php?pid=2066 把与草儿相连的城市最短距离置为0,然后进行dijkstra,在t个城市里找出距离最近的一个即可. #inc ...
- BZOJ——2190: [SDOI2008]仪仗队
思路: 我们将其所在的位置设为(0,0),那么如果存在一个点(x,y),且有gcd(x,y)=k(k!=1),那么点(x/k,y/k)一定会将(x,y)挡住.而如果k=1,那么点(x,y)就一定会被看 ...
- 谈谈hibernate的缓存
一,hibernate是什么技术? 1 hibernate是一个orm 就是对象关系映射框架. 及我们创建的实体类对象与数据库表中的数据是一一对应的关系. 我们可以改变对象信息从而改变数据库中的 ...
- Ubuntu 16.04设置Redis为开机自动启动服务
继上一篇文章http://www.cnblogs.com/EasonJim/p/7599941.html安装好Redis后,假设文件已经安装到/usr/local/redis目录下.假设我安装的版本为 ...