跳到正文

题解

P7606 [THUPC2021] 混乱邪恶 - Solution

strcmp约 4 分钟
原文
文章信息与版本

文章信息

发布
2024年2月27日
收录
2026年7月2日
更新
2026年11月16日
篇幅
约 4 分钟
获赞
4
题目
P7606

版本

  1. v1当前

做一个坐标变换。具体的,将原空间映射到平面直角坐标系上,六个方向分别代表:

  • (x, y)→(x+1, y)(x,\,y) \to (x + 1,\,y),(x, y)→(x, y+1)(x,\,y) \to (x,\,y + 1),(x, y)→(x+1, y+1)(x,\,y) \to (x + 1,\,y + 1)。反向同理。

具体构造你可以想象钦定网格中的一个平行四边形,将它“旋转”为一个矩形。

显然的设 fi, l, g, x, yf_{i,\,l,\,g,\,x,\,y} 为用完了前 ii 个 idea,在 (x, y)(x,\,y),当前的 L=l, G=gL = l,\,G = g 这种情况是否可能存在。

枚举六个方向转移即可。

注意到这是一个只有 0/1 状态的背包,显然可以 bitset 优化掉最后一维,同时空间上滚动数组。空间复杂度 Θ(n2p2w)\Theta(\frac{n^2p^2}{w}),可以接受。

时间复杂度 Θ(n3p2w)\Theta(\frac{n^3p^2}{w}),注意到运算量大概有三亿级别,而时限只有 700ms700\text{ms},显然很难通过。

idea 加入的顺序并不影响我们的答案,因此可以考虑将 idea 打乱,在二维平面上随机走 nn 步期望与源点距离是 n\sqrt n 级别的,于是在背包里后面两维只枚举到 2n2 \sqrt n 就行了。(当然注意到你的 bitset 能轻松的用 long long 存下,因此可以直接用 long long,但是实际上效率几乎一样,这里假设压成一个二进制数)

时间复杂度 Θ(p2nn)\Theta(p^2n \sqrt n),可以通过。

C++
#include <bits/stdc++.h>
#define X first
#define Y second
using namespace std;
typedef long long int ll;
using pll = pair<ll, ll>;
using uint = unsigned int;
const int maxn = 1e6 + 10;
constexpr ll mod = 998244353;
const ll inf = 114514191981052013LL;
mt19937 rd(mod); 
ll a[maxn][13], L, G; int n, p;
bitset<40> f[2][105][105][40];
inline int r(int x, int y) { return x >= y ? x - y : x - y + p; }
int main() {
	scanf("%d%d", &n, &p);
    int s = ceil(sqrt(n)) + 5;
	for (int i = 1; i <= n; i++) {
		for (int j = 1; j <= 12; j++) scanf("%lld", &a[i][j]);
	}
	scanf("%lld%lld", &L, &G);
	shuffle(a + 1, a + n + 1, rd); int c = 0;
	f[0][0][0][s][s] = 1;
	for (int i = 1; i <= n; i++) {
		for (int l = 0; l < p; l++) {
			for (int g = 0; g < p; g++) {
				for (int x = 0; x <= (s << 1); x++) {
					bitset<40>& d = f[c ^ 1][l][g][x]; d.reset();
					d |= f[c][r(l, a[i][1])][r(g, a[i][2])][x] << 1;
					d |= f[c][r(l, a[i][3])][r(g, a[i][4])][x + 1];
					d |= f[c][r(l, a[i][5])][r(g, a[i][6])][x + 1] >> 1;
					d |= f[c][r(l, a[i][7])][r(g, a[i][8])][x] >> 1;
					if (x >= 1) d |= f[c][r(l, a[i][9])][r(g, a[i][10])][x - 1];
					if (x >= 1) d |= f[c][r(l, a[i][11])][r(g, a[i][12])][x - 1] << 1;
				}
			}
		}
		c ^= 1;
	}
	puts(f[c][L][G][s][s] ? "Chaotic Evil" : "Not a true problem setter");
	return 0;
}
原文