跳到正文

题解

题解:CF1552D Array Differentiation

Hsl_Beat约 2 分钟
原文
文章信息与版本

文章信息

收录
2026年1月24日
更新
2026年1月24日
篇幅
约 2 分钟
题目
CF1552D

版本

  1. v1当前

首先注意到 nn 小的吓人。

考虑一个有 nn 个结点的图,对于每个 Ai=bj−bkA_i = b_j - b_k,我们连一条 j→kj \rightarrow k 的边。这么说图上就会有 nn 条边了,那么不考虑边的方向,图上就肯定有一个环啦。

我们对于这个环上的每条边,都对应了 AA 中的一个元素,那么假设考虑上边的方向后所有的边能让你绕一圈回到起点,那么依次把 aia_i 加和一定为 00。为什么呢,原因是因为式子是 (bv1−bv2)+(bv2−bv3)+⋯+(bvk−bv1)=0(b_{v_1} - b_{v_2}) + (b_{v_2} - b_{v_3}) + \dots + (b_{v_k} - b_{v_1}) = 0,这里 vv 表示环上依次的元素。

要是环上有反向的边,其实我们只需要把反向边对应的 AiA_i 乘上 −1-1 之后还是满足这些元素的和为 00 的条件的。

所以这题就简单了,直接枚举 AA 中哪些元素在环里,并给他们确定一个系数,要是有任意一种情况和为 00 那么就有解。

代码瞎写的,最好别看

C++
#include<bits/stdc++.h>
using namespace std;
#define int long long
int fastpow(int a, int b, int mod)
{
	int res = 1;
	while (b) {
		if (b & 1) {
			res = (res * a) % mod;
		}
		a = (a * a) % mod;
		b >>= 1;
	}
	return res;
}
void solve()
{
	int n;
	cin >> n;
	vector<int> a(n);
	for (int i = 0; i < n; i++) {
		cin >> a[i];
	}
	int tp = fastpow(3, n, 1145141919810);
	for (int i = 1; i < tp; i++) {
		int cnt = 0;
		int ttp = i;
		for (int j = 0; j < n; j++) {
			cnt += (ttp % 3 == 2 ? -1 : ttp % 3) * a[j];
			ttp /= 3;
		}
		if (cnt == 0) {
			cout << "YES" << '\n';
			return;
		}
	}
	cout << "NO" << '\n';
}
signed main()
{
	int T;
	cin >> T;
	while (T--) {
		solve();
	}
	return 0;
}
原文