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 输 ...
随机推荐
- [Machine Learning with Python] Familiar with Your Data
Here I list some useful functions in Python to get familiar with your data. As an example, we load a ...
- DozerBeanMapper + 对象转Map方法
1.简介 dozer是一种JavaBean的映射工具,类似于apache的BeanUtils.但是dozer更强大,它可以灵活的处理复杂类型之间的映射.不但可以进行简单的属性映射.复杂的类型映 ...
- remove xcode recent projects from dock menu 移除xcode dock菜单显示的项目列表
Launch Xcode Select File->Open Recent->Clear Menu Right-click the Xcode icon and select Show A ...
- awk的求和计算使用;awk多个分隔符如何使用?
1.对于下图,如何使用awk求所有各列的和 和:175 16 78 19 方法:awk '{for(n=1;n<=NF;n++)t[n]+=$n}END{for(n=1;n<=NF;n++ ...
- 如何模拟alert/confirm/prompt实现阻断程序运行
场景:在执行js的时候,我们希望运行到某处,进行用户交互,根据交互的内容,运行下面的程序:下面的js程序需要用的和用户交互的内容,所以,和用户交互时,后面的程序必须停止运行 方案: 1. 原生的ale ...
- Odoo event
使用流程 建立活动 发布到网站 在线销售 订单确认,付款确认 注册.出席 建立活动 设置门票 确认并发布到网站 进入编辑模式,即可在线编辑活动 ...
- iterm2 配色
http://blog.csdn.net/sanwuhai/article/details/48729561
- 读取配置文件(configparser,.ini文件)
使用configparser来读取配置信息config.ini 读取的信息(config.ini)如下: [baseconf]host=127.0.0.1port=3306user=rootpassw ...
- Python--数据类型整理
数据类型整理-------------------------------------------------------------------------------------------- ...
- canvas 五角星之回顾【初中三角函数】
当程序中遇到三角函数的时候我是懵逼的,于是百度了“初中三角函数”, 忘了这几个公式的,自己打脸. 目的是通过Canvas画一个五角星, 突破口:只要能通过给定的两个外圈点的半径,和内圈点的半径,借助上 ...