-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMain.java
More file actions
43 lines (35 loc) · 1.21 KB
/
Main.java
File metadata and controls
43 lines (35 loc) · 1.21 KB
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
33
34
35
36
37
38
39
40
41
42
43
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.*;
public class Main {
static int minV= Integer.MAX_VALUE;
public static void main(String[] args) throws IOException {
BufferedReader br =new BufferedReader(new InputStreamReader(System.in));
StringTokenizer st =new StringTokenizer(br.readLine());
long N = Integer.parseInt(st.nextToken());
long K = Integer.parseInt(st.nextToken());
BFSFindSUM(N, K);
if (minV==Integer.MAX_VALUE){
System.out.println(-1);
}else {
System.out.println(minV);
}
}
private static void BFSFindSUM(Long n, Long k) {
Deque<long[]> dq = new LinkedList<>();
dq.offer(new long[]{n, 1});
while (!dq.isEmpty()) {
long [] now = dq.pollFirst();
long value = now[0];
long count = now[1];
if (value==k){
minV= (int) Math.min(minV,count);
}
if (value <= k) {
dq.offer(new long[]{value * 2, count + 1});
dq.offer(new long[]{Long.valueOf(String.valueOf(value) +"1"), count + 1});
}
}
}
}