Main.java 1.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748
  1. import java.io.*;
  2. import java.util.*;
  3. public class Main {
  4. public static void main(String[] args) throws IOException {
  5. BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
  6. PrintWriter out = new PrintWriter(new OutputStreamWriter(System.out));
  7. String s;
  8. int N, K;
  9. boolean[] set = new boolean[200001];
  10. Queue<Integer> queue = new LinkedList<Integer>();
  11. while ((s = in.readLine()) != null) {
  12. String[] nk = s.split("\\s+");
  13. N = Integer.parseInt(nk[0]);
  14. K = Integer.parseInt(nk[1]);
  15. Arrays.fill(set, 0, Math.max(N + 1, 2 * K + 1),false);
  16. queue.clear();
  17. queue.offer(N);
  18. set[N] = true;
  19. int cnt = 1, min = 0;
  20. while (cnt != 0) {
  21. int n = queue.poll();
  22. cnt--;
  23. if (n == K) break;
  24. if (n < K) {
  25. if (!set[2 * n]) {
  26. queue.offer(2 * n);
  27. set[2 * n] = true;
  28. }
  29. if (!set[n + 1]) {
  30. queue.offer(n + 1);
  31. set[n + 1] = true;
  32. }
  33. }
  34. if (0 < n && !set[n - 1]) {
  35. queue.offer(n - 1);
  36. set[n - 1] = true;
  37. }
  38. if (cnt == 0) {
  39. cnt = queue.size();
  40. min++;
  41. }
  42. }
  43. out.println(min);
  44. out.flush();
  45. }
  46. }
  47. }