直接构造矩阵,最上面一行加一排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学习笔记(19)----Consumer高级特性(一)

    1. Exclusive Consumer 独有消费者:Queue中的消息是按照顺序被分发到consumer的,然而,当你有多个consumers同时从相同的queue中提取消息时,你将失去这个保证. ...

  2. html转word文档

    html转word文档 package cn.com.szhtkj.util; import java.io.ByteArrayInputStream; import java.io.File; im ...

  3. 路飞学城-Python开发-第一章

    # 基础需求: # 让用户输入用户名密码 # 认证成功后显示欢迎信息 # 输错三次后退出程序 username = 'pandaboy' password = ' def Login(username ...

  4. moment.js获取本周本月本年的开始日期和结束日期

    //获取本日 const startDate = moment().format('YYYY-MM-DD'); const startDate = moment().format('YYYY-MM-D ...

  5. JavaScript内存机制

    内存模型 JS内存空间分为栈(stack).堆(heap).池(一般也会归类为栈中). 其中栈存放变量,堆存放复杂对象,池存放常量. 基础数据类型与栈内存 JS中的基础数据类型,这些值都有固定的大小, ...

  6. 【BZOJ2006】【NOI2010】超级钢琴

    题意: Description 小Z是一个小有名气的钢琴家,最近C博士送给了小Z一架超级钢琴,小Z希望能够用这架钢琴创作出世界上最美妙的音乐. 这架超级钢琴可以弹奏出n个音符,编号为1至n.第i个音符 ...

  7. NOIp2018模拟赛三十六

    好久没打模拟赛了...今天一样是两道国集,一道bzoj题 成绩:13+0+95=108 A题开始看错题了...导致样例都没看懂,结果xfz提醒我后我理解了一个我自认为正确的题意(事实证明我和xfz都错 ...

  8. BZOJ 4453 cys就是要拿英魂!(后缀数组+单调栈+平衡树)

    一开始的时候感觉就是一个主席树裸题. 然后发现自己错了. 首先建出后缀数组. 设\(i<j\) 如果\(rk[i]>rk[j]\)显然i更优. 如果\(rk[i]<rk[j]\)不一 ...

  9. HDU-2204- Eddy’s爱好 (容斥原理)

    题意 给出一个数n,问1-n中有多少个数可以表示为m^k,m,k均为正整数且k>1 (1<=n<=1^18) 题解 (一开始^以为是异或懵逼了好久....) 额,显然1这个数比较讨厌 ...

  10. java实现websocket 终极指南

    大概思路:  首先用户登陆  获取用户信息存储到httpsession中,然后客户端链接服务端websocket,首先HandshakeInterceptor这个拦截器会拦截请求 调用 beforeH ...