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
#include <bits/stdc++.h>

using namespace std;

struct state {
  int a;
  int b;
  int c;
  state(int aa,int bb, int cc)   {
      a=aa;
      b=bb;
      c=cc;
  }
};
bool operator< (state s1, state s2) {
    if(s1.a < s2.a) return true;
    if(s1.a==s2.a && s1.b < s2.b) return true;
    if(s1.a==s2.a && s1.b == s2.b && s1.c < s2.c) return true;
    return false;
}

int turns[100001];

vector<state> new_sts;
vector<state> curr_turn;
vector<state> next_turn;

map<state,bool> previous;

state pour(int i,int j,state s, int A, int B ,int C) {
    int ABC[3]={A,B,C};
    int abc[3]={s.a,s.b,s.c};
    if(abc[i]+abc[j] <= ABC[j]) {
        abc[j]+=abc[i];
        abc[i]=0;
    }
    else {
        abc[i]=abc[i]+abc[j]-ABC[j];
        abc[j]=ABC[j];
    }
    state ss=state(abc[0],abc[1],abc[2]);
    return ss;
}

void generate(state s, int A, int B, int C) {
    new_sts.clear();
    for(int i=0; i<3; ++i)
        for(int j=0; j<3; ++j )
            if(i!=j)
               new_sts.push_back(pour(i,j,s,A,B,C));
}

int main() {
    int A, B, C, a, b, c;
    scanf("%d",&A);
    scanf("%d",&B);
    scanf("%d",&C);
    scanf("%d",&a);
    scanf("%d",&b);
    scanf("%d",&c);

    for(int i=0; i<=C; ++i) turns[i]=-1;
    state s=state(a,b,c);
    curr_turn.push_back(s);
    int turn=0;

    while(curr_turn.size() > 0) {
       for(auto st : curr_turn) {
           if(turns[st.a]==-1) turns[st.a]=turn;
           if(turns[st.b]==-1) turns[st.b]=turn;
           if(turns[st.c]==-1) turns[st.c]=turn;

            map<state,bool>::const_iterator got=previous.find(st);
            if(got==previous.end()) previous[st]=true;
       }
       for(auto st : curr_turn ) {
          generate(st,A,B,C);
          for(auto sg : new_sts) {
              map<state,bool>::const_iterator got=previous.find(sg);
              if(got==previous.end()) next_turn.push_back(sg);
          }

       }
       turn++;
       curr_turn.clear();
       curr_turn=next_turn;
       next_turn.clear();
    }
    for(int i=0; i<=C; ++i) printf("%d ",turns[i]);
    printf("\n");
    //printf("%d\n",(int)previous.size());
    return 0;
}