直接构造矩阵,最上面一行加一排1.高速幂计算矩阵的m次方,统计第一行的和

CRB and Puzzle

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

Total Submission(s): 133    Accepted Submission(s): 63

Problem Description
CRB is now playing Jigsaw Puzzle.

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.)

 
Input
There are multiple test cases. The first line of input contains an integer T,
indicating the number of test cases. For each test case:

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


 
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)
 
Source
 

/* ***********************************************
Author :CKboss
Created Time :2015年08月20日 星期四 23时25分19秒
File Name :HDOJ5411.cpp
************************************************ */ #include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <string>
#include <cmath>
#include <cstdlib>
#include <vector>
#include <queue>
#include <set>
#include <map> using namespace std; const int mod=2015; int n,m; struct Matrix
{
int m[60][60];
Matrix() { memset(m,0,sizeof(m)); }
void getE()
{
for(int i=0;i<n;i++) m[i][i]=1;
}
void toString()
{
for(int i=0;i<n;i++)
{
for(int j=0;j<n;j++)
{
printf("%d,",m[i][j]);
}
putchar(10);
}
}
}; Matrix Mulit(Matrix a,Matrix b)
{
Matrix M;
for(int i=0;i<n;i++)
{
for(int j=0;j<n;j++)
{
int temp=0;
for(int k=0;k<n;k++)
{
temp=(temp+a.m[i][k]*b.m[k][j])%mod;
}
M.m[i][j]=temp;
}
}
return M;
} Matrix QuickPow(Matrix a,int x)
{
Matrix e;
e.getE();
while(x)
{
if(x&1) e=Mulit(e,a);
a=Mulit(a,a);
x/=2;
}
return e;
} int main()
{
//freopen("in.txt","r",stdin);
//freopen("out.txt","w",stdout); int T_T;
scanf("%d",&T_T);
while(T_T--)
{
scanf("%d%d",&n,&m);
Matrix M;
for(int i=1;i<=n;i++)
{
int k,x;
scanf("%d",&k);
for(int j=0;j<k;j++)
{
scanf("%d",&x);
M.m[i][x]=1;
}
}
n++;
for(int i=0;i<n;i++) M.m[0][i]=1;
Matrix mt=QuickPow(M,m);
int ans=0;
for(int i=0;i<n;i++)
{
ans=(ans+mt.m[0][i])%mod;
}
printf("%d\n",ans);
} return 0;
}

HDOJ 5411 CRB and Puzzle 矩阵高速幂的更多相关文章

  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 (矩阵高速幂优化dp)

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

  3. HDOJ 4686 Arc of Dream 矩阵高速幂

    矩阵高速幂: 依据关系够建矩阵 , 高速幂解决. Arc of Dream Time Limit: 2000/2000 MS (Java/Others)    Memory Limit: 65535/ ...

  4. HDU5411CRB and Puzzle(矩阵高速幂)

    题目链接:传送门 题意: 一个图有n个顶点.已知邻接矩阵.问点能够反复用长度小于m的路径有多少. 分析: 首先我们知道了邻接矩阵A.那么A^k代表的就是长度为k的路径有多少个. 那么结果就是A^0+A ...

  5. HDU 5411 CRB and puzzle (Dp + 矩阵高速幂)

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

  6. HDOJ 4549 M斐波那契数列 费马小定理+矩阵高速幂

    MF( i ) = a ^ fib( i-1 ) * b ^ fib ( i )   ( i>=3) mod 1000000007 是质数 , 依据费马小定理  a^phi( p ) = 1 ( ...

  7. HDOJ How many ways?? 2157【矩阵高速幂】

    How many ways? ? Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) ...

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

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

  9. UVA 11551 - Experienced Endeavour(矩阵高速幂)

    UVA 11551 - Experienced Endeavour 题目链接 题意:给定一列数,每一个数相应一个变换.变换为原先数列一些位置相加起来的和,问r次变换后的序列是多少 思路:矩阵高速幂,要 ...

随机推荐

  1. ActiveMQ学习笔记(7)----ActiveMQ支持的传输协议

    1. 连接到ActiveMQ Connector: Active提供的,用来实现连接通讯的功能,包括:client-to-broker,broker-to-broker.ActiveMQ允许客户端使用 ...

  2. SASS 使用(vs code)

    二.在vs code中编译sass 1.在拓展商店里搜索“easy sass”,并安装,安装成功后点重新加载. 2.接下来进行配置: 在 vs code 菜单栏依次点击“文件 首选项 设置”,打开 s ...

  3. luogu P2117 小Z的矩阵(结论题)

    题意 题解 这题有点水. 我们发现对答案有贡献的实际上只有左上到右下的对角线上的数. 因为不在这条对角线上的乘积都要计算两遍,然后%2就都没了... 然后就做完了. #include<iostr ...

  4. Redis学习总结(3)——Redis整合Spring结合使用缓存实例

    摘要:本文介绍了如何在Spring中配置redis,并通过Spring中AOP的思想,将缓存的方法切入到有需要进入缓存的类或方法前面. 一.Redis介绍 什么是Redis? redis是一个key- ...

  5. 【转】一天学会PHP(转)

    [转]一天学会PHP(转) 只需要一天,只要你用心去看和学,一定行. - 这里希望大家需要明白一点,这只是在讲如何快速入门,更好的认识PHP!也能初级掌握PHP基础知识!PHP语言博大精深!并不是一两 ...

  6. vjudge A - Beautiful numbers

    A - Beautiful numbers Volodya is an odd boy and his taste is strange as well. It seems to him that a ...

  7. spring boot启动STS 运行报错 java.lang.NoClassDefFoundError: ch/qos/logback/classic/LoggerContext

    spring boot启动STS 运行报错 java.lang.NoClassDefFoundError: ch/qos/logback/classic/LoggerContext 学习了: http ...

  8. [MST] Defining Asynchronous Processes Using Flow

    In real life scenarios, many operations on our data are asynchronous. For example, because additiona ...

  9. [MST] Test mobx-state-tree Models by Recording Snapshots or Patches

    Testing models is straightforward. Especially because MST provides powerful tools to track exactly h ...

  10. RecyclerView借助ItemTouchHelper实现拖动和滑动删除功能

    RecyclerView是官方推荐代替ListView的空间,怎样实现RecyclerView列表元素的拖动呢? 官方提供了ItemTouchHelper类使用过程例如以下: 定义ItemTouchH ...