Submission #987599

# Submission time Handle Problem Language Result Execution time Memory
987599 2024-05-23T06:52:59 Z HienTD Ancient Machine 2 (JOI23_ancient2) C++17
100 / 100
72 ms 1984 KB
#include "ancient2.h"
#include <bits/stdc++.h>

#define MAX_N 1000
#define M_UPPERBOUND 102

using bitvec = std::bitset<MAX_N>;

struct basis_manager {
	std::vector<std::pair<int, bitvec> > basis;
	bool add(bitvec v) {
		for (auto i : basis) if (v[i.first]) v ^= i.second;
		if (v.any()) {
			basis.push_back({v._Find_first(), v});
			return true;
		}
		return false;
	}
};

// {mod, remainder, actual 01 vector}
// {-1, i,          actual 01 vector} : i-th
// {-2, i,          actual 01 vector} : i-th from the back
std::vector<std::tuple<int, int, bitvec> > list_needed_vecs(int n) {
	std::vector<std::vector<std::tuple<int, int, bitvec> > > cands(M_UPPERBOUND + 1);
	// periodic
	for (int mod = 1; mod <= M_UPPERBOUND / 2; mod++) {
		for (int rem = 0; rem < mod; rem++) {
			bitvec x;
			for (int i = rem; i < n; i += mod) x[i] = 1;
			cands[2 * mod].push_back(std::make_tuple(mod, rem, x));
		}
	}
	// prefix
	for (int i = 0; i + 3 <= M_UPPERBOUND; i++) {
		bitvec x;
		x[i] = 1;
		cands[i + 3].push_back(std::make_tuple(-1, i, x));
	}
	// suffix
	for (int i = 0; i + 2 <= M_UPPERBOUND; i++) {
		bitvec x;
		x[n - 1 - i] = 1;
		cands[i + 2].push_back({-2, i, x});
	}
	basis_manager basis;
	std::vector<std::tuple<int, int, bitvec> > res;
	for (int i = 0; i <= M_UPPERBOUND; i++) {
		for (auto j : cands[i]) {
			if (basis.add(std::get<2>(j))) res.push_back(j);
			if ((int) basis.basis.size() == n) break;
		}
		if ((int) basis.basis.size() == n) break;
	}
	return res;
}
std::vector<bool> solve_linear_equation(std::vector<bitvec> &a, std::vector<bool> &b) {
	int n = a.size();
	for (int i = 0; i < n; i++) {
		int id = -1;
		for (int j = i; j < n; j++) if (a[j][i]) { id = j; break; }
		assert(id != -1);
		std::swap(a[i], a[id]);
		swap(b[i], b[id]);
		for (int j = 0; j < n; j++) if (j != i && a[j][i]) a[j] ^= a[i], b[j] = b[j] ^ b[i];
	}
	return b;
}


