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
#include <bits/stdc++.h>1
using namespace std;

using tup = tuple<int, int, int>;

int A, B, C;
int a, b, c;
int first_found[100007];
set<tup> s;
queue<pair<tup, int>> q;

void bfs() {
    while (!q.empty()) {
        tup state = q.front().first;
        int turn = q.front().second;
        q.pop();
        
        int a_state = get<0>(state);
        int b_state = get<1>(state);
        int c_state = get<2>(state);
        
        first_found[a_state] = min(first_found[a_state], turn);
        first_found[b_state] = min(first_found[b_state], turn);
        first_found[c_state] = min(first_found[c_state], turn);
        
        int ab = min(a_state, B - b_state);
        int ac = min(a_state, C - c_state);
        int ba = min(b_state, A - a_state);
        int bc = min(b_state, C - c_state);
        int ca = min(c_state, A - a_state);
        int cb = min(c_state, B - b_state);
        
        tup a_pour_b = {a_state - ab, b_state + ab, c_state};
        tup a_pour_c = {a_state - ac, b_state, c_state + ac};
        tup b_pour_a = {a_state + ba, b_state - ba, c_state};
        tup b_pour_c = {a_state, b_state - bc, c_state + bc};
        tup c_pour_a = {a_state + ca, b_state, c_state - ca};
        tup c_pour_b = {a_state, b_state + cb, c_state - cb};
        
        if (s.find(a_pour_b) == s.end()) {
            s.insert(a_pour_b);
            q.push({a_pour_b, turn + 1});
        }
        if (s.find(a_pour_c) == s.end()) {
            s.insert(a_pour_c);
            q.push({a_pour_c, turn + 1});
        }
        if (s.find(b_pour_a) == s.end()) {
            s.insert(b_pour_a);
            q.push({b_pour_a, turn + 1});
        }
        if (s.find(b_pour_c) == s.end()) {
            s.insert(b_pour_c);
            q.push({b_pour_c, turn + 1});
        }
        if (s.find(c_pour_a) == s.end()) {
            s.insert(c_pour_a);
            q.push({c_pour_a, turn + 1});
        }
        if (s.find(c_pour_b) == s.end()) {
            s.insert(c_pour_b);
            q.push({c_pour_b, turn + 1});
        }
    }
}

int main() {

    ios_base::sync_with_stdio(false);
    cin.tie(nullptr);
    cout.tie(nullptr);

    cin >> A >> B >> C;
    cin >> a >> b >> c;

    for (int i = 0; i <= C; i++) {
        first_found[i] = INT_MAX;
    }

    s.insert({a, b, c});

    q.push({{a, b, c}, 0});
    bfs();

    for (int i = 0; i <= C; i++) {
        cout << (first_found[i] == INT_MAX ? -1 : first_found[i]) << " ";
    }

    return 0;
}