File tree Expand file tree Collapse file tree 3 files changed +55
-0
lines changed
Expand file tree Collapse file tree 3 files changed +55
-0
lines changed Original file line number Diff line number Diff line change 1+ <?xml version =" 1.0" encoding =" UTF-8" ?>
2+ <module type =" JAVA_MODULE" version =" 4" >
3+ <component name =" NewModuleRootManager" inherit-compiler-output =" true" >
4+ <exclude-output />
5+ <content url =" file://$MODULE_DIR$" >
6+ <sourceFolder url =" file://$MODULE_DIR$/src" isTestSource =" false" />
7+ </content >
8+ <orderEntry type =" inheritedJdk" />
9+ <orderEntry type =" sourceFolder" forTests =" false" />
10+ </component >
11+ </module >
Original file line number Diff line number Diff line change 1+ import java .io .BufferedReader ;
2+ import java .io .IOException ;
3+ import java .io .InputStreamReader ;
4+ import java .util .*;
5+
6+ public class Main {
7+ static int minV = Integer .MAX_VALUE ;
8+ public static void main (String [] args ) throws IOException {
9+ BufferedReader br =new BufferedReader (new InputStreamReader (System .in ));
10+ StringTokenizer st =new StringTokenizer (br .readLine ());
11+ long N = Integer .parseInt (st .nextToken ());
12+ long K = Integer .parseInt (st .nextToken ());
13+
14+ BFSFindSUM (N , K );
15+ if (minV ==Integer .MAX_VALUE ){
16+ System .out .println (-1 );
17+ }else {
18+ System .out .println (minV );
19+ }
20+
21+
22+ }
23+
24+ private static void BFSFindSUM (Long n , Long k ) {
25+ Deque <long []> dq = new LinkedList <>();
26+ dq .offer (new long []{n , 1 });
27+ while (!dq .isEmpty ()) {
28+ long [] now = dq .pollFirst ();
29+ long value = now [0 ];
30+ long count = now [1 ];
31+ if (value ==k ){
32+ minV = (int ) Math .min (minV ,count );
33+ }
34+ if (value <= k ) {
35+ dq .offer (new long []{value * 2 , count + 1 });
36+ dq .offer (new long []{Long .valueOf (String .valueOf (value ) +"1" ), count + 1 });
37+ }
38+
39+
40+ }
41+
42+ }
43+ }
You can’t perform that action at this time.
0 commit comments