File tree Expand file tree Collapse file tree 1 file changed +62
-0
lines changed
Expand file tree Collapse file tree 1 file changed +62
-0
lines changed Original file line number Diff line number Diff line change 1+ ``` java
2+ import java.io.* ;
3+ import java.util.* ;
4+
5+ public class Main {
6+ static int [] time = new int [100001 ];
7+ static int [] count = new int [100001 ];
8+ static int n, k;
9+
10+ public static void main (String [] args ) throws Exception {
11+ BufferedReader br = new BufferedReader (new InputStreamReader (System . in));
12+ StringTokenizer st = new StringTokenizer (br. readLine());
13+
14+ n = Integer . parseInt(st. nextToken());
15+ k = Integer . parseInt(st. nextToken());
16+
17+ if (n == k) {
18+ System . out. println(0 );
19+ System . out. println(1 );
20+ return ;
21+ }
22+
23+ Queue<Integer > q = new LinkedList<> ();
24+ q. add(n);
25+ time[n] = 0 ;
26+ count[n] = 1 ;
27+
28+ while (! q. isEmpty()) {
29+ int cur = q. poll();
30+
31+ for (int j = 0 ; j < 3 ; j++ ) {
32+ int next = 0 ;
33+ switch (j) {
34+ case 0 :
35+ next = cur - 1 ;
36+ break ;
37+ case 1 :
38+ next = cur + 1 ;
39+ break ;
40+ case 2 :
41+ next = cur * 2 ;
42+ break ;
43+ }
44+
45+ if (next < 0 || next > 100000 ) continue ;
46+
47+ if (time[next] == 0 && next != n) {
48+ time[next] = time[cur] + 1 ;
49+ count[next] = count[cur];
50+ q. add(next);
51+ } else if (time[next] == time[cur] + 1 ) {
52+ count[next] += count[cur];
53+ }
54+ }
55+ }
56+
57+ System . out. println(time[k]);
58+ System . out. println(count[k]);
59+ }
60+ }
61+
62+ ```
You can’t perform that action at this time.
0 commit comments