QOJ.ac

QOJ

ID题目提交者结果用时内存语言文件大小提交时间测评时间
#688748#7014. Rikka with Grid GraphsOIer_kzcAC ✓5711ms37364kbC++174.8kb2024-10-30 13:19:392024-10-30 13:19:41

Judging History

This is the latest submission verdict.

  • [2024-10-30 13:19:41]
  • Judged
  • Verdict: AC
  • Time: 5711ms
  • Memory: 37364kb
  • [2024-10-30 13:19:39]
  • Submitted

answer

#include <stdio.h>
#include <string.h>
#include <assert.h>

#include <vector>
#include <algorithm>
#include <unordered_map>
#include <map>

#define LOG(FMT...) fprintf(stderr, FMT)

#define eb emplace_back

using namespace std;

typedef unsigned long long ULL;
constexpr int N = 16;
constexpr int NS = 262144;

char buf[1 << 21], *p1 = buf, *p2 = buf;
char gc() {
	return (p1 == p2 && (p2 = (p1 = buf) + fread(buf, 1, 1 << 21, stdin), p1 == p2) ? -1 : *p1++);
}
int read() {
	int x = 0; char c = gc();
	while (c < 48 || c > 57) c = gc();
	for (; c > 47 && c < 58; c = gc()) x = x * 10 + (c ^ 48);
	return x;
}
void readstr(char *s) {
	char c = gc();
	while (c != '\n' && c != -1) {
		*s++ = c;
		c = gc();
	}
}

int n, m;
char gr[N][N];

constexpr int MS = 1000007;

struct HT {
	ULL hsv[MS];
	int id[MS];
	
	ULL f[NS], st[NS];
	int ids;
	void reset() {
		memset(f, 0, ids << 3);
		memset(hsv, 0, sizeof hsv);
		ids = 0;
	}
	int get(ULL s) {
		int x = s % MS;
		while (hsv[x] && hsv[x] != s + 1) {
			if ((x += 1) == MS) {
				x = 0;
			}
		}
		if (hsv[x]) {
			return id[x];
		}
		hsv[x] = s + 1;
		id[x] = ids;
		st[ids] = s;
		++ids;
		return id[x];
	}
	void upd(ULL state, ULL v) {
		f[get(state)] += v;
	}
} last, cur;

void reset(ULL &x, int k) {
	if (x >> k & 1) {
		x ^= 1ull << k;
	}
}

void trans(int y, char fL, char fU, ULL s, ULL vf) {
	ULL emp = s, ns;
	for (int z = 0; z < m; ++z) {
		reset(emp, z * m + y);
		reset(emp, y * m + z);
	}
	if (!fL && !fU) {
		cur.upd(emp, vf);
		return;
	}
	if (!fL) {
		ns = emp;
		for (int z = 0; z < m; ++z) {
			if (s >> z * m + y & 1) {
				ns |= 1ull << z * m + y;
			}
		}
		cur.upd(ns, vf);
		ns = emp;
		for (int z = 0; z < m; ++z) {
			if (s >> y * m + z & 1) {
				ns |= 1ull << y * m + z;
			}
		}
		cur.upd(ns, vf);
		return;
	}
	if (!fU) {
		ns = emp | 1ull << (y - 1) * m + y;
		for (int z = 0; z < m; ++z) {
			if (s >> z * m + y - 1 & 1) {
				ns |= 1ull << z * m + y;
			}
		}
		cur.upd(ns, vf);
		ns = emp | 1ull << y * m + y - 1;
		for (int z = 0; z < m; ++z) {
			if (s >> (y - 1) * m + z & 1) {
				ns |= 1ull << y * m + z;
			}
		}
		cur.upd(ns, vf);
		return;
	}
	ns = emp | 1ull << (y - 1) * m + y;
	for (int z = 0; z < m; ++z) {
		if ((s >> z * m + y - 1 & 1) || (s >> z * m + y & 1)) {
			ns |= 1ull << z * m + y;
		}
	}
	cur.upd(ns, vf);
	ns = emp | 1ull << y * m + y - 1;
	for (int z = 0; z < m; ++z) {
		if ((s >> (y - 1) * m + z & 1) || (s >> y * m + z & 1)) {
			ns |= 1ull << y * m + z;
		}
	}
	cur.upd(ns, vf);
	bool cycle;
	static vector<int> S, E;
	// left down
	S.clear(), E.clear();
	ns = emp | 1ull << y * m + y - 1;
	S.eb(y), E.eb(y - 1);
	for (int z = 0; z < m; ++z) {
		if (s >> z * m + y & 1) {
			S.eb(z);
			ns |= 1ull << z * m + y;
		}
	}
	for (int z = 0; z < m; ++z) {
		if (s >> (y - 1) * m + z & 1) {
			E.eb(z);
			ns |= 1ull << y * m + z;
		}
	}
	cycle = false;
	for (int x : S) if (!cycle) {
		for (int y : E) if (!cycle) {
			if (s >> y * m + x & 1) {
				cycle = true;
			}
			ns |= 1ull << x * m + y;
		}
	}
	if (!cycle) {
		cur.upd(ns, vf);
	}
	// right up
	S.clear(), E.clear();
	S.eb(y - 1), E.eb(y);
	ns = emp | 1ull << (y - 1) * m + y;
	for (int z = 0; z < m; ++z) {
		if (s >> z * m + y - 1 & 1) {
			S.eb(z);
			ns |= 1ull << z * m + y;
		}
	}
	for (int z = 0; z < m; ++z) {
		if (s >> y * m + z & 1) {
			E.eb(z);
			ns |= 1ull << y * m + z;
		}
	}
	cycle = false;
	for (int x : S) if (!cycle) {
		for (int y : E) if (!cycle) {
			if (s >> y * m + x & 1) {
				cycle = true;
			}
			ns |= 1ull << x * m + y;
		}
	}
	if (!cycle) {
		cur.upd(ns, vf);
	}
}

