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
/* -----------------------
Autor: Tomasz Boguslawski
-------------------------- */
#include<cstdio>
#include<cstdlib>
#include<iostream>
#include<fstream>
#include<iomanip>
#include<string>
#include<sstream>
#include<cstring>
#include<map>
#include<vector>
#include<set>
#include<queue>
#include<algorithm>
#include <fstream>
#include<math.h>

#define FOR(x, b, e) for(long x = b; x <= (e); x++)
#define FORD(x, b, e) for(long x = b; x >= (e); x--)
#define VAR(v, n) __typeof(n) v = (n)
#define ALL(c) (c).begin(), (c).end()
#define FOREACH(i, c) for(VAR(i, (c).begin()); i != (c).end(); ++i)
#define DEBUG if (debug)
#define MIN(a,b) ((a>b)?b:a)
#define MAX(a,b) ((a>b)?a:b)
#define LL long long

using namespace std;

LL A,B,C;

LL res[110000];
set<LL> visited;

LL code(LL a, LL b, LL c)
{
    LL code=a*110000;
    code=code*110000;
    code=code+b*110000;
    code=code+c;
    return code;
}

void store(LL a, LL b, LL c)
{
    visited.insert(code(a,b,c));
}

bool wasBefore(LL a, LL b, LL c)
{
    return (visited.count(code(a,b,c))!=0);
}

inline LL mini(LL x, LL y)
{
    if (x<y) return x; else return y;
}

void przelej(long jak, LL &a, LL &b, LL &c)
{
    //cout << " przelewam na sposob " << jak << " inicjalnie majac: " << a << " " << b << " " << c << "\n";
    LL ileMoznaWlac;
    LL d;
    if (jak==12)
    {
        ileMoznaWlac=B-b;
        d=mini(a,ileMoznaWlac);
        a-=d; b+=d;
    }
    else if (jak==13)
    {
        ileMoznaWlac=C-c;
        d=mini(a,ileMoznaWlac);
        a-=d; c+=d;
    }
    else if (jak==21)
    {
        ileMoznaWlac=A-a;
        d=mini(b,ileMoznaWlac);
        b-=d; a+=d;
    }
    else if (jak==23)
    {
        ileMoznaWlac=C-c;
        d=mini(b,ileMoznaWlac);
        //cout << " ileMoznaWlac=" << ileMoznaWlac << " C=" << C << " c=" << c << " d=" << d << " b=" << b << "\n";
        b-=d; c+=d;
    }
    else if (jak==31)
    {
        ileMoznaWlac=A-a;
        d=mini(c,ileMoznaWlac);
        c-=d; a+=d;
    }
    else if (jak==32)
    {
        ileMoznaWlac=B-b;
        d=mini(c,ileMoznaWlac);
        c-=d; b+=d;
    }
    //cout << " i otrzymuje: " << a << " " << b << " " << c << "\n";
}

void registerNewState(LL steps, LL a, LL b, LL c)
{
    if (res[a]==-1) res[a]=steps;
    if (res[b]==-1) res[b]=steps;
    if (res[c]==-1) res[c]=steps;
    store(a,b,c);
}

/// MAIN
int main(int argc, char* argv[])
{
    // magic formula, which makes streams work faster:
	ios_base::sync_with_stdio(0);

    cin >> A; cin >> B; cin >> C;
    LL a,b,c;
    cin >> a; cin >> b; cin >> c;
    FOR(i,0,C) res[i]=-1;
    res[a]=0; res[b]=0; res[c]=0;
    store(a,b,c);
    queue<LL> que[2];
    que[0].push(code(a,b,c));
    LL next;
    LL steps=0;
    LL aa, bb, cc;
    long q1=0; long q2=1;
    while(true)
    {
        steps++;
        //cout << "-- Step=" << steps << "\n";
        // que[q2] jest pusta tutaj, a que[q1] nie jest posta
        while(!que[q1].empty())
        {
            next=que[q1].front(); que[q1].pop();
            c=next%110000; next=next/110000;
            b=next%110000; next=next/110000;
            a=next%110000; next=next/110000;
            //cout << "next: a=" << a << " b=" << b << " c=" << c << "\n";
            aa=a; bb=b; cc=c; przelej(12,aa,bb,cc); if (!wasBefore(aa,bb,cc)) { registerNewState(steps, aa,bb,cc); que[q2].push(code(aa,bb,cc)); };
            aa=a; bb=b; cc=c; przelej(13,aa,bb,cc); if (!wasBefore(aa,bb,cc)) { registerNewState(steps, aa,bb,cc); que[q2].push(code(aa,bb,cc)); };
            aa=a; bb=b; cc=c; przelej(21,aa,bb,cc); if (!wasBefore(aa,bb,cc)) { registerNewState(steps, aa,bb,cc); que[q2].push(code(aa,bb,cc)); };
            aa=a; bb=b; cc=c; przelej(23,aa,bb,cc); if (!wasBefore(aa,bb,cc)) { registerNewState(steps, aa,bb,cc); que[q2].push(code(aa,bb,cc)); };
            aa=a; bb=b; cc=c; przelej(31,aa,bb,cc); if (!wasBefore(aa,bb,cc)) { registerNewState(steps, aa,bb,cc); que[q2].push(code(aa,bb,cc)); };
            aa=a; bb=b; cc=c; przelej(32,aa,bb,cc); if (!wasBefore(aa,bb,cc)) { registerNewState(steps, aa,bb,cc); que[q2].push(code(aa,bb,cc)); };
        }
        if (que[q2].empty()) break; // nic sie nie pojawilo nowego
        if (q1==0) { q1=1; q2=0; } else { q1=0; q2=1; };
    }
    FOR(i,0,C) cout << res[i] << ' '; cout << "\n";
    return 0;
};