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
| public class boj_16953 { static long a; static long b; static long answer; static boolean isCheck; public static void main(String[] args) throws IOException { BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); StringTokenizer st = new StringTokenizer(br.readLine());
answer = Long.MAX_VALUE; a = Long.parseLong(st.nextToken()); b = Long.parseLong(st.nextToken()); isCheck = false; dfs(a,1); if(!isCheck) System.out.println(-1); else System.out.println(answer); }
private static void dfs(long a, int cnt) { if(a > b){ return; } if(a == b){ answer = Math.min(answer, cnt); isCheck = true; return; } dfs(a * 2,cnt+1); dfs(a * 10 + 1, cnt+1); } }
|