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
#pragma GCC optimize("O3")
#pragma GCC optimize("unroll-loops")

#include <bits/stdc++.h>

using namespace std;

typedef uint32_t ul;
typedef int32_t l;
typedef uint64_t ull;
typedef int64_t ll;

const l INF = 1000000000 + 9;
const ll MOD = 123456789;
const l PRIME = 200003;
const ll L_INF = 1000000000000000000LL + 7;
const double EPS = 1e-5;

#define FOR(x, y, z) for (l z = x; z < y; z++)
#define FORE(x, y, z) for (l z = x; z <= y; z++)
#define FORD(x, y, z) for (l z = x; z > y; z--)
#define FORDE(x, y, z) for (l z = x; z >= y; z--)
#define REP(x, y) for (l y = 0; y < x; y++)
#define ALL(...) (__VA_ARGS__).begin(), (__VA_ARGS__).end()

#define PF push_front
#define POF pop_front
#define PB push_back
#define POB pop_back
#define MP make_pair
#define FS first
#define SC second

l A, B, C, a, b, c;
l dist[100005];
map<pair<l, pair<l, l >>, bool> visited;

struct State
{
    l a, b, c;
    l dist;
};

pair<l, pair<l, l>> make_move(l x, l y, l z, l from, l to)
{
    l kubek[3] = {x, y, z};
    l rozmiar[3] = {A, B, C};

    l nadmiar = max(0, kubek[from]-(rozmiar[to]-kubek[to]));
    kubek[to] = kubek[to] + kubek[from] - nadmiar;
    kubek[from] = nadmiar;

    return {kubek[0], {kubek[1], kubek[2]}};
}

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

    cin >> A >> B >> C >> a >> b >> c;
    fill(dist, dist+100005, INF);
    dist[a] = dist[b] = dist[c] = 0;

    visited[{a, {b, c}}] = true;
    queue<State> Q;
    Q.push({a, b, c, 0});

    while(Q.size())
    {
        auto v = Q.front();
        Q.pop();

        FORE(0, 2, from)
            FORE(0, 2, to)
                if(from != to && visited[make_move(v.a, v.b, v.c, from, to)] == false)
                {
                    auto u = make_move(v.a, v.b, v.c, from, to);
                    //cout << x << " " << y << " " << z << " -> " << u.FS << " " << u.SC.FS << " " << u.SC.SC << "\n";
                    visited[u] = true;
                    Q.push({u.FS, u.SC.FS, u.SC.SC, v.dist+1});

                    dist[u.FS] = min(dist[u.FS], v.dist+1);
                    dist[u.SC.FS] = min(dist[u.SC.FS], v.dist+1);
                    dist[u.SC.SC] = min(dist[u.SC.SC], v.dist+1);
                }
    }

    FORE(0, C, i)
        cout << (dist[i] == INF ? -1 : dist[i]) << " ";
    cout << "\n";
}