답안 #810336

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
810336 2023-08-06T08:37:35 Z model_code Ancient Machine 2 (JOI23_ancient2) C++17
100 / 100
87 ms 976 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;
}
# 결과 실행 시간 메모리 Grader output
1 Correct 61 ms 976 KB Output is correct
2 Correct 57 ms 848 KB Output is correct
3 Correct 58 ms 848 KB Output is correct
4 Correct 52 ms 848 KB Output is correct
5 Correct 52 ms 904 KB Output is correct
6 Correct 56 ms 848 KB Output is correct
7 Correct 54 ms 944 KB Output is correct
8 Correct 56 ms 848 KB Output is correct
9 Correct 56 ms 848 KB Output is correct
10 Correct 63 ms 956 KB Output is correct
11 Correct 52 ms 848 KB Output is correct
12 Correct 50 ms 848 KB Output is correct
13 Correct 52 ms 920 KB Output is correct
14 Correct 58 ms 848 KB Output is correct
15 Correct 51 ms 928 KB Output is correct
16 Correct 59 ms 848 KB Output is correct
17 Correct 59 ms 848 KB Output is correct
18 Correct 60 ms 932 KB Output is correct
19 Correct 60 ms 848 KB Output is correct
20 Correct 51 ms 940 KB Output is correct
21 Correct 56 ms 928 KB Output is correct
22 Correct 56 ms 848 KB Output is correct
23 Correct 60 ms 944 KB Output is correct
24 Correct 60 ms 848 KB Output is correct
25 Correct 56 ms 848 KB Output is correct
26 Correct 61 ms 848 KB Output is correct
27 Correct 51 ms 848 KB Output is correct
28 Correct 53 ms 928 KB Output is correct
29 Correct 87 ms 900 KB Output is correct
30 Correct 65 ms 920 KB Output is correct
31 Correct 54 ms 848 KB Output is correct
32 Correct 56 ms 928 KB Output is correct
33 Correct 73 ms 924 KB Output is correct
34 Correct 56 ms 848 KB Output is correct
35 Correct 52 ms 908 KB Output is correct
36 Correct 59 ms 848 KB Output is correct
37 Correct 65 ms 908 KB Output is correct
38 Correct 54 ms 932 KB Output is correct
39 Correct 62 ms 952 KB Output is correct
40 Correct 54 ms 848 KB Output is correct
41 Correct 53 ms 848 KB Output is correct
42 Correct 54 ms 848 KB Output is correct
43 Correct 58 ms 928 KB Output is correct
44 Correct 71 ms 932 KB Output is correct
45 Correct 57 ms 940 KB Output is correct
46 Correct 54 ms 908 KB Output is correct
47 Correct 54 ms 848 KB Output is correct
48 Correct 62 ms 848 KB Output is correct
49 Correct 53 ms 944 KB Output is correct
50 Correct 57 ms 924 KB Output is correct
51 Correct 56 ms 904 KB Output is correct
52 Correct 53 ms 928 KB Output is correct
53 Correct 55 ms 848 KB Output is correct
54 Correct 57 ms 848 KB Output is correct
55 Correct 63 ms 848 KB Output is correct
56 Correct 56 ms 920 KB Output is correct
57 Correct 72 ms 848 KB Output is correct
58 Correct 57 ms 904 KB Output is correct
59 Correct 53 ms 848 KB Output is correct
60 Correct 62 ms 932 KB Output is correct
61 Correct 58 ms 916 KB Output is correct
62 Correct 62 ms 976 KB Output is correct
63 Correct 51 ms 900 KB Output is correct