#include <cstdio> #include <queue> #include <map> using namespace std; const int N = 100000 + 5; int A, B, C; int todo; int res[N]; struct State { int a; int b; int c; bool operator<(const State &oth) const { if (a != oth.a) return a < oth.a; if (b != oth.b) return b < oth.b; return c < oth.c; } }; int NWD(int a, int b) { if (a < b) std::swap(a, b); if (b == 0) return a; return NWD(b, a % b); } int NWD(int a, int b, int c, int d, int e, int f) { return NWD(NWD(a, b), NWD(NWD(c, d), NWD(e, f))); } void addRes(int i, int cnt) { if (res[i] >= 0) return; res[i] = cnt; --todo; } bool moveXY(int &x, int &y, int &X, int &Y) { if (x == 0) return false; if (y == Y) return false; int cnt = x; if (y + cnt > Y) cnt = Y - y; x -= cnt; y += cnt; return true; } int main() { scanf("%d%d%d", &A, &B, &C); for (int i = 0; i <= C; ++i) res[i] = -1; queue<State> q; map<State, int> dist; int a, b, c; scanf("%d%d%d", &a, &b, &c); todo = C / NWD(A, B, C, a, b, c) + 1; q.push({ a, b, c }); dist[{a, b, c}] = 0; addRes(a, 0); addRes(b, 0); addRes(c, 0); while (todo > 0 && !q.empty()) { int a = q.front().a; int b = q.front().b; int c = q.front().c; int d = dist[q.front()] + 1; q.pop(); int aa, bb, cc; aa = a; bb = b; cc = c; if (moveXY(aa, bb, A, B)) { State s{ aa, bb, cc }; if (dist.count(s) == 0) { //fprintf(stderr, "move: %d %d %d => %d %d %d\n", a, b, c, aa, bb, cc); addRes(aa, d); addRes(bb, d); q.push(s); dist[s] = d; } } aa = a; bb = b; cc = c; if (moveXY(bb, aa, B, A)) { State s{ aa, bb, cc }; if (dist.count(s) == 0) { //fprintf(stderr, "move: %d %d %d => %d %d %d\n", a, b, c, aa, bb, cc); addRes(aa, d); addRes(bb, d); q.push(s); dist[s] = d; } } aa = a; bb = b; cc = c; if (moveXY(aa, cc, A, C)) { State s{ aa, bb, cc }; if (dist.count(s) == 0) { //fprintf(stderr, "move: %d %d %d => %d %d %d\n", a, b, c, aa, bb, cc); addRes(aa, d); addRes(cc, d); q.push(s); dist[s] = d; } } aa = a; bb = b; cc = c; if (moveXY(cc, aa, C, A)) { State s{ aa, bb, cc }; if (dist.count(s) == 0) { //fprintf(stderr, "move: %d %d %d => %d %d %d\n", a, b, c, aa, bb, cc); addRes(aa, d); addRes(cc, d); q.push(s); dist[s] = d; } } aa = a; bb = b; cc = c; if (moveXY(bb, cc, B, C)) { State s{ aa, bb, cc }; if (dist.count(s) == 0) { //fprintf(stderr, "move: %d %d %d => %d %d %d\n", a, b, c, aa, bb, cc); addRes(bb, d); addRes(cc, d); q.push(s); dist[s] = d; } } aa = a; bb = b; cc = c; if (moveXY(cc, bb, C, B)) { State s{ aa, bb, cc }; if (dist.count(s) == 0) { //fprintf(stderr, "move: %d %d %d => %d %d %d\n", a, b, c, aa, bb, cc); addRes(bb, d); addRes(cc, d); q.push(s); dist[s] = d; } } } for (int i = 0; i < C; ++i) printf("%d ", res[i]); printf("%d\n", res[C]); return 0; }
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 | #include <cstdio> #include <queue> #include <map> using namespace std; const int N = 100000 + 5; int A, B, C; int todo; int res[N]; struct State { int a; int b; int c; bool operator<(const State &oth) const { if (a != oth.a) return a < oth.a; if (b != oth.b) return b < oth.b; return c < oth.c; } }; int NWD(int a, int b) { if (a < b) std::swap(a, b); if (b == 0) return a; return NWD(b, a % b); } int NWD(int a, int b, int c, int d, int e, int f) { return NWD(NWD(a, b), NWD(NWD(c, d), NWD(e, f))); } void addRes(int i, int cnt) { if (res[i] >= 0) return; res[i] = cnt; --todo; } bool moveXY(int &x, int &y, int &X, int &Y) { if (x == 0) return false; if (y == Y) return false; int cnt = x; if (y + cnt > Y) cnt = Y - y; x -= cnt; y += cnt; return true; } int main() { scanf("%d%d%d", &A, &B, &C); for (int i = 0; i <= C; ++i) res[i] = -1; queue<State> q; map<State, int> dist; int a, b, c; scanf("%d%d%d", &a, &b, &c); todo = C / NWD(A, B, C, a, b, c) + 1; q.push({ a, b, c }); dist[{a, b, c}] = 0; addRes(a, 0); addRes(b, 0); addRes(c, 0); while (todo > 0 && !q.empty()) { int a = q.front().a; int b = q.front().b; int c = q.front().c; int d = dist[q.front()] + 1; q.pop(); int aa, bb, cc; aa = a; bb = b; cc = c; if (moveXY(aa, bb, A, B)) { State s{ aa, bb, cc }; if (dist.count(s) == 0) { //fprintf(stderr, "move: %d %d %d => %d %d %d\n", a, b, c, aa, bb, cc); addRes(aa, d); addRes(bb, d); q.push(s); dist[s] = d; } } aa = a; bb = b; cc = c; if (moveXY(bb, aa, B, A)) { State s{ aa, bb, cc }; if (dist.count(s) == 0) { //fprintf(stderr, "move: %d %d %d => %d %d %d\n", a, b, c, aa, bb, cc); addRes(aa, d); addRes(bb, d); q.push(s); dist[s] = d; } } aa = a; bb = b; cc = c; if (moveXY(aa, cc, A, C)) { State s{ aa, bb, cc }; if (dist.count(s) == 0) { //fprintf(stderr, "move: %d %d %d => %d %d %d\n", a, b, c, aa, bb, cc); addRes(aa, d); addRes(cc, d); q.push(s); dist[s] = d; } } aa = a; bb = b; cc = c; if (moveXY(cc, aa, C, A)) { State s{ aa, bb, cc }; if (dist.count(s) == 0) { //fprintf(stderr, "move: %d %d %d => %d %d %d\n", a, b, c, aa, bb, cc); addRes(aa, d); addRes(cc, d); q.push(s); dist[s] = d; } } aa = a; bb = b; cc = c; if (moveXY(bb, cc, B, C)) { State s{ aa, bb, cc }; if (dist.count(s) == 0) { //fprintf(stderr, "move: %d %d %d => %d %d %d\n", a, b, c, aa, bb, cc); addRes(bb, d); addRes(cc, d); q.push(s); dist[s] = d; } } aa = a; bb = b; cc = c; if (moveXY(cc, bb, C, B)) { State s{ aa, bb, cc }; if (dist.count(s) == 0) { //fprintf(stderr, "move: %d %d %d => %d %d %d\n", a, b, c, aa, bb, cc); addRes(bb, d); addRes(cc, d); q.push(s); dist[s] = d; } } } for (int i = 0; i < C; ++i) printf("%d ", res[i]); printf("%d\n", res[C]); return 0; } |