#include #include #include #define ARRAYSIZE 8000000 int a, b, as, bs, i; int *mapA; int *mapB; int main() { mapA = (int *) malloc(sizeof (int) * ARRAYSIZE); mapB = (int *) malloc(sizeof (int) * ARRAYSIZE); while (1) { int counter = 1; scanf("%d %d", &as, &bs); int aend = 0; int bend = 0; a = as; b = bs; if (a == 0 && b == 0) break; if (a == b) { printf("%d needs %d steps, %d needs %d steps, they meet at %d\n", as, 0, bs, 0, a); continue; } for (i = 0; i < ARRAYSIZE; ++i) { mapA[i] = 0; mapB[i] = 0; } int h = 1; while (h == 1) { if (!aend) { mapA[a] = counter; if (a % 2 == 0) a /= 2; else a = 3 * a + 1; } if (a == 1) aend = 1; if (!bend) { mapB[b] = counter; if (b % 2 == 0) b /= 2; else b = 3 * b + 1; } if (b == 1) bend = 1; if (mapA[b] != 0) { printf("%d needs %d steps, %d needs %d steps, they meet at %d\n", as, mapA[b] - 1, bs, counter, b); break; } else if (mapB[a] != 0) { printf("%d needs %d steps, %d needs %d steps, they meet at %d\n", as, counter, bs, mapB[a] - 1, a); break; } ++counter; } } }