#include <bits/stdc++.h> #define ull unsigned long long int n, m; std::vector<std::pair<int, int>> v; struct Mask3 { ull x, y; short get(short i) { if (x & (1ull << i)) return (y & (1ull << i)) ? 2 : 1; return 0; } void set(short i, short d) { y &= (~(1ull << i)); switch (d) { case 0: x &= (~(1ull << i)); break; case 2: y |= (1ull << i); case 1: x |= (1ull << i); } } }; void input() { std::cin >> n >> m; for (int i = 1; i <= m; i++) { int x, y; std::cin >> x >> y; v.push_back({x - 1, y - 1}); } } void print_mask(ull x) { for (int i = 1; i <= n; i++) std::cout << ((x & (1ull << i)) ? "1" : "0") << " "; std::cout << "\n"; } int min1, max1; int x, y, w, wmin, wmax, l; bool found10; short digits[40]; ull z; ull calc_ranges(Mask3 S) { z = 0; min1 = -1; max1 = n + 5; found10 = false; for (int i = 0; i < n; i++) { digits[i] = S.get(i); if (digits[i] == 1) { if (found10) return 0; if (min1 == -1) min1 = i; max1 = i; } else if (digits[i] == 2 && min1 != -1) found10 = true; } digits[n] = 2; if (min1 != -1) { x = min1; y = max1; while (x > 0 && digits[x - 1] != 2) x--; while (y < n - 1 && digits[y + 1] != 2) y++; for (int k = y - x + 1; k >= max1 - min1 + 1; k--) { wmin = std::max(x, max1 - k + 1); wmax = std::min(min1, y - k + 1); w = (wmax - wmin + 1) % 2; if (w == 1) z ^= (1ull << k); } } else { x = -1; y = 0; while (x < n) { if (digits[y] == 2) { l = y - x - 1; for (int k = l; k >= 1; k -= 2) z ^= (1ull << k); x = y; } y++; } } return z; } short da, db; ull f(Mask3 S, int i) { if (i == m) return calc_ranges(S); auto [a, b] = v[i]; da = S.get(a); db = S.get(b); if (da == 0 && db == 0) { // ?? - rozbijamy sie na dwa Mask3 Z = S; S.set(a, 1); S.set(b, 1); Z.set(a, 2); Z.set(b, 2); return f(S, i + 1) ^ f(Z, i + 1); } else if (da == 0 && db == 2) { // ?0 - powstaje 01 albo 00, czyli powstaje 0? S.set(a, 2); S.set(b, 0); return f(S, i + 1); } else if (da == 1 && db == 2) { // 10, zamienia sie na 01 S.set(a, 2); S.set(b, 1); return f(S, i + 1); } else if (da == 1 && db == 0) { // 1?, powstaje 11 lub 01, czyli zamienia sie na ?1 S.set(a, 0); S.set(b, 1); return f(S, i + 1); } else { // ?1, 0?, 00, 01, 11 - nic sie nie zmienia return f(S, i + 1); } } int main() { std::ios_base::sync_with_stdio(0); std::cin.tie(NULL); input(); Mask3 S; S.x = S.y = 0; print_mask(f(S, 0)); }
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 | #include <bits/stdc++.h> #define ull unsigned long long int n, m; std::vector<std::pair<int, int>> v; struct Mask3 { ull x, y; short get(short i) { if (x & (1ull << i)) return (y & (1ull << i)) ? 2 : 1; return 0; } void set(short i, short d) { y &= (~(1ull << i)); switch (d) { case 0: x &= (~(1ull << i)); break; case 2: y |= (1ull << i); case 1: x |= (1ull << i); } } }; void input() { std::cin >> n >> m; for (int i = 1; i <= m; i++) { int x, y; std::cin >> x >> y; v.push_back({x - 1, y - 1}); } } void print_mask(ull x) { for (int i = 1; i <= n; i++) std::cout << ((x & (1ull << i)) ? "1" : "0") << " "; std::cout << "\n"; } int min1, max1; int x, y, w, wmin, wmax, l; bool found10; short digits[40]; ull z; ull calc_ranges(Mask3 S) { z = 0; min1 = -1; max1 = n + 5; found10 = false; for (int i = 0; i < n; i++) { digits[i] = S.get(i); if (digits[i] == 1) { if (found10) return 0; if (min1 == -1) min1 = i; max1 = i; } else if (digits[i] == 2 && min1 != -1) found10 = true; } digits[n] = 2; if (min1 != -1) { x = min1; y = max1; while (x > 0 && digits[x - 1] != 2) x--; while (y < n - 1 && digits[y + 1] != 2) y++; for (int k = y - x + 1; k >= max1 - min1 + 1; k--) { wmin = std::max(x, max1 - k + 1); wmax = std::min(min1, y - k + 1); w = (wmax - wmin + 1) % 2; if (w == 1) z ^= (1ull << k); } } else { x = -1; y = 0; while (x < n) { if (digits[y] == 2) { l = y - x - 1; for (int k = l; k >= 1; k -= 2) z ^= (1ull << k); x = y; } y++; } } return z; } short da, db; ull f(Mask3 S, int i) { if (i == m) return calc_ranges(S); auto [a, b] = v[i]; da = S.get(a); db = S.get(b); if (da == 0 && db == 0) { // ?? - rozbijamy sie na dwa Mask3 Z = S; S.set(a, 1); S.set(b, 1); Z.set(a, 2); Z.set(b, 2); return f(S, i + 1) ^ f(Z, i + 1); } else if (da == 0 && db == 2) { // ?0 - powstaje 01 albo 00, czyli powstaje 0? S.set(a, 2); S.set(b, 0); return f(S, i + 1); } else if (da == 1 && db == 2) { // 10, zamienia sie na 01 S.set(a, 2); S.set(b, 1); return f(S, i + 1); } else if (da == 1 && db == 0) { // 1?, powstaje 11 lub 01, czyli zamienia sie na ?1 S.set(a, 0); S.set(b, 1); return f(S, i + 1); } else { // ?1, 0?, 00, 01, 11 - nic sie nie zmienia return f(S, i + 1); } } int main() { std::ios_base::sync_with_stdio(0); std::cin.tie(NULL); input(); Mask3 S; S.x = S.y = 0; print_mask(f(S, 0)); } |