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
#include <cstdio>
#include <set>
#include <queue>
#include <utility>

struct State
{
    int a, b, c;

    bool operator<(const State &other) const {
        return (a < other.a) || (
                (a == other.a) && (
                        (b < other.b) || (
                                (b == other.b) && (c < other.c)
                        )
                )
        );
    }
};

typedef int State::*Pole;

int turns[100001];

bool przelej(const State& start, State& target, Pole from, Pole to, int max_to) {
    if (!(start.*from)) {
        return false;
    }
    if (start.*to == max_to) {
        return false;
    }
    target = start;
    int ilosc = std::min(start.*from, max_to - start.*to);
    target.*from -= ilosc;
    target.*to += ilosc;
    return true;
}

int main() {
    int A, B, C;
    int a, b, c;

    scanf("%d%d%d", &A, &B, &C);
    scanf("%d%d%d", &a, &b, &c);
    std::fill(turns, turns+C+1, -1);

    std::set<State> visited;
    std::queue<std::pair<State,int>> queue;
    queue.push({{a, b, c}, 0});

    int counted = 0;
    while (!queue.empty() && counted<=C) {
        const auto& pair = queue.front();
        State state = pair.first;
        int turn = pair.second;
        queue.pop();
        if (!visited.count(pair.first)) {
            visited.insert(pair.first);
            if (turns[state.a] < 0) {
                turns[state.a] = turn;
                ++counted;
            }
            if (turns[state.b] < 0) {
                turns[state.b] = turn;
                ++counted;
            }
            if (turns[state.c] < 0) {
                turns[state.c] = turn;
                ++counted;
            }

            State target;
            if (przelej(state, target, &State::b, &State::a, A)) {
                queue.push({target, turn+1});
            }
            if (przelej(state, target, &State::c, &State::a, A)) {
                queue.push({target, turn+1});
            }
            if (przelej(state, target, &State::a, &State::b, B)) {
                queue.push({target, turn+1});
            }
            if (przelej(state, target, &State::c, &State::b, B)) {
                queue.push({target, turn+1});
            }
            if (przelej(state, target, &State::a, &State::c, C)) {
                queue.push({target, turn+1});
            }
            if (przelej(state, target, &State::b, &State::c, C)) {
                queue.push({target, turn+1});
            }
        }
    }

    for (int i=0; i<C; ++i) {
        printf("%d ", turns[i]);
    }
    printf("%d\n", turns[C]);
}