#include #include #include #include using namespace std; pair next(set& con, vector& vec, unsigned long num) { if (num == 1) { return make_pair(0, false); } unsigned long res; bool done; if (num % 2) { res = num * 3 + 1; done = !con.insert(res).second; vec.push_back(res); } else { res = num / 2; done = !con.insert(res).second; vec.push_back(res); } return make_pair(res, done); } unsigned int pos(const vector& where, unsigned long num) { for (unsigned int i = 0; i < where.size(); i ++) { if (where[i] == num) return i; } return 0; } int main() { unsigned long A, B; cin >> A; cin >> B; while(A || B) { unsigned long at = A; unsigned long bat = 0; unsigned long aat = 0; set s; vector Avec; vector Bvec; unsigned long Alast = A; unsigned long Blast = B; s.insert(A); s.insert(B); Avec.push_back(A); Bvec.push_back(B); while (true && A != B) { pair tmp; if (Alast) { tmp = next(s, Avec, Alast); Alast = tmp.first; if (tmp.second) { aat = Avec.size() - 1; bat = pos(Bvec, Alast); at = Alast; break; } } if (Blast) { tmp = next(s, Bvec, Blast); Blast = tmp.first; if (tmp.second) { aat = pos(Avec, Blast); bat = Bvec.size() - 1; at = Blast; break; } } } cout << A << " needs " << aat << " steps, " << B << " needs " << bat << " steps, they meet at " << at << endl; //cout << A << " " << aat << " " << B << " " << bat << " " << at << " " << endl; cin >> A; cin >> B; } return 0; }