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
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
#include <cstdio>
#include <cassert>
#include <vector>
#include <tuple>
#include <unordered_set>
#include <queue>
#include <chrono>

using namespace std;
using namespace std::chrono;

const int n = 3;
const int big_p = 99991;

const unsigned int magic_number = 9999991;

unsigned int modulo;

struct hash_tuple {
        size_t operator()(const pair<int, int>& pr) const
        {
                return (int) (((pr.first+1) * big_p +  pr.second) % big_p);
        }
};

int c[n];
vector<int> rstab;
int nresult;
//unordered_set<pair<int,int>, hash_tuple> set;

//char* sss;

vector<pair<int,int>> hasht;

int pair00 = 0;

unsigned int hashf(pair<int,int> pr, int i)
{
        return ((pr.first+1) * i * i + (pr.second+1) * (i+1)) % modulo;
}

int hash_find(pair<int,int> pr)
{
        unsigned int h = hashf(pr, 3);
        pair<int,int> found;
        int i = 3;

        if (pr.first == 0 && pr.second == 0) {
                return pair00;
        }

        while(1) {
                found = hasht[h];
                if (found.first == 0 && found.second == 0) 
                        return 0;
                if (found.first == pr.first && found.second == pr.second) {
                        return 1;
                }
                i++;
                h = hashf(pr, i);
        }
}

void hash_insert(pair<int,int> pr)
{
        unsigned int h = hashf(pr, 3);
        pair<int,int> found;
        int i = 3;

        if (pr.first == 0 && pr.second == 0) {
                pair00 = 1;
                return;
        }

        while(1) {
                found = hasht[h];
                if (found.first == 0 && found.second == 0) {
                        hasht[h] = pr;
                        return;
                }
                i++;
                h = hashf(pr, i);
        }
}


typedef struct state {
        int st[n];
        long long len;
} tstate;

queue<tstate> q;

//long check = 0;
//unsigned long long time_count = 0;
//unsigned long long time_insert = 0;

void print_rstab()
{
        for(int i = 0; i <= c[2]; ++i) {
//                printf("   %d(%d)", i, rstab[i]);
                printf(" %d", rstab[i]);
        }
        printf("\n");
}

bool set_result(tstate s)
{
        for(int i = 0; i < n; ++i) {
                if (rstab[ s.st[i] ] == -1) {
                        nresult++;
                        rstab[ s.st[i] ] = s.len;
//                        printf("found %d at len %lld\n", s.st[i], s.len);
                }
        }

        return (nresult == c[2]+1);
}


tstate next_state(tstate s, int a, int b, int len)
{
        s.len = len;
        if (s.st[a] + s.st[b] <= c[b]) {
                s.st[b] = s.st[a] + s.st[b];
                s.st[a] = 0;
        } else {
                s.st[a] = s.st[a] - (c[b] - s.st[b]);
                s.st[b] = c[b];
        }
        return s;
}

bool state_seen_before(tstate s)
{
        auto pr = make_pair(s.st[0], s.st[1]);
        unsigned long size;
//         char size;
//        time_point<high_resolution_clock> t1,t2,t3,t4;

//        t1 = high_resolution_clock::now();
        size = hash_find(pr);
        //size = set.count(pr);
        //size = sss[s.st[0]*(c[1]+1) + s.st[1]];
//        t2 = high_resolution_clock::now();
//        auto d1 = duration_cast<microseconds>(t2 - t1);
//        time_count += d1.count();
        if (size == 0) {
//                t3 = high_resolution_clock::now();
                hash_insert(pr);
//                set.insert(pr);
//                sss[s.st[0]*(c[1]+1) + s.st[1]] = 1;
//                t4 = high_resolution_clock::now();
//                auto d2 = duration_cast<microseconds>(t4 - t3);
//                time_insert += d2.count();
                return false;
        }
        return true;
}

void print_s(tstate s)
{
        for(int i = 0; i < n; ++i) {
                printf(" %c(%d)", 'A'+i, s.st[i]);
        }
        printf("\n");
}

void calc()
{
        tstate s;

        nresult = 0;
        while (!q.empty()) {
                s = q.front();
                q.pop();
  //              print_s(s);
                if (set_result(s)) {
                        return;
                }
                /* gen next possible states */
                for(int i = 0; i < n; ++i) {
                        if (s.st[i] > 0) {
                                for(int j = 0; j < n; ++j) {
                                        if (i == j) {
                                                continue;
                                        }
                                        if (s.st[j] < c[j]) {
                                                /* pour i -> j */
                                                tstate sn = next_state(s, i, j, s.len+1);
                                                if (!state_seen_before(sn)) {
                                                        q.push(sn);
                                                }
                                                
                                        }
                                }
                        }
                }
        }
}

int main()
{
        tstate s;

        for(int i=0; i < n; ++i) {
                scanf("%d", c+i);
        }
        for(int i=0; i <= c[2]; ++i) {
                rstab.push_back(-1);
        }
        for(int i=0; i < n; ++i) {
                scanf("%d", s.st+i);
                //rstab[ st[i] ] = 0;
        }

        //sss = new char[(c[0]+1)*(c[1]+1)];
        //for (long long i = 0; i < (c[0]+1)*(c[1]+1); ++i) {
        //        sss[i] = 0;
        //}
        
        if ((c[0]+1) * (c[1]+1) < magic_number) {
                modulo = (c[0]+1) * (c[1]+1) + 3;
        } else {
                modulo = magic_number;
        }


        hasht.resize(modulo);
//       for(int i = 0; i < magic_number; ++i) {
//                hasht.push_back( make_pair(-1,-1) );
//        }


        s.len = 0;
        q.push(s);
        calc();

        print_rstab();

        //printf("set size %lu, check %ld\n", set.size(), check);
//        printf("set.count()  time %d:%d\n", time_count/1000000, (time_count%1000000)/1000);
//        printf("set.insert() time %d:%d\n", time_insert/1000000, (time_insert%1000000)/1000);
 
        return 0;
}