跳到正文

题解

ARC190D Matrix Pow Sum

EuphoricStar约 6 分钟
原文
文章信息与版本

文章信息

收录
2025年12月12日
更新
2025年12月12日
篇幅
约 6 分钟
获赞
6

版本

  1. v1当前

考虑 BpB^p 的第 (i,j)(i, j) 位的所有可能的和是什么。把它看成一个图论问题,i→ji \to j 的边权是 Bi,jB_{i, j},那么相当于求所有经过 pp 条边且起点为 ii,终点为 jj 的路径的边权乘积的和。对于一条 Ai,j=0A_{i, j} = 0 的边,设一条路径经过它的次数为 ci,jc_{i, j},那么一条路径的贡献即为 ∏i,j(∑k=1p−1kci,j)\prod\limits_{i, j} (\sum\limits_{k = 1}^{p - 1} k^{c_{i, j}})。

先特判 p=2p = 2。发现对于 c∈[0,p]c \in [0, p],只有 c=0c = 0 或 p−1p - 1 时 ∑k=1p−1kc\sum\limits_{k = 1}^{p - 1} k^c 在模 pp 意义下非 00(此时值为 p−1p - 1),对于其余的 cc,∑k=1p−1kc\sum\limits_{k = 1}^{p - 1} k^c 在模 pp 意义下都为 00。证明大概是取离散对数,发现当 c=0c = 0 或 p−1p - 1 时 ∀k∈[1,p−1],kc\forall k \in [1, p - 1], k^c 在模 pp 意义下都为 11,当 cc 为其余值时 kck^c 在模 pp 意义下取遍 [1,p−1][1, p - 1] 的所有值,而因为 p≥3p \ge 3 所以 p(p−1)2\frac{p(p - 1)}{2} 是 pp 的倍数。

这意味着对于每条 00 权边,只有经过它恰好 00 次或 p−1p - 1 次,整条路径的贡献才非 00,且设总共有 ll 条 00 权边,路径经过的非 00 权边的乘积为 xx,那么此时这条路径的贡献为 (p−1)lx(p - 1)^l x。考虑先算出 ApA^p 表示不考虑经过 00 权边时 i→ji \to j 的路径的和作为初始答案,那么只有当 i≠ji \ne j 时路径才可能经过每条 00 权边恰好 00 次或 p−1p - 1 次。然后分类讨论:

  • 当 Ai,i=0A_{i, i} = 0 且 Ai,j≠0A_{i, j} \ne 0 时,路径可能形如 i→i→⋯→i→ji \to i \to \cdots \to i \to j,贡献 (p−1)lAi,j(p - 1)^l A_{i, j}。
  • 当 Aj,j=0A_{j, j} = 0 且 Ai,j≠0A_{i, j} \ne 0 时,路径可能形如 i→j→j→⋯→ji \to j \to j \to \cdots \to j,贡献 (p−1)lAi,j(p - 1)^l A_{i, j}。
  • 特别地,当 p=3p = 3 且 Ai,j=0A_{i, j} = 0 且 Aj,i≠0A_{j, i} \ne 0,路径可能形如 i→j→i→ji \to j \to i \to j,贡献 (p−1)lAj,i(p - 1)^l A_{j, i}。

那么就做完了,瓶颈在算 ApA^p。时间复杂度 O(n3log⁡p)O(n^3 \log p)。

C++
// Problem: D - Matrix Pow Sum
// Contest: AtCoder - AtCoder Regular Contest 190 (Div. 1)
// URL: https://atcoder.jp/contests/arc190/tasks/arc190_d
// Memory Limit: 1024 MB
// Time Limit: 2000 ms
//
// Powered by CP Editor (https://cpeditor.org)

#include <bits/stdc++.h>
#define pb emplace_back
#define fst first
#define scd second
#define mkp make_pair
#define uint unsigned
#define mems(a, x) memset((a), (x), sizeof(a))

using namespace std;
typedef long long ll;
typedef double db;
typedef unsigned long long ull;
typedef long double ldb;
typedef pair<ll, ll> pii;

const int maxn = 110;

ll n, mod, a[maxn][maxn], b[maxn][maxn];

struct mat {
	ll a[maxn][maxn];
	mat() {
		mems(a, 0);
	}
} A, B;

inline mat operator * (const mat &a, const mat &b) {
	mat res;
	for (int k = 1; k <= n; ++k) {
		for (int i = 1; i <= n; ++i) {
			for (int j = 1; j <= n; ++j) {
				res.a[i][j] = (res.a[i][j] + a.a[i][k] * b.a[k][j]) % mod;
			}
		}
	}
	return res;
}

inline mat qpow(mat a, ll p) {
	mat res = a;
	--p;
	while (p) {
		if (p & 1) {
			res = res * a;
		}
		a = a * a;
		p >>= 1;
	}
	return res;
}

void solve() {
	scanf("%lld%lld", &n, &mod);
	int k = 0;
	for (int i = 1; i <= n; ++i) {
		for (int j = 1; j <= n; ++j) {
			scanf("%lld", &a[i][j]);
			if (!a[i][j]) {
				k ^= 1;
			}
			A.a[i][j] = a[i][j];
		}
	}
	if (mod == 2) {
		for (int i = 1; i <= n; ++i) {
			for (int j = 1; j <= n; ++j) {
				printf("%lld%c", n & 1, " \n"[j == n]);
			}
		}
		return;
	}
	A = qpow(A, mod);
	for (int i = 1; i <= n; ++i) {
		for (int j = 1; j <= n; ++j) {
			ll ans = A.a[i][j];
			if (i != j && !a[i][i] && a[i][j]) {
				ans = (ans + a[i][j]) % mod;
			}
			if (i != j && !a[j][j] && a[i][j]) {
				ans = (ans + a[i][j]) % mod;
			}
			if (mod == 3 && !a[i][j] && a[j][i]) {
				ans = (ans + a[j][i]) % mod;
			}
			if (k & 1) {
				ans = (mod - ans) % mod;
			}
			printf("%lld%c", ans, " \n"[j == n]);
		}
	}
}

int main() {
	int T = 1;
	// scanf("%d", &T);
	while (T--) {
		solve();
	}
	return 0;
}
原文