123456789101112131415161718192021222324252627282930313233343536373839404142434445464748 |
- import java.io.*;
- import java.util.*;
- public class Main {
- public static void main(String[] args) throws IOException {
- BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
- PrintWriter out = new PrintWriter(new OutputStreamWriter(System.out));
- String s;
- int N, K;
- boolean[] set = new boolean[200001];
- Queue<Integer> queue = new LinkedList<Integer>();
- while ((s = in.readLine()) != null) {
- String[] nk = s.split("\\s+");
- N = Integer.parseInt(nk[0]);
- K = Integer.parseInt(nk[1]);
- Arrays.fill(set, 0, Math.max(N + 1, 2 * K + 1),false);
- queue.clear();
- queue.offer(N);
- set[N] = true;
- int cnt = 1, min = 0;
- while (cnt != 0) {
- int n = queue.poll();
- cnt--;
- if (n == K) break;
- if (n < K) {
- if (!set[2 * n]) {
- queue.offer(2 * n);
- set[2 * n] = true;
- }
- if (!set[n + 1]) {
- queue.offer(n + 1);
- set[n + 1] = true;
- }
- }
- if (0 < n && !set[n - 1]) {
- queue.offer(n - 1);
- set[n - 1] = true;
- }
- if (cnt == 0) {
- cnt = queue.size();
- min++;
- }
- }
- out.println(min);
- out.flush();
- }
- }
- }
|