std::string Solve(int n) {
	auto list = list_needed_vecs(n);
	
	std::vector<bitvec> mat;
	std::vector<bool> res;
	std::vector<bool> back_res;
	for (auto i : list) {
		int mod = std::get<0>(i);
		int rem = std::get<1>(i);
		auto query_mod = [&] () {
			int m = mod * 2;
			std::vector<int> a(m), b(m);
			for (int j = 0; j < mod; j++) {
				int next = (j + 1) % mod;
				a[j] = b[j] = next;
				a[j + mod] = b[j + mod] = next + mod;
				if (j == rem) std::swap(b[j], b[j + mod]);
			}
			return Query(m, a, b) >= mod;
		};
		auto query_forward = [&] () {
			int m = rem + 3;
			std::vector<int> a(m), b(m);
			for (int j = 0; j < rem; j++) a[j] = b[j] = j + 1;
			a[rem] = rem + 1;
			b[rem] = rem + 2;
			a[rem + 1] = b[rem + 1] = rem + 1;
			a[rem + 2] = b[rem + 2] = rem + 2;
			return Query(m, a, b) == rem + 2;
		};
		auto query_back = [&] () {
			assert(rem == (int) back_res.size());
			back_res.insert(back_res.begin(), 0);
			int m = rem + 2;
			std::vector<int> a(m), b(m);
			for (int i = 0; i <= rem + 1; i++) {
				auto get = [&] (bool next) {
					auto cur = std::vector<bool>(back_res.begin(), back_res.begin() + i);
					cur.push_back(next);
					for (int j = std::min(rem + 1, i + 1); j; j--)
						if (std::vector<bool>(cur.end() - j, cur.end()) ==
							std::vector<bool>(back_res.begin(), back_res.begin() + j)) return j;
					return 0;
				};
				a[i] = get(0);
				b[i] = get(1);
			}
			bool res = Query(m, a, b) != rem + 1;
			back_res[0] = res;
			return res;
		};
		mat.push_back(std::get<2>(i));
		res.push_back(mod >= 1 ? query_mod() : mod == -1 ? query_forward() : query_back());
	}
	res = solve_linear_equation(mat, res);
	
	std::string res_str;
	for (auto i : res) res_str.push_back('0' + i);
	
	return res_str;
}
# Verdict Execution time Memory Grader output
1 Correct 59 ms 1984 KB Output is correct
2 Correct 51 ms 1696 KB Output is correct
3 Correct 50 ms 1460 KB Output is correct
4 Correct 55 ms 968 KB Output is correct
5 Correct 60 ms 1220 KB Output is correct
6 Correct 61 ms 1456 KB Output is correct
7 Correct 50 ms 1468 KB Output is correct
8 Correct 54 ms 1220 KB Output is correct
9 Correct 57 ms 1216 KB Output is correct
10 Correct 66 ms 1224 KB Output is correct
11 Correct 54 ms 1180 KB Output is correct
12 Correct 64 ms 1220 KB Output is correct
13 Correct 59 ms 1436 KB Output is correct
14 Correct 58 ms 1208 KB Output is correct
15 Correct 65 ms 1952 KB Output is correct
16 Correct 58 ms 1460 KB Output is correct
17 Correct 57 ms 964 KB Output is correct
18 Correct 58 ms 1444 KB Output is correct
19 Correct 63 ms 1708 KB Output is correct
20 Correct 63 ms 960 KB Output is correct
21 Correct 51 ms 1480 KB Output is correct
22 Correct 61 ms 1456 KB Output is correct
23 Correct 53 ms 1208 KB Output is correct
24 Correct 72 ms 1468 KB Output is correct
25 Correct 56 ms 1192 KB Output is correct
26 Correct 60 ms 1444 KB Output is correct
27 Correct 64 ms 1452 KB Output is correct
28 Correct 58 ms 1452 KB Output is correct
29 Correct 57 ms 1208 KB Output is correct
30 Correct 52 ms 956 KB Output is correct
31 Correct 71 ms 1200 KB Output is correct
32 Correct 58 ms 1448 KB Output is correct
33 Correct 69 ms 1456 KB Output is correct
34 Correct 53 ms 1448 KB Output is correct
35 Correct 53 ms 1180 KB Output is correct
36 Correct 60 ms 1212 KB Output is correct
37 Correct 56 ms 1204 KB Output is correct
38 Correct 58 ms 1448 KB Output is correct
39 Correct 61 ms 1436 KB Output is correct
40 Correct 61 ms 1460 KB Output is correct
41 Correct 61 ms 1452 KB Output is correct
42 Correct 59 ms 1464 KB Output is correct
43 Correct 63 ms 1180 KB Output is correct
44 Correct 59 ms 1440 KB Output is correct
45 Correct 57 ms 968 KB Output is correct
46 Correct 59 ms 1464 KB Output is correct
47 Correct 68 ms 1436 KB Output is correct
48 Correct 71 ms 964 KB Output is correct
49 Correct 58 ms 1204 KB Output is correct
50 Correct 56 ms 1196 KB Output is correct
51 Correct 63 ms 1208 KB Output is correct
52 Correct 61 ms 1192 KB Output is correct
53 Correct 63 ms 1196 KB Output is correct
54 Correct 65 ms 1200 KB Output is correct
55 Correct 66 ms 1220 KB Output is correct
56 Correct 61 ms 1180 KB Output is correct
57 Correct 58 ms 1448 KB Output is correct
58 Correct 53 ms 1212 KB Output is correct
59 Correct 61 ms 1216 KB Output is correct
60 Correct 57 ms 1192 KB Output is correct
61 Correct 57 ms 1444 KB Output is correct
62 Correct 58 ms 1736 KB Output is correct
63 Correct 58 ms 964 KB Output is correct