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
#include <iostream>
#include <vector>
#include <queue>
#include <tuple>
#include <set>

using namespace std;

typedef pair<int, int> P;
typedef pair<P, int> T;
struct State : public T {
	State(int a, int b, int c, int N) : T{P{a,b},c}, cnt{N} {}
	int cnt;
	int a() const { return first.first; }
	int b() const { return first.second; }
	int c() const { return second; }
};

int main() {
	ios_base::sync_with_stdio(false);

	int A, B, C;
	cin >> A;
	cin >> B;
	cin >> C;
	int a, b, c;
	cin >> a;
	cin >> b;
	cin >> c;

	vector<int> v(C+1, -1);
	set<T> s;
	queue<State> q;

	auto xfer = [](int a, int B, int b) {
		if (B - b >= a) return a;
		else return (B - b);
	};

	int globalcnt = 0;

	q.push(State(a, b, c, 0));
	while (!q.empty() && globalcnt < C+1) {
		auto check = [&v, &globalcnt](int a, int cnt) {
			if (v[a] == -1) {
				v[a] = cnt;
				globalcnt++;
			}
		};

		auto next = q.front();
		check(next.a(), next.cnt);
		check(next.b(), next.cnt);
		check(next.c(), next.cnt);
		q.pop();

		auto handle = [&s, &q](const State &old, int la, int lb, int lc) {
			State nstate{old.a()+la, old.b()+lb, old.c()+lc, old.cnt+1};
			auto it = s.find(nstate);
			if (it == s.end()) {
				q.push(nstate);
				s.insert(nstate);
			}
		};

		int l = xfer(next.a(), B, next.b());
		handle(next, -l, l, 0);
		l = xfer(next.a(), C, next.c());
		handle(next, -l, 0, l);
		l = xfer(next.b(), A, next.a());
		handle(next, l, -l, 0);
		l = xfer(next.b(), C, next.c());
		handle(next, 0, -l, l);
		l = xfer(next.c(), A, next.a());
		handle(next, l, 0, -l);
		l = xfer(next.c(), B, next.b());
		handle(next, 0, l, -l);
	}

	for (auto i: v)
		cout << i << " ";

	cout << endl;

	return 0;
}