#include <bits/stdc++.h>
using namespace std;
uint MAX_VOL = 1e6;
void unpack(uint64_t state, uint vals[]) {
for (int i = 2; i >= 0; i--) {
vals[i] = state % MAX_VOL;
state /= MAX_VOL;
}
}
uint64_t to_state(const uint vals[]) {
uint64_t result = 0;
for (int i = 0; i < 3; i++)
result = result * MAX_VOL + vals[i];
return result;
}
int main() {
ios_base::sync_with_stdio(0); cin.tie(0);
uint A, B, C;
uint a, b, c;
unordered_set<uint64_t> visited;
queue<pair<int, uint64_t>> cand;
cin >> A >> B >> C;
cin >> a >> b >> c;
vector<int> min_obt(C+1, -1);
const uint limits[3] = {A, B, C};
uint tab_vals[3] = {a, b, c};
auto zero_state = to_state(tab_vals);
cand.push({0, zero_state});
while (!cand.empty()) {
auto top = cand.front(); cand.pop();
if (visited.count(top.second)) continue;
visited.insert(top.second);
int dist = top.first;
unpack(top.second, tab_vals);
for (int i = 0; i < 3; i++)
if (min_obt[tab_vals[i]] == -1)
min_obt[tab_vals[i]] = dist;
for (int i = 0; i < 3; i++)
for (int j = 0; j < 3; j++) {
if (i == j) continue;
uint a = tab_vals[i];
uint b = tab_vals[j];
uint liq_sum = a + b;
uint new_a, new_b;
if (liq_sum <= limits[i]) {
new_a = liq_sum;
new_b = 0;
} else {
new_a = limits[i];
new_b = liq_sum - limits[i];
}
tab_vals[i] = new_a;
tab_vals[j] = new_b;
auto new_state = to_state(tab_vals);
if (!visited.count(new_state)) cand.push({dist + 1, new_state});
tab_vals[i] = a;
tab_vals[j] = b;
}
}
for (int i : min_obt) cout << i << " ";
cout << 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 | #include <bits/stdc++.h> using namespace std; uint MAX_VOL = 1e6; void unpack(uint64_t state, uint vals[]) { for (int i = 2; i >= 0; i--) { vals[i] = state % MAX_VOL; state /= MAX_VOL; } } uint64_t to_state(const uint vals[]) { uint64_t result = 0; for (int i = 0; i < 3; i++) result = result * MAX_VOL + vals[i]; return result; } int main() { ios_base::sync_with_stdio(0); cin.tie(0); uint A, B, C; uint a, b, c; unordered_set<uint64_t> visited; queue<pair<int, uint64_t>> cand; cin >> A >> B >> C; cin >> a >> b >> c; vector<int> min_obt(C+1, -1); const uint limits[3] = {A, B, C}; uint tab_vals[3] = {a, b, c}; auto zero_state = to_state(tab_vals); cand.push({0, zero_state}); while (!cand.empty()) { auto top = cand.front(); cand.pop(); if (visited.count(top.second)) continue; visited.insert(top.second); int dist = top.first; unpack(top.second, tab_vals); for (int i = 0; i < 3; i++) if (min_obt[tab_vals[i]] == -1) min_obt[tab_vals[i]] = dist; for (int i = 0; i < 3; i++) for (int j = 0; j < 3; j++) { if (i == j) continue; uint a = tab_vals[i]; uint b = tab_vals[j]; uint liq_sum = a + b; uint new_a, new_b; if (liq_sum <= limits[i]) { new_a = liq_sum; new_b = 0; } else { new_a = limits[i]; new_b = liq_sum - limits[i]; } tab_vals[i] = new_a; tab_vals[j] = new_b; auto new_state = to_state(tab_vals); if (!visited.count(new_state)) cand.push({dist + 1, new_state}); tab_vals[i] = a; tab_vals[j] = b; } } for (int i : min_obt) cout << i << " "; cout << endl; return 0; } |
English