UVA11551 Experienced Endeavour —— 矩阵快速幂
题目链接:https://vjudge.net/problem/UVA-11551


题意:
给定一列数,每个数对应一个变换,变换为原先数列一些位置相加起来的和,问r次变换后的序列是多少
题解:
构造矩阵:要加的位置值为1,其余位置为0。然后用快速幂计算。
代码如下:
#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <vector>
#include <cmath>
#include <queue>
#include <stack>
#include <map>
#include <string>
#include <set>
using namespace std;
typedef long long LL;
const int INF = 2e9;
const LL LNF = 9e18;
//const int MOD = 1000000007;
const int MAXN = 1e6+; const int MOD = ;
const int Size = ;
struct MA
{
LL mat[Size][Size];
void init()
{
for(int i = ; i<Size; i++)
for(int j = ; j<Size; j++)
mat[i][j] = (i==j);
}
}; MA mul(MA x, MA y)
{
MA ret;
memset(ret.mat, , sizeof(ret.mat));
for(int i = ; i<Size; i++)
for(int j = ; j<Size; j++)
for(int k = ; k<Size; k++)
ret.mat[i][j] += (1LL*x.mat[i][k]*y.mat[k][j])%MOD, ret.mat[i][j] %= MOD;
return ret;
} MA qpow(MA x, LL y)
{
MA s;
s.init();
while(y)
{
if(y&) s = mul(s, x);
x = mul(x, x);
y >>= ;
}
return s;
} int main()
{
LL T, n, r, a[];
scanf("%lld", &T);
while(T--)
{
scanf("%lld%lld", &n, &r);
for(int i = ; i<n; i++)
scanf("%lld", &a[i]); MA s;
memset(s.mat, , sizeof(s.mat));
for(int i = ; i<n; i++)
{
int m, x;
scanf("%d", &m);
while(m--)
{
scanf("%d", &x);
s.mat[i][x] = ;
}
} s = qpow(s, r);
for(int i = ; i<n; i++)
{
LL sum = ;
for(int j = ; j<n; j++)
sum += s.mat[i][j]*a[j], sum %= MOD;
printf("%lld", sum);
if(i!=n-) printf(" ");
}
printf("\n");
}
}
UVA11551 Experienced Endeavour —— 矩阵快速幂的更多相关文章
- F - Experienced Endeavour 矩阵快速幂
Alice is given a list of integers by Bob and is asked to generate a new list where each element in t ...
- UVA 11551 - Experienced Endeavour(矩阵高速幂)
UVA 11551 - Experienced Endeavour 题目链接 题意:给定一列数,每一个数相应一个变换.变换为原先数列一些位置相加起来的和,问r次变换后的序列是多少 思路:矩阵高速幂,要 ...
- uva11551矩阵快速幂
题目看了半天没看懂,,就是把一个数列更新r次,每次更新就是计算和,就是每一个数,只要出现了的表号都要加上去,具体看代码 矩阵快速幂实现加速 #include<map> #include&l ...
- 矩阵快速幂 HDU 4565 So Easy!(简单?才怪!)
题目链接 题意: 思路: 直接拿别人的图,自己写太麻烦了~ 然后就可以用矩阵快速幂套模板求递推式啦~ 另外: 这题想不到或者不会矩阵快速幂,根本没法做,还是2013年长沙邀请赛水题,也是2008年Go ...
- 51nod 算法马拉松18 B 非010串 矩阵快速幂
非010串 基准时间限制:1 秒 空间限制:131072 KB 分值: 80 如果一个01字符串满足不存在010这样的子串,那么称它为非010串. 求长度为n的非010串的个数.(对1e9+7取模) ...
- 51nod 1113 矩阵快速幂
题目链接:51nod 1113 矩阵快速幂 模板题,学习下. #include<cstdio> #include<cmath> #include<cstring> ...
- 【66测试20161115】【树】【DP_LIS】【SPFA】【同余最短路】【递推】【矩阵快速幂】
还有3天,今天考试又崩了.状态还没有调整过来... 第一题:小L的二叉树 勤奋又善于思考的小L接触了信息学竞赛,开始的学习十分顺利.但是,小L对数据结构的掌握实在十分渣渣.所以,小L当时卡在了二叉树. ...
- HDU5950(矩阵快速幂)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5950 题意:f(n) = f(n-1) + 2*f(n-2) + n^4,f(1) = a , f(2 ...
- 51nod 1126 矩阵快速幂 水
有一个序列是这样定义的:f(1) = 1, f(2) = 1, f(n) = (A * f(n - 1) + B * f(n - 2)) mod 7. 给出A,B和N,求f(n)的值. Input 输 ...
随机推荐
- git上传(本地和远程有冲突时)
一. 冲突的产生:在上次git同步(上传)之后,本地和远程均有更改 二. 处理 1. 丢弃本地,采用远程: git checkout 冲突文件及其路径 如: git checkout bzrobot_ ...
- Linux下安装python3.3.2及configrue、make、make install
一.安装python3.3.2 raspberry的/usr/local/src目录没有权限,可执行如下命令 pi@raspberrypi:~$ sudo chmod -R 777 /usr/loca ...
- Java随机数技巧-新手篇
package 向家康; import java.util.concurrent.ThreadLocalRandom; public class 练习21 { public static void m ...
- HDU5618 Jam's problem again
CDQ分治模板题 #include<cstdio> #include<cctype> #include<algorithm> #include<cstring ...
- xshell配置
字体:DejaVu Sans Mono 或者 Consolas 11号
- [反汇编练习] 160个CrackMe之037
[反汇编练习] 160个CrackMe之037. 本系列文章的目的是从一个没有任何经验的新手的角度(其实就是我自己),一步步尝试将160个CrackMe全部破解,如果可以,通过任何方式写出一个类似于注 ...
- C#文件路径操作总结【转】
http://www.cnblogs.com/zhoufoxcn/archive/2006/10/24/2515874.html 一.获取当前文件的路径 1. System.Diagnostics ...
- ubuntu 卸载干净软件(包括配置文件)
var/cache/apt/archives occupying huge space I am in the process of cleaning up my system. And I see ...
- Win7如何自定义鼠标右键菜单 添加新建EXCEL文档
鼠标右键添加新建EXCEL文档.reg Windows Registry Editor Version 5.00 [HKEY_CLASSES_ROOT\.xls] "Content Type ...
- USACO 1.2 Milking Cows (枚举)
标记数组(哈希) 1e6的范围,开一个char数组全然能够,有人为1,无人为0,注意边界就可以.最后线性扫描就可以. 时间复杂度,应该是O(n),n为最后结束的时间. 缺点就是--比較慢 /* ID: ...