답안 #836594

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
836594 2023-08-24T13:12:48 Z happypotato Vision Program (IOI19_vision) C++17
100 / 100
13 ms 1904 KB
#include "vision.h"
#include <bits/stdc++.h>
using namespace std;
#define pb push_back

void construct_network(int n, int m, int k) {
	if (n * m == 2) {
		add_and({0, 1});
		return;
	}
	const int ZERO = add_and({0, 1, 2});
	const int ONE = add_not(ZERO);
	function<int(void)> add_zero = [&]() -> int { return add_and({ZERO}); };
	function<int(void)> add_one = [&]() -> int { return add_and({ONE}); };

	function<int(int, int)> hash = [&](int x, int y) -> int {
		return x * m + y;
	};
	function<bool(int, int)> valid = [&](int x, int y) -> bool {
		return ((0 <= x && x < n) && (0 <= y && y < m));
	};
	
	// stores position of min in range [st, en]
	function<int(int, int)> findmin = [&](int st, int en) -> int {
		int diff = en - st + 1;
		int prev = add_zero();
		for (int i = 1; i < 8; i++) add_zero();
		vector<int> allprev;
		int prevtakexor = add_zero();
		for (int cur = 1; cur <= diff; cur++) { // from min
			int pos = st + (cur - 1);
			// check if [prev, prev + 8) has value
			vector<int> prevs = {prevtakexor};
			for (int i = prev; i < prev + 8; i++) prevs.pb(i);
			prevtakexor = add_or(prevs);
			int takexor = add_not(prevtakexor); // 1 if prev = 0
			// assign (pos & takexor & (cur & (1 << i))) to prev
			int nxt = -1;
			for (int i = 0; i < 8; i++) {
				int res = add_and({pos, takexor, (bool(cur & (1 << i)) ? ONE : ZERO)});
				if (nxt == -1) nxt = res;
			}
			prev = nxt;
			allprev.pb(nxt);
		}
		// only 1 has value, take or
		int ret = -1;
		for (int i = 0; i < 8; i++) {
			vector<int> v;
			for (int &x : allprev) v.pb(x + i);
			int res = add_or(v);
			if (ret == -1) ret = res;
		}
		return ret;
	};
	// stores position of max in range [st, en]
	function<int(int, int)> findmax = [&](int st, int en) -> int {
		int diff = en - st + 1;
		int prev = add_zero();
		for (int i = 1; i < 8; i++) add_zero();
		vector<int> allprev;
		int prevtakexor = add_zero();
		for (int cur = diff; cur >= 1; cur--) { // from max
			int pos = st + (cur - 1);
			// check if [prev, prev + 8) has value
			vector<int> prevs = {prevtakexor};
			for (int i = prev; i < prev + 8; i++) prevs.pb(i);
			prevtakexor = add_or(prevs);
			int takexor = add_not(prevtakexor); // 1 if prev = 0
			// assign (pos & takexor & (cur & (1 << i))) to prev
			int nxt = -1;
			for (int i = 0; i < 8; i++) {
				int res = add_and({pos, takexor, (bool(cur & (1 << i)) ? ONE : ZERO)});
				if (nxt == -1) nxt = res;
			}
			prev = nxt;
			allprev.pb(nxt);
		}
		// only 1 has value, take or
		int ret = -1;
		for (int i = 0; i < 8; i++) {
			vector<int> v;
			for (int &x : allprev) v.pb(x + i);
			int res = add_or(v);
			if (ret == -1) ret = res;
		}
		return ret;
	};

	// addition modulo 2^10 = 1024
	function<vector<int>(vector<int>, vector<int>)> add = [&](vector<int> lhs, vector<int> rhs) -> vector<int> {
		vector<int> res;
		int prev = add_zero(); // carrying
		res.pb(add_xor({lhs[0], rhs[0]}));
		for (int i = 1; i < 10; i++) {
			// handle carrying:
			// (prev, lhs[i - 1], rhs[i - 1]) >= 2
			// (AND = 1) or (XOR = 0 and OR = 1)
			// (AND) or (XOR xor OR) (no case where (XOR = 1 and OR = 0))
			vector<int> check = {prev, lhs[i - 1], rhs[i - 1]};
			int isthree = add_and(check);
			int istwo = add_xor({add_xor(check), add_or(check)});
			int fincarry = add_or({isthree, istwo});
			prev = fincarry;
			// calculate current bit
			res.pb(add_xor({prev, lhs[i], rhs[i]}));
		}
		return res;
	};

	int st = -1, en = -1;
	// find vertical
	for (int i = 0; i < n; i++) {
		vector<int> v;
		for (int j = 0; j < m; j++) v.pb(hash(i, j));
		int res = add_or(v);
		if (i == 0) st = res;
		if (i == n - 1) en = res;
	}
	int vmini = findmin(st, en);
	int vmaxi = findmax(st, en);
	// find horizontal
	for (int i = 0; i < m; i++) {
		vector<int> v;
		for (int j = 0; j < n; j++) v.pb(hash(j, i));
		int res = add_or(v);
		if (i == 0) st = res;
		if (i == m - 1) en = res;
	}
	int hmini = findmin(st, en);
	int hmaxi = findmax(st, en);

	// cerr << vmini << ' ' << vmaxi << ' ' << hmini << ' ' << hmaxi << endl;

	//   (vmax - vmin) + (hmax - hmin)
	// = (vmax + (1023 - vmin)) + (hmax + (1023 - hmin)) + 2 (mod 1024)
	// = (vmax + (vmin ^ 1023)) + (hmax + (hmin ^ 1023)) + 2 (mod 1024)

	vector<int> vmax(10), vmin(10), hmax(10), hmin(10);
	vector<int> two(10);
	for (int i = 0; i < 10; i++) {
		// take xor with mins
		vmax[i] = (i < 8 ? vmaxi + i : ZERO);
		vmin[i] = (i < 8 ? add_not(vmini + i) : ONE);
		hmax[i] = (i < 8 ? hmaxi + i : ZERO);
		hmin[i] = (i < 8 ? add_not(hmini + i) : ONE);
		two[i] = (i == 1 ? ONE : ZERO);
	}
	vector<int> res1 = add(vmax, vmin);
	vector<int> res2 = add(hmax, hmin);
	vector<int> dist = add(add(res1, res2), two);
	// compare dist and k
	vector<int> fin;
	for (int i = 0; i < 10; i++) {
		fin.pb(add_xor({dist[i], (bool(k & (1 << i)) ? ONE : ZERO)}));
	}
	add_not(add_or(fin));
}
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 212 KB Output is correct
2 Correct 1 ms 212 KB Output is correct
3 Correct 1 ms 212 KB Output is correct
4 Correct 0 ms 212 KB Output is correct
5 Correct 1 ms 212 KB Output is correct
6 Correct 0 ms 212 KB Output is correct
7 Correct 1 ms 212 KB Output is correct
8 Correct 1 ms 212 KB Output is correct
9 Correct 1 ms 340 KB Output is correct
10 Correct 1 ms 340 KB Output is correct
11 Correct 1 ms 212 KB Output is correct
12 Correct 1 ms 340 KB Output is correct
13 Correct 1 ms 212 KB Output is correct
14 Correct 1 ms 212 KB Output is correct
15 Correct 1 ms 212 KB Output is correct
16 Correct 1 ms 212 KB Output is correct
17 Correct 0 ms 212 KB Output is correct
18 Correct 0 ms 212 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 212 KB Output is correct
2 Correct 1 ms 212 KB Output is correct
3 Correct 1 ms 212 KB Output is correct
4 Correct 0 ms 212 KB Output is correct
5 Correct 1 ms 212 KB Output is correct
6 Correct 0 ms 212 KB Output is correct
7 Correct 1 ms 212 KB Output is correct
8 Correct 1 ms 212 KB Output is correct
9 Correct 1 ms 340 KB Output is correct
10 Correct 1 ms 340 KB Output is correct
11 Correct 1 ms 212 KB Output is correct
12 Correct 1 ms 340 KB Output is correct
13 Correct 1 ms 212 KB Output is correct
14 Correct 1 ms 212 KB Output is correct
15 Correct 1 ms 212 KB Output is correct
16 Correct 1 ms 212 KB Output is correct
17 Correct 0 ms 212 KB Output is correct
18 Correct 0 ms 212 KB Output is correct
19 Correct 1 ms 340 KB Output is correct
20 Correct 1 ms 288 KB Output is correct
21 Correct 1 ms 384 KB Output is correct
22 Correct 1 ms 340 KB Output is correct
23 Correct 1 ms 340 KB Output is correct
24 Correct 1 ms 340 KB Output is correct
25 Correct 1 ms 300 KB Output is correct
26 Correct 1 ms 340 KB Output is correct
27 Correct 1 ms 340 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 212 KB Output is correct
2 Correct 1 ms 212 KB Output is correct
3 Correct 1 ms 212 KB Output is correct
4 Correct 0 ms 212 KB Output is correct
5 Correct 1 ms 212 KB Output is correct
6 Correct 0 ms 212 KB Output is correct
7 Correct 1 ms 212 KB Output is correct
8 Correct 1 ms 212 KB Output is correct
9 Correct 1 ms 340 KB Output is correct
10 Correct 1 ms 340 KB Output is correct
11 Correct 1 ms 212 KB Output is correct
12 Correct 1 ms 340 KB Output is correct
13 Correct 1 ms 212 KB Output is correct
14 Correct 1 ms 212 KB Output is correct
15 Correct 1 ms 212 KB Output is correct
16 Correct 1 ms 212 KB Output is correct
17 Correct 0 ms 212 KB Output is correct
18 Correct 0 ms 212 KB Output is correct
19 Correct 1 ms 340 KB Output is correct
20 Correct 1 ms 288 KB Output is correct
21 Correct 1 ms 384 KB Output is correct
22 Correct 1 ms 340 KB Output is correct
23 Correct 1 ms 340 KB Output is correct
24 Correct 1 ms 340 KB Output is correct
25 Correct 1 ms 300 KB Output is correct
26 Correct 1 ms 340 KB Output is correct
27 Correct 1 ms 340 KB Output is correct
28 Correct 1 ms 340 KB Output is correct
29 Correct 1 ms 340 KB Output is correct
30 Correct 1 ms 340 KB Output is correct
31 Correct 1 ms 340 KB Output is correct
32 Correct 1 ms 340 KB Output is correct
33 Correct 1 ms 340 KB Output is correct
34 Correct 1 ms 340 KB Output is correct
35 Correct 2 ms 340 KB Output is correct
36 Correct 1 ms 340 KB Output is correct
37 Correct 1 ms 340 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 212 KB Output is correct
2 Correct 1 ms 212 KB Output is correct
3 Correct 1 ms 212 KB Output is correct
4 Correct 0 ms 212 KB Output is correct
5 Correct 1 ms 212 KB Output is correct
6 Correct 0 ms 212 KB Output is correct
7 Correct 1 ms 212 KB Output is correct
8 Correct 1 ms 212 KB Output is correct
9 Correct 1 ms 340 KB Output is correct
10 Correct 1 ms 340 KB Output is correct
11 Correct 1 ms 212 KB Output is correct
12 Correct 1 ms 340 KB Output is correct
13 Correct 1 ms 212 KB Output is correct
14 Correct 1 ms 212 KB Output is correct
15 Correct 1 ms 212 KB Output is correct
16 Correct 1 ms 212 KB Output is correct
17 Correct 0 ms 212 KB Output is correct
18 Correct 0 ms 212 KB Output is correct
19 Correct 1 ms 340 KB Output is correct
20 Correct 1 ms 288 KB Output is correct
21 Correct 1 ms 384 KB Output is correct
22 Correct 1 ms 340 KB Output is correct
23 Correct 1 ms 340 KB Output is correct
24 Correct 1 ms 340 KB Output is correct
25 Correct 1 ms 300 KB Output is correct
26 Correct 1 ms 340 KB Output is correct
27 Correct 1 ms 340 KB Output is correct
28 Correct 1 ms 340 KB Output is correct
29 Correct 1 ms 340 KB Output is correct
30 Correct 1 ms 340 KB Output is correct
31 Correct 1 ms 340 KB Output is correct
32 Correct 1 ms 340 KB Output is correct
33 Correct 1 ms 340 KB Output is correct
34 Correct 1 ms 340 KB Output is correct
35 Correct 2 ms 340 KB Output is correct
36 Correct 1 ms 340 KB Output is correct
37 Correct 1 ms 340 KB Output is correct
38 Correct 4 ms 848 KB Output is correct
39 Correct 2 ms 468 KB Output is correct
40 Correct 2 ms 596 KB Output is correct
41 Correct 3 ms 596 KB Output is correct
42 Correct 3 ms 596 KB Output is correct
43 Correct 5 ms 864 KB Output is correct
44 Correct 5 ms 824 KB Output is correct
45 Correct 4 ms 848 KB Output is correct
46 Correct 4 ms 868 KB Output is correct
47 Correct 6 ms 848 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 3 ms 596 KB Output is correct
2 Correct 3 ms 596 KB Output is correct
3 Correct 3 ms 596 KB Output is correct
4 Correct 3 ms 596 KB Output is correct
5 Correct 3 ms 596 KB Output is correct
6 Correct 4 ms 596 KB Output is correct
7 Correct 3 ms 596 KB Output is correct
8 Correct 3 ms 596 KB Output is correct
9 Correct 3 ms 596 KB Output is correct
10 Correct 3 ms 720 KB Output is correct
11 Correct 3 ms 596 KB Output is correct
12 Correct 3 ms 596 KB Output is correct
13 Correct 3 ms 596 KB Output is correct
14 Correct 3 ms 596 KB Output is correct
15 Correct 3 ms 720 KB Output is correct
16 Correct 3 ms 720 KB Output is correct
17 Correct 3 ms 720 KB Output is correct
18 Correct 3 ms 596 KB Output is correct
19 Correct 3 ms 720 KB Output is correct
20 Correct 3 ms 652 KB Output is correct
21 Correct 0 ms 212 KB Output is correct
22 Correct 0 ms 212 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 212 KB Output is correct
2 Correct 1 ms 212 KB Output is correct
3 Correct 3 ms 596 KB Output is correct
4 Correct 2 ms 596 KB Output is correct
5 Correct 2 ms 596 KB Output is correct
6 Correct 3 ms 596 KB Output is correct
7 Correct 3 ms 596 KB Output is correct
8 Correct 3 ms 724 KB Output is correct
9 Correct 3 ms 724 KB Output is correct
10 Correct 4 ms 724 KB Output is correct
11 Correct 4 ms 724 KB Output is correct
12 Correct 3 ms 724 KB Output is correct
13 Correct 3 ms 688 KB Output is correct
14 Correct 3 ms 596 KB Output is correct
15 Correct 3 ms 596 KB Output is correct
16 Correct 3 ms 596 KB Output is correct
17 Correct 3 ms 596 KB Output is correct
18 Correct 3 ms 596 KB Output is correct
19 Correct 4 ms 596 KB Output is correct
20 Correct 7 ms 1236 KB Output is correct
21 Correct 7 ms 1236 KB Output is correct
22 Correct 7 ms 1236 KB Output is correct
23 Correct 7 ms 1236 KB Output is correct
24 Correct 7 ms 1292 KB Output is correct
25 Correct 7 ms 1288 KB Output is correct
26 Correct 7 ms 1188 KB Output is correct
27 Correct 12 ms 1868 KB Output is correct
28 Correct 11 ms 1800 KB Output is correct
29 Correct 12 ms 1868 KB Output is correct
30 Correct 12 ms 1844 KB Output is correct
31 Correct 12 ms 1848 KB Output is correct
32 Correct 0 ms 212 KB Output is correct
33 Correct 0 ms 212 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 12 ms 1868 KB Output is correct
2 Correct 1 ms 212 KB Output is correct
3 Correct 2 ms 596 KB Output is correct
4 Correct 3 ms 724 KB Output is correct
5 Correct 3 ms 596 KB Output is correct
6 Correct 3 ms 596 KB Output is correct
7 Correct 7 ms 1236 KB Output is correct
8 Correct 7 ms 1232 KB Output is correct
9 Correct 13 ms 1904 KB Output is correct
10 Correct 0 ms 212 KB Output is correct
11 Correct 0 ms 212 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 212 KB Output is correct
2 Correct 1 ms 212 KB Output is correct
3 Correct 1 ms 212 KB Output is correct
4 Correct 0 ms 212 KB Output is correct
5 Correct 1 ms 212 KB Output is correct
6 Correct 0 ms 212 KB Output is correct
7 Correct 1 ms 212 KB Output is correct
8 Correct 1 ms 212 KB Output is correct
9 Correct 1 ms 340 KB Output is correct
10 Correct 1 ms 340 KB Output is correct
11 Correct 1 ms 212 KB Output is correct
12 Correct 1 ms 340 KB Output is correct
13 Correct 1 ms 212 KB Output is correct
14 Correct 1 ms 212 KB Output is correct
15 Correct 1 ms 212 KB Output is correct
16 Correct 1 ms 212 KB Output is correct
17 Correct 0 ms 212 KB Output is correct
18 Correct 0 ms 212 KB Output is correct
19 Correct 1 ms 340 KB Output is correct
20 Correct 1 ms 288 KB Output is correct
21 Correct 1 ms 384 KB Output is correct
22 Correct 1 ms 340 KB Output is correct
23 Correct 1 ms 340 KB Output is correct
24 Correct 1 ms 340 KB Output is correct
25 Correct 1 ms 300 KB Output is correct
26 Correct 1 ms 340 KB Output is correct
27 Correct 1 ms 340 KB Output is correct
28 Correct 1 ms 340 KB Output is correct
29 Correct 1 ms 340 KB Output is correct
30 Correct 1 ms 340 KB Output is correct
31 Correct 1 ms 340 KB Output is correct
32 Correct 1 ms 340 KB Output is correct
33 Correct 1 ms 340 KB Output is correct
34 Correct 1 ms 340 KB Output is correct
35 Correct 2 ms 340 KB Output is correct
36 Correct 1 ms 340 KB Output is correct
37 Correct 1 ms 340 KB Output is correct
38 Correct 4 ms 848 KB Output is correct
39 Correct 2 ms 468 KB Output is correct
40 Correct 2 ms 596 KB Output is correct
41 Correct 3 ms 596 KB Output is correct
42 Correct 3 ms 596 KB Output is correct
43 Correct 5 ms 864 KB Output is correct
44 Correct 5 ms 824 KB Output is correct
45 Correct 4 ms 848 KB Output is correct
46 Correct 4 ms 868 KB Output is correct
47 Correct 6 ms 848 KB Output is correct
48 Correct 3 ms 596 KB Output is correct
49 Correct 3 ms 596 KB Output is correct
50 Correct 3 ms 596 KB Output is correct
51 Correct 3 ms 596 KB Output is correct
52 Correct 3 ms 596 KB Output is correct
53 Correct 4 ms 596 KB Output is correct
54 Correct 3 ms 596 KB Output is correct
55 Correct 3 ms 596 KB Output is correct
56 Correct 3 ms 596 KB Output is correct
57 Correct 3 ms 720 KB Output is correct
58 Correct 3 ms 596 KB Output is correct
59 Correct 3 ms 596 KB Output is correct
60 Correct 3 ms 596 KB Output is correct
61 Correct 3 ms 596 KB Output is correct
62 Correct 3 ms 720 KB Output is correct
63 Correct 3 ms 720 KB Output is correct
64 Correct 3 ms 720 KB Output is correct
65 Correct 3 ms 596 KB Output is correct
66 Correct 3 ms 720 KB Output is correct
67 Correct 3 ms 652 KB Output is correct
68 Correct 0 ms 212 KB Output is correct
69 Correct 0 ms 212 KB Output is correct
70 Correct 0 ms 212 KB Output is correct
71 Correct 1 ms 212 KB Output is correct
72 Correct 3 ms 596 KB Output is correct
73 Correct 2 ms 596 KB Output is correct
74 Correct 2 ms 596 KB Output is correct
75 Correct 3 ms 596 KB Output is correct
76 Correct 3 ms 596 KB Output is correct
77 Correct 3 ms 724 KB Output is correct
78 Correct 3 ms 724 KB Output is correct
79 Correct 4 ms 724 KB Output is correct
80 Correct 4 ms 724 KB Output is correct
81 Correct 3 ms 724 KB Output is correct
82 Correct 3 ms 688 KB Output is correct
83 Correct 3 ms 596 KB Output is correct
84 Correct 3 ms 596 KB Output is correct
85 Correct 3 ms 596 KB Output is correct
86 Correct 3 ms 596 KB Output is correct
87 Correct 3 ms 596 KB Output is correct
88 Correct 4 ms 596 KB Output is correct
89 Correct 7 ms 1236 KB Output is correct
90 Correct 7 ms 1236 KB Output is correct
91 Correct 7 ms 1236 KB Output is correct
92 Correct 7 ms 1236 KB Output is correct
93 Correct 7 ms 1292 KB Output is correct
94 Correct 7 ms 1288 KB Output is correct
95 Correct 7 ms 1188 KB Output is correct
96 Correct 12 ms 1868 KB Output is correct
97 Correct 11 ms 1800 KB Output is correct
98 Correct 12 ms 1868 KB Output is correct
99 Correct 12 ms 1844 KB Output is correct
100 Correct 12 ms 1848 KB Output is correct
101 Correct 0 ms 212 KB Output is correct
102 Correct 0 ms 212 KB Output is correct
103 Correct 12 ms 1868 KB Output is correct
104 Correct 1 ms 212 KB Output is correct
105 Correct 2 ms 596 KB Output is correct
106 Correct 3 ms 724 KB Output is correct
107 Correct 3 ms 596 KB Output is correct
108 Correct 3 ms 596 KB Output is correct
109 Correct 7 ms 1236 KB Output is correct
110 Correct 7 ms 1232 KB Output is correct
111 Correct 13 ms 1904 KB Output is correct
112 Correct 0 ms 212 KB Output is correct
113 Correct 0 ms 212 KB Output is correct
114 Correct 12 ms 1860 KB Output is correct
115 Correct 3 ms 720 KB Output is correct
116 Correct 3 ms 644 KB Output is correct
117 Correct 7 ms 1236 KB Output is correct
118 Correct 7 ms 1232 KB Output is correct
119 Correct 11 ms 1868 KB Output is correct
120 Correct 11 ms 1852 KB Output is correct
121 Correct 12 ms 1868 KB Output is correct
122 Correct 12 ms 1900 KB Output is correct
123 Correct 11 ms 1828 KB Output is correct
124 Correct 12 ms 1824 KB Output is correct
125 Correct 13 ms 1828 KB Output is correct
126 Correct 12 ms 1868 KB Output is correct
127 Correct 12 ms 1868 KB Output is correct
128 Correct 11 ms 1808 KB Output is correct