HDOJ 5411 CRB and Puzzle 矩阵高速幂
直接构造矩阵,最上面一行加一排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
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.)
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
1
3 2
1 2
1 3
0
6Hintpossible patterns are ∅, 1, 2, 3, 1→2, 2→3
/* ***********************************************
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 矩阵高速幂的更多相关文章
- 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 (矩阵高速幂优化dp)
题目:http://acm.hdu.edu.cn/showproblem.php?pid=5411 题意:按题目转化的意思是,给定N和M,再给出一些边(u,v)表示u和v是连通的,问走0,1,2... ...
- HDOJ 4686 Arc of Dream 矩阵高速幂
矩阵高速幂: 依据关系够建矩阵 , 高速幂解决. Arc of Dream Time Limit: 2000/2000 MS (Java/Others) Memory Limit: 65535/ ...
- HDU5411CRB and Puzzle(矩阵高速幂)
题目链接:传送门 题意: 一个图有n个顶点.已知邻接矩阵.问点能够反复用长度小于m的路径有多少. 分析: 首先我们知道了邻接矩阵A.那么A^k代表的就是长度为k的路径有多少个. 那么结果就是A^0+A ...
- HDU 5411 CRB and puzzle (Dp + 矩阵高速幂)
CRB and Puzzle Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others) T ...
- HDOJ 4549 M斐波那契数列 费马小定理+矩阵高速幂
MF( i ) = a ^ fib( i-1 ) * b ^ fib ( i ) ( i>=3) mod 1000000007 是质数 , 依据费马小定理 a^phi( p ) = 1 ( ...
- HDOJ How many ways?? 2157【矩阵高速幂】
How many ways? ? Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) ...
- HDU 5411 CRB and Puzzle (2015年多校比赛第10场)
1.题目描写叙述:pid=5411">点击打开链接 2.解题思路:本题实际是是已知一张无向图.问长度小于等于m的路径一共同拥有多少条. 能够通过建立转移矩阵利用矩阵高速幂解决.当中,转 ...
- UVA 11551 - Experienced Endeavour(矩阵高速幂)
UVA 11551 - Experienced Endeavour 题目链接 题意:给定一列数,每一个数相应一个变换.变换为原先数列一些位置相加起来的和,问r次变换后的序列是多少 思路:矩阵高速幂,要 ...
随机推荐
- 使用python绘制词云
最近在忙考试的事情,没什么时间敲代码,一个月也没几天看代码,最近看到可视化的词云,看到网上也很多这样的工具, 但是都不怎么完美,有些不支持中文,有的中文词频统计得莫名其妙.有的不支持自定义形状.所有的 ...
- 玩转图片上传————原生js XMLHttpRequest 结合FormData对象实现的图片上传
var form=document.getElementById("formId"); var formData=new FormData(form); var oReq = ne ...
- 对称平方数(to_string函数,stoi函数真香)
题目描述 打印所有不超过n(n<256)的,其平方具有对称性质的数.如11*11=121. 输入描述: 无 输出描述: 每行一个数,表示对称平方数. 示例1 输入 复制 无 输出 复制 无解题思 ...
- C语言Huffman压缩和解压
符号表结构体: struct node { // 字符串形式存储的Huffman编码 char code[MAX_CODE_LENGTH]; // 这个字符在文件中出现的次数 long count; ...
- react-native 运行提示红屏 error: bundling failed: ambiguous resolution: module `/User/xxx/Project/ico/index.js` tries to require `react-native`, but there are several files providing this module. You can de
运行 react-native start 报错 执行这2个进行清除缓存问题 yarn start -- --reset-cache npm start -- --reset-cache
- docker私有仓库yum安装 docker-registry
[root@riyimei-node1:/root]> yum install docker-registryLoaded plugins: fastestmirror, langpacksLo ...
- The Zen of Python, by Tim Peters
Beautiful is better than ugly. Explicit is better than implicit. Simple is better than complex. Comp ...
- Object-C,NSArraySortTest,数组排序3种方式
晚上回来,继续写Object-C的例子,今天不打算写iOS可视化界面的程序,太累了. 刚刚dady又电话过来,老一套,烦死了. 其实,我一直一个观点,无论发生什么事情,不要整天一副不开心的样子. 开开 ...
- Qt 5.3 下OpenCV 2.4.11 开发(5)最高效的像素引用
OpenCV 提供一个函数 getTickCount() ,能够用来測量一段代码的执行时间.另一个函数 getTickFrequency() 用来返回每秒内的时钟周期.代码操作例如以下: double ...
- 【剑指Offer面试题】 九度OJ1516:调整数组顺序使奇数位于偶数前面
题目链接地址: http://ac.jobdu.com/problem.php?pid=1516 题目1516:调整数组顺序使奇数位于偶数前面 时间限制:1 秒内存限制:128 兆特殊判题:否提交:2 ...