/* * To change this template, choose Tools | Templates * and open the template in the editor. */ import java.io.*; import java.util.Arrays; import java.util.ArrayList; /** * * @author cteam025 */ public class collatz { public static final int MAX = 1000000*100+1;//(3*1000000+1+1)*80; /** * @param args the command line arguments */ public static void main(String[] args) throws IOException{ BufferedInputStream br = new BufferedInputStream(System.in); int first, second; boolean arraA[] = new boolean[MAX]; boolean arraB[] = new boolean[MAX]; int fi, se, oldfi, oldse; boolean isfi; ArrayList lfi = new ArrayList(), lse = new ArrayList(); while(((first = readInt(br)) != 0) && ((second = readInt(br)) != 0)) { Arrays.fill(arraA, false); Arrays.fill(arraB, false); lfi.clear();lse.clear(); fi = first; se = second; isfi = false; // while(!(arraA[fi] && arraB[fi]) || !(arraA[se] && arraB[se])) { // if(!arraB[se]) lse.add(new Integer(se)); // if(!arraA[fi]) lfi.add(new Integer(fi)); // arraA[fi] = true; arraB[se] = true; // fi = next(fi); se = next(se); // } while(true) { arraA[fi] = true; arraB[se] = true; if(arraA[fi] && arraB[fi]) {isfi = true; break;} if(arraA[se] && arraB[se]) break; lfi.add(new Integer(fi)); lse.add(new Integer(se)); oldfi = fi; oldse = se; fi = next(fi); se = next(se); } lfi.add(new Integer(fi)); lse.add(new Integer(se)); int loa, lob, out; if(isfi) { loa = find(lfi, fi); lob = find(lse, fi); out = fi; } else { loa = find(lfi, se); lob = find(lse, se); out = se; } //print(lfi); //print(lse); //print(arraA); print(arraB); //System.out.println(lfi.get(loa)); //System.out.println(lse.get(lob)); System.out.println(first + " needs " + loa + " steps, " + second + " needs " + lob + " steps, they meet at " + out); } } public static int find(ArrayList l, int val) { int i = 0; for (Object object : l) { if(val == (((Integer) object).intValue())) return i; i++; } return -1; } public static void print(ArrayList l) { for (Object object : l) { System.out.print(object.toString() + " "); } System.out.println(""); } public static void print(boolean[] arr) { int i = 0; for (boolean val: arr) { if(val) System.out.print(i+" "); i++; } System.out.println(""); } public static int next(int i) { if(i % 2 == 0) return i/2; return 3*i + 1; } public static int readInt(InputStream in) throws IOException { int i = 0, j; while((j = in.read()) != '\n') { if(j == '\r') continue; if(j == ' ') break; i = (((i << 2) + i) << 1) + (j - 48); } //System.out.println(i); return i; } }