void solve() {
	last.reset(), cur.reset();
	int k = last.get(0ull);
	last.st[k] = 0ull, last.f[k] = 1ull;
	for (int x = 0; x < n; ++x) {
		for (int y = 0; y < m; ++y) {
			for (int i = 0; i < last.ids; ++i) {
				trans(y, gr[2 * x + 1][2 * y], gr[2 * x][2 * y + 1], last.st[i], last.f[i]);
			}
			/* LOG("%d\n", last.ids);
			for (int i = 0; i < last.ids; ++i) {
				for (int x = 0; x < m; ++x) {
					for (int y = 0; y < m; ++y) {
						LOG("%c", ".#"[last.st[i] >> x * m + y & 1]);
					}
					LOG("\n");
				}
				LOG("%llu\n\n", last.f[i]);
			} */
			last = cur, cur.reset();
		}
	}
	ULL res = 0;
	for (int i = 0; i < last.ids; ++i) {
		res += last.f[i];
	}
	printf("%llu\n", res);
	last.reset();
}

int main() {
	int task;
	for (task = read(); task--; ) {
		n = read(), m = read();
		memset(gr, 0, sizeof gr);
		for (int i = 1; i < 2 * n; ++i) {
			readstr(gr[i] + 1);
			for (int j = 1; j < 2 * m; ++j) {
				gr[i][j] = (gr[i][j] == '|' || gr[i][j] == '-');
			}
		}
		/* for (int i = 1; i < 2 * n; ++i) {
			for (int j = 1; j < 2 * m; ++j) {
				printf("%d ", gr[i][j]);
			}
			puts("");
		} */
		solve();
	}
	return 0;
}

这程序好像有点Bug,我给组数据试试?

详细

Test #1:

score: 100
Accepted
time: 28ms
memory: 34592kb

input:

4
2 2
+-+

+ +
2 2
+-+
|
+ +
2 2
+-+
|
+-+
2 2
+-+
| |
+-+

output:

2
4
8
14

result:

ok 4 lines

Test #2:

score: 0
Accepted
time: 5711ms
memory: 37364kb

input:

60
1 1
+
6 6
+-+-+-+-+-+
| | | | | |
+-+-+-+-+-+
| | | | | |
+-+-+-+-+-+
| | | | | |
+-+-+-+-+-+
| | | | | |
+-+-+-+-+-+
| | | | | |
+-+-+-+-+-+
6 4
+ + + +

+ + + +

+ + + +

+ + + +

+ + + +

+ + + +
4 5
+-+-+-+ +
|       |
+-+-+-+ +
  |     |
+-+ +-+ +
| |
+ + +-+ +
5 6
+ + +-+ + +
| | |   | |
+-...

output:

1
39931856138212664
1
32768
2097152
396928
1912140726272
17072685056
33681342464
1070508371744
4877910016
470169766912
181121759168
814322432
891691528874048
358663151840
1905966526844408
851738327552
191458384162304
288115586432
1368156669845336
356038587648896
3776487925133468
691176008890304
1090...

result:

ok 60 lines