跳到正文

题解

UVA11551

_luanyi_约 3 分钟
原文
文章信息与版本

文章信息

发布
2022年7月29日
收录
2026年4月24日
更新
2026年11月2日
篇幅
约 3 分钟
题目
UVA11551

版本

  1. v2当前
  2. v1

题目大意

给定 nn 和 rr,接下来给定一个长度为 nn 的序列 aa。你需要对这个序列进行变换。 接下来 nn 行第 ii 行第一个数为 xx ,该行接下来 xx 个数 di,jd_{i,j} 表示 ai′=∑j=1xadi,ja'_i=\sum\limits_{j=1}^xa_{d_{i,j}}。 同样的变换要重复 rr 次,要求你输出变换后的序列,答案对 10001000 取模(神奇的模数)。 多测,测 tt 组。 1⩽t⩽10,1⩽n⩽50,1⩽r⩽1091 \leqslant t \leqslant 10, 1\leqslant n \leqslant 50, 1 \leqslant r \leqslant 10^9。

正文

很难说这不是矩阵快速幂。 设变换矩阵为 AA,本题的答案就是 aAraA^r。 其中变换矩阵 AA 表达为

Ai,j={0,∃ k,di,k=j1,otherwiseA_{i,j}= \begin{cases} 0, &\exists\ k,d_{i,k}=j\\ 1, &\text{otherwise} \end{cases}

直接做就好了,个人认为实际难度没有黑。

注意

由于 UVA 的神奇的特性,行末不要有多余的空格,会爆 PE,洛谷会 WA。

code

小心多测怪

C++
#include <bits/stdc++.h>
#define fq(i,a,b) for (int i = (a); i <= (b); i++)
using namespace std;

inline int rd () {
	int f = 1;
	char ch = getchar ();
	while (!isdigit (ch)) (ch == '-' ? (f = -1) : 0), ch = getchar ();
	int num = 0;
	while (isdigit (ch)) num = num * 10 + ch - '0', ch = getchar ();
	return num * f;
}
#define d rd ()


const int maxn = 55;
const int p = 1000;
int A[maxn][maxn], B[maxn][maxn], C[maxn][maxn];
int n, r, a[maxn];
void mul () {
	memset (C, 0, sizeof C);
	fq (i, 1, n) fq (j, 1, n) fq (k, 1, n) C[i][j] = (C[i][j] + A[i][k] * B[k][j]) % p;
	memcpy (B, C, sizeof B);
}
void mult () {
	memset (C, 0, sizeof C);
	fq (i, 1, n) fq (j, 1, n) fq (k, 1, n) C[i][j] = (C[i][j] + A[i][k] * A[k][j]) % p;
	memcpy (A, C, sizeof A);
}
signed main () {
	int t = d;
	while (t--) {
		n = d, r = d;
		memset (A, 0, sizeof A);
		memset (B, 0, sizeof B);
		fq (i, 1, n) a[i] = d % p;
		fq (i, 1, n) {int x = d; while (x--) A[i][d + 1] = 1;}
		fq (i, 1, n) B[i][i] = 1;
		while (r) {
			if (r & 1) mul ();
			mult ();
			r >>= 1;
		}
		fq (i, 1, n) {
			int ans = 0;
			fq (j, 1, n) ans = (ans + a[j] * B[i][j]) % p;
			cout << ans << " \n"[i == n];
		}
	}
	return 0;
}
原文
存档评论2 条抓取于