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
#include<bits/stdc++.h>
using namespace std;
int main(){
  int A, B, C = 0, a, b, c, dist;
  cin >> A >> B >> C >> a >> b >> c;
  vector<int> res(C + 1, -1), v, full = {A, B, C}, initial = {a, b, c};
  
  auto make_hasz = [](vector<int> &v){
    return ((long long)(v[0]) << 38) + ((long long)(v[1]) << 19) + (long long)(v[0]);
  };

  map<long long, bool> visited;
  queue<pair<vector<int>, int>> q;
  q.emplace(initial, 0);
  visited[make_hasz(initial)] = 1;
  while(!q.empty()){
    tie(v, dist) = q.front();
    q.pop();
    if(v[0] <= C && res[v[0]] == -1) res[v[0]] = dist;
    if(v[1] <= C && res[v[1]] == -1) res[v[1]] = dist;
    if(v[2] <= C && res[v[2]] == -1) res[v[2]] = dist;
    
    for(int i = 0;i < 3;i++){
      for(int j = 0;j < 3;j++){
        if (v[i] == 0 || i == j) continue;
        if (v[i] <= full[j] - v[j]){
          vector<int> u = v;
          u[i] = 0;
          u[j] += v[i];
          long long tmp = make_hasz(u);
          if(!visited[tmp]){
            q.emplace(u, dist + 1);
            visited[tmp] = true;
          }
        }
        if (v[i] > full[j] - v[j] && full[j] > v[j]){
          vector<int> u = v;
          u[i] -= full[j] - v[j];
          u[j] = full[j];
          long long tmp = make_hasz(u);
          if(!visited[tmp]){
            q.emplace(u, dist + 1);
            visited[tmp] = true;
          }
        }
      }
    }
  }

  for(auto i : res) cout << i << ' ';
}