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

UVA 11551 - Experienced Endeavour

题目链接

题意:给定一列数,每一个数相应一个变换。变换为原先数列一些位置相加起来的和,问r次变换后的序列是多少

思路:矩阵高速幂,要加的位置值为1。其余位置为0构造出矩阵,进行高速幂就可以

代码:

#include <cstdio>
#include <cstring>

const int N = 55;

int t, n, r, a[N];

struct mat {
    int v[N][N];
    mat() {memset(v, 0, sizeof(v));}
    mat operator * (mat c) {
	mat ans;
	for (int i = 0; i < n; i++) {
	    for (int j = 0; j < n; j++) {
		for (int k = 0; k < n; k++)
		    ans.v[i][j] = (ans.v[i][j] + v[i][k] * c.v[k][j]) % 1000;
	    }
	}
	return ans;
    }
};

mat pow_mod(mat x, int k) {
    mat ans;
    for (int i = 0; i < n; i++) ans.v[i][i] = 1;
    while (k) {
	if (k&1) ans = ans * x;
	x = x * x;
	k >>= 1;
    }
    return ans;
}

int main() {
    scanf("%d", &t);
    while (t--) {
	scanf("%d%d", &n, &r);
	for (int i = 0; i < n; i++) scanf("%d", &a[i]);
	int x; mat Mat;
	for (int i = 0; i < n; i++) {
	    scanf("%d", &x);
	    int b;
	    while (x--) {
		scanf("%d", &b);
		Mat.v[i][b] = 1;
	    }
	}
	Mat = pow_mod(Mat, r);
	for (int i = 0; i < n; i++) {
	    int ans = 0;
	    for (int j = 0; j < n; j++) {
		ans = (ans + a[j] * Mat.v[i][j]) % 1000;
	    }
	    printf("%d%c", ans, (i == n - 1 ? '\n' : ' '));
	}
    }
    return 0;
}
时间: 2024-08-26 01:26:44

UVA 11551 - Experienced Endeavour(矩阵高速幂)的相关文章

Uva 11551 - Experienced Endeavour ( 矩阵快速幂 )

Uva 11551 - Experienced Endeavour ( 矩阵快速幂 ) #include <cstdio> #include <cstring> #include <algorithm> using namespace std; #define MAX_SIZE 50 #define MOD 1000 #define CLR( a, b ) memset( a, b, sizeof(a) ) struct Mat { int n; int mat[MAX

UVA 11551 Experienced Endeavour

矩阵快速幂. 题意事实上已经告诉我们这是一个矩阵乘法的运算过程. 构造矩阵:把xi列的bij都标为1. 例如样例二: #include<cstdio> #include<cstring> #include<cmath> #include<vector> #include<algorithm> using namespace std; long long const MOD = 1000; int n, m; long long a[50 + 5]

UVA 11551 - Experienced Endeavour(构造矩阵-水题)

题意:求一列序列的经过r次变化后的新序列,这些变化都是旧序列的某些已给位置的和产生新的项 思路:好水,直接构造01矩阵 //Accepted 45 ms C++ 4.8.2 1442 #include<cstdio> #include<iostream> #include<cstring> #include<algorithm> using namespace std; const int mod= 1000; int num[55]; int res[55

HDU 1588 Gauss Fibonacci(矩阵高速幂+二分等比序列求和)

HDU 1588 Gauss Fibonacci(矩阵高速幂+二分等比序列求和) ACM 题目地址:HDU 1588 Gauss Fibonacci 题意: g(i)=k*i+b;i为变量. 给出k,b,n,M,问( f(g(0)) + f(g(1)) + ... + f(g(n)) ) % M的值. 分析: 把斐波那契的矩阵带进去,会发现这个是个等比序列. 推倒: S(g(i)) = F(b) + F(b+k) + F(b+2k) + .... + F(b+nk) // 设 A = {1,1,

UVA 10655 - Contemplation! Algebra(矩阵快速幂)

UVA 10655 - Contemplation! Algebra 题目链接 题意:给定p, q, n代表p=a+b,q=ab求an+bn 思路:矩阵快速幂,公式变换一下得到(an+bn)(a+b)=an+1+bn+1+ab(an?1+bn?1),移项一下得到an+1+bn+1=(an+bn)p?q(an?1+bn?1) 这样就可以用矩阵快速幂求解了 代码: #include <stdio.h> #include <string.h> long long p, q, n; str

HDU3117-Fibonacci Numbers(矩阵高速幂+log)

题目链接 题意:斐波那契数列,当长度大于8时.要输出前四位和后四位 思路:后四位非常easy,矩阵高速幂取模,难度在于前四位的求解. 已知斐波那契数列的通项公式:f(n) = (1 / sqrt(5)) * (((1 + sqrt(5)) / 2) ^ n - ((1 + sqrt(5)) / 2) ^ n).当n >= 40时((1 + sqrt(5)) / 2) ^ n近似为0. 所以我们如果f(n) = t * 10 ^ k(t为小数),所以当两边同一时候取对数时.log10(t * 10

HDOJ 4686 Arc of Dream 矩阵高速幂

矩阵高速幂: 依据关系够建矩阵 , 高速幂解决. Arc of Dream Time Limit: 2000/2000 MS (Java/Others)    Memory Limit: 65535/65535 K (Java/Others) Total Submission(s): 2164    Accepted Submission(s): 680 Problem Description An Arc of Dream is a curve defined by following fun

HDU 4965 Fast Matrix Calculation(矩阵高速幂)

HDU 4965 Fast Matrix Calculation 题目链接 矩阵相乘为AxBxAxB...乘nn次.能够变成Ax(BxAxBxA...)xB,中间乘n n - 1次,这样中间的矩阵一个仅仅有6x6.就能够用矩阵高速幂搞了 代码: #include <cstdio> #include <cstring> const int N = 1005; const int M = 10; int n, m; int A[N][M], B[M][N], C[M][M], CC[N

hdu 5318 The Goddess Of The Moon 矩阵高速幂

链接:http://acm.hdu.edu.cn/showproblem.php?pid=5318 The Goddess Of The Moon Time Limit: 6000/3000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others) Total Submission(s): 438    Accepted Submission(s): 150 Problem Description Chang'e (嫦娥) is