#include #include #include using namespace std; int ptr = 0; struct comb { int num; int iter; }; int compare(const void * a, const void * b) { return( (int)(*(struct comb *)a).num - (int)(*(struct comb *)b).num); } int compareMyType(const void * a, const void * b) { if( (int)(*(struct comb*)a).num > (int)(*(struct comb*)b).num) return 1; if( (int)(*(struct comb*)a).num == (int)(*(struct comb*)b).num) return 0; if( (int)(*(struct comb*)a).num < (int)(*(struct comb*)b).num) return -1; return 0; //never reached } inline void next(struct comb& in) { if(in.num % 2 == 0) { in.num = in.num / 2; } else { in.num = 3 * in.num + 1; } in.iter++; } int main() { int x1; int x2; int vysl1; //int vysl2; int orig1; int orig2; struct comb mnozina[128000]; int swap = 0; struct comb * cptr; while(1) { ptr = 0; struct comb test; test.iter = 0; scanf("%d %d", &x1, &x2); orig1 = x1; orig2 = x2; if((x1 == 0) && (x2 == 0)) { break; } if(x1*3 <= x2/2) { test.num = x1; } else { test.num = x2; swap = 1; } while(test.num != 1) { mnozina[ptr] = test; ptr++; next(test); } if(swap == 1) { test.num = x1; } else { test.num = x2; } test.iter = 0; // printf("faza 2\n"); qsort(mnozina, ptr, sizeof(struct comb), compare); while(1) { cptr = (struct comb *) bsearch((void *)&test.num, (void *)mnozina, ptr, sizeof(struct comb), compareMyType); if(cptr != NULL) { vysl1 = (int)(*(struct comb *)cptr).iter; goto end; } next(test); } end: if(swap == 1) { printf("%d needs %d steps, %d needs %d steps, they meet at %d\n", x1, test.iter, x2, vysl1, test.num); } else { printf("%d needs %d steps, %d needs %d steps, they meet at %d\n", x1, vysl1, x2, test.iter, test.num); } } return 0; }