#include <iostream> #include <set> #include <vector> #include <map> struct TODO { int val; long long h; }; long long hash(long long a, long long b, long long c) { return (a << 34) | (b << 17) | c; } int h2a(long long h) { return (int) (h >> 34); } int h2b(long long h) { return (int) ((h >> 17) & ((1<<17)-1)); } int h2c(long long h) { return (int) (h & ((1<<17)-1)); } int aa, bb, cc; int done = 0; long long a2b(long long h) { int a = h2a(h), b = h2b(h), c = h2c(h), x; if(a+b > bb) { x = bb-b; a -= x; b = bb; } else { x = a; a = 0; b += x; } return hash(a, b, c); } long long a2c(long long h) { int a = h2a(h), b = h2b(h), c = h2c(h), x; if(a+c > cc) { x = cc-c; a -= x; c = cc; } else { x = a; a = 0; c += x; } return hash(a, b, c); } long long b2a(long long h) { int a = h2a(h), b = h2b(h), c = h2c(h), x; if(a+b > aa) { x = aa-a; b -= x; a = aa; } else { x = b; b = 0; a += x; } return hash(a, b, c); } long long b2c(long long h) { int a = h2a(h), b = h2b(h), c = h2c(h), x; if(b+c > cc) { x = cc-c; b -= x; c = cc; } else { x = b; b = 0; c += x; } return hash(a, b, c); } long long c2a(long long h) { int a = h2a(h), b = h2b(h), c = h2c(h), x; if(a+c > aa) { x = aa-a; c -= x; a = aa; } else { x = c; c = 0; a += x; } return hash(a, b, c); } long long c2b(long long h) { int a = h2a(h), b = h2b(h), c = h2c(h), x; if(b+c > bb) { x = bb-b; c -= x; b = bb; } else { x = c; c = 0; b += x; } return hash(a, b, c); } std::set<long long> checked; std::vector<TODO> todo; std::vector<int> res; void check() { int a, b, c, val; long long h, hh; h = todo[0].h; if(checked.count(h) == 0) { checked.insert(h); a = h2a(h); b = h2b(h); c = h2c(h); val = todo[0].val; if(res[a] == -1) { res[a] = val; done++; } if(res[b] == -1) { res[b] = val; done++; } if(res[c] == -1) { res[c] = val; done++; } hh = a2b(h); if(checked.count(hh) == 0) todo.push_back({val+1, hh}); hh = a2c(h); if(checked.count(hh) == 0) todo.push_back({val+1, hh}); hh = b2a(h); if(checked.count(hh) == 0) todo.push_back({val+1, hh}); hh = b2c(h); if(checked.count(hh) == 0) todo.push_back({val+1, hh}); hh = c2a(h); if(checked.count(hh) == 0) todo.push_back({val+1, hh}); hh = c2b(h); if(checked.count(hh) == 0) todo.push_back({val+1, hh}); } todo.erase(todo.begin()); } int main() { int a, b, c; std::cin >> aa >> bb >> cc; std::cin >> a >> b >> c; res.resize(cc+1); fill(res.begin(), res.end(), -1); todo.push_back({0, hash(a, b, c)}); while(!todo.empty() && done <= cc) { check(); } for(const auto& el: res) std::cout << el << ' '; std::cout << std::endl; return 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 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 | #include <iostream> #include <set> #include <vector> #include <map> struct TODO { int val; long long h; }; long long hash(long long a, long long b, long long c) { return (a << 34) | (b << 17) | c; } int h2a(long long h) { return (int) (h >> 34); } int h2b(long long h) { return (int) ((h >> 17) & ((1<<17)-1)); } int h2c(long long h) { return (int) (h & ((1<<17)-1)); } int aa, bb, cc; int done = 0; long long a2b(long long h) { int a = h2a(h), b = h2b(h), c = h2c(h), x; if(a+b > bb) { x = bb-b; a -= x; b = bb; } else { x = a; a = 0; b += x; } return hash(a, b, c); } long long a2c(long long h) { int a = h2a(h), b = h2b(h), c = h2c(h), x; if(a+c > cc) { x = cc-c; a -= x; c = cc; } else { x = a; a = 0; c += x; } return hash(a, b, c); } long long b2a(long long h) { int a = h2a(h), b = h2b(h), c = h2c(h), x; if(a+b > aa) { x = aa-a; b -= x; a = aa; } else { x = b; b = 0; a += x; } return hash(a, b, c); } long long b2c(long long h) { int a = h2a(h), b = h2b(h), c = h2c(h), x; if(b+c > cc) { x = cc-c; b -= x; c = cc; } else { x = b; b = 0; c += x; } return hash(a, b, c); } long long c2a(long long h) { int a = h2a(h), b = h2b(h), c = h2c(h), x; if(a+c > aa) { x = aa-a; c -= x; a = aa; } else { x = c; c = 0; a += x; } return hash(a, b, c); } long long c2b(long long h) { int a = h2a(h), b = h2b(h), c = h2c(h), x; if(b+c > bb) { x = bb-b; c -= x; b = bb; } else { x = c; c = 0; b += x; } return hash(a, b, c); } std::set<long long> checked; std::vector<TODO> todo; std::vector<int> res; void check() { int a, b, c, val; long long h, hh; h = todo[0].h; if(checked.count(h) == 0) { checked.insert(h); a = h2a(h); b = h2b(h); c = h2c(h); val = todo[0].val; if(res[a] == -1) { res[a] = val; done++; } if(res[b] == -1) { res[b] = val; done++; } if(res[c] == -1) { res[c] = val; done++; } hh = a2b(h); if(checked.count(hh) == 0) todo.push_back({val+1, hh}); hh = a2c(h); if(checked.count(hh) == 0) todo.push_back({val+1, hh}); hh = b2a(h); if(checked.count(hh) == 0) todo.push_back({val+1, hh}); hh = b2c(h); if(checked.count(hh) == 0) todo.push_back({val+1, hh}); hh = c2a(h); if(checked.count(hh) == 0) todo.push_back({val+1, hh}); hh = c2b(h); if(checked.count(hh) == 0) todo.push_back({val+1, hh}); } todo.erase(todo.begin()); } int main() { int a, b, c; std::cin >> aa >> bb >> cc; std::cin >> a >> b >> c; res.resize(cc+1); fill(res.begin(), res.end(), -1); todo.push_back({0, hash(a, b, c)}); while(!todo.empty() && done <= cc) { check(); } for(const auto& el: res) std::cout << el << ' '; std::cout << std::endl; return 0; } |