QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#232589#7635. Fairy Chessmendicillin2#WA 31ms21796kbC++173.5kb2023-10-30 17:06:592023-10-30 17:06:59

Judging History

你现在查看的是最新测评结果

  • [2023-10-30 17:06:59]
  • 评测
  • 测评结果:WA
  • 用时:31ms
  • 内存:21796kb
  • [2023-10-30 17:06:59]
  • 提交

answer

#pragma GCC optimize ("Ofast")
#include <bits/extc++.h>
using namespace std;

using ll = int64_t;
using ull = uint64_t;

mt19937_64 mt(chrono::steady_clock::now().time_since_epoch().count());

namespace hash_map_impl {

struct custom_hash {
	static const uint64_t c = ll(4e18 * acos(0)) + 71;
	const ll z = mt();
	ll operator ()(ll x) const { return __builtin_bswap64((x ^ z) * c); }
};

template <class K, class V>
using hash_map = __gnu_pbds::gp_hash_table<K, V, custom_hash>;

} // namespace hash_map_impl

namespace hashing_impl {

struct H {
	ull x;
	H(ull x_ = 0) : x(x_) {}
#define OP(O,A,B) H operator O(H o) { ull r = x; asm \
	(A "addq %%rdx, %0\n adcq $0,%0" : "+a"(r) : B); return r; }
	OP(+,,"d"(o.x)) OP(*,"mul %1\n", "r"(o.x) : "rdx")
	H operator - (H o) { return *this + H(~o.x); }
	explicit operator ull() const { return x + !~x; }
	bool operator == (H o) const { return ull(*this) == ull(o); }
	bool operator < (H o) const { return ull(*this) < ull(o); }
};

H rand_base() {
	return H(2 * uniform_int_distribution<ll>(4e10, 5e10)(mt) + 1);
}

} // namespace hashing_impl

int get_idx(int x, int y) {
	return 8 * x + y;
}

bool can_attack(char type, int x, int y, int ox, int oy) {
	if (x == ox && y == oy) return true;
	if (type == 'B') {
		return x+y == ox+oy || x-y == ox-oy;
	} else if (type == 'R') {
		return x == ox || y == oy;
	} else if (type == 'Q') {
		return can_attack('B', x, y, ox, oy)
			|| can_attack('R', x, y, ox, oy);
	} else if (type == 'K') {
		return (x - ox) * (x - ox) + (y - oy) * (y - oy) == 5;
	} else if (type == 'C') {
		return can_attack('R', x, y, ox, oy)
			|| can_attack('K', x, y, ox, oy);
	} else if (type == 'M') {
		return can_attack('Q', x, y, ox, oy)
			|| can_attack('K', x, y, ox, oy);
	} else if (type == 'A') {
		return can_attack('B', x, y, ox, oy)
			|| can_attack('K', x, y, ox, oy);
	} assert(false);
}

bool works_pair(char t, int x, int y, char ot, int ox, int oy) {
	if (x == ox && y == oy) return false;
	return !(can_attack(t, x, y, ox, oy)
			 || can_attack(ot, ox, oy, x, y));
}

constexpr int K = 6;
const string types = "BRQACM";
ull occ[K][64];

void precomp() {
	assert(int(types.size()) == K);
	for (int k = 0; k < K; k++) {
		char t = types[k];
		for (int x = 0; x < 8; x++) {
			for (int y = 0; y < 8; y++) {
				for (int ox = 0; ox < 8; ox++) {
					for (int oy = 0; oy < 8; oy++) {
						if (can_attack(t, x, y, ox, oy)) {
							occ[k][get_idx(x, y)] |= ull(1) << get_idx(ox, oy);
						}
					}
				}
			}
		}
	}
}

int main() {
	ios_base::sync_with_stdio(false), cin.tie(nullptr);

	precomp();

	string A_; cin >> A_;
	constexpr int L = 12;
	assert(int(A_.size()) == L);

	array<int, L> A;
	for (int i = 0; i < L; i++) {
		int k = types.find(A_[i]);
		assert(0 <= k && k < 6);
		A[i] = k;
	}

	using hashing_impl::H;
	const H base = hashing_impl::rand_base();

	using T = ull;
	using hash_map_impl::hash_map;
	hash_map<T, bool> mem;

	auto encode = [&](ull a, ull b) -> ull {
		return ull(H(a) * base + b);
	};

	auto wins = [&](auto self, int cur, ull used, ull banned) -> bool {
		if (cur == L) return false;
		auto it = mem.find(encode(used, banned));
		if (it == mem.end()) {
			bool res = [&]() -> bool {
				int k = A[cur];
				ull free = ~banned;
				while (free > 0) {
					int g = __builtin_ctzll(free);
					if (!(occ[k][g] & used) && !self(self, cur+1, used | (ull(1) << g), banned | occ[k][g])) {
						return true;
					}
					free -= ull(1) << g;
				}
				return false;
			}();
			it = mem.insert({used, res}).first;
		}
		return it->second;
	};
	cout << (wins(wins, 0, 0, 0) ? "Alice" : "Bob") << '\n';

	return 0;
}

Details

Tip: Click on the bar to expand more detailed information

Test #1:

score: 100
Accepted
time: 31ms
memory: 21796kb

input:

BBAARRCCQQMM

output:

Bob

result:

ok single line: 'Bob'

Test #2:

score: 0
Accepted
time: 0ms
memory: 4552kb

input:

BAMBAMQQRCCR

output:

Alice

result:

ok single line: 'Alice'

Test #3:

score: -100
Wrong Answer
time: 16ms
memory: 12448kb

input:

QQRAACMRMCBB

output:

Bob

result:

wrong answer 1st lines differ - expected: 'Alice', found: 'Bob'