File tree 3 files changed +91
-0
lines changed
A Rational Sequence (Take 3)
3 files changed +91
-0
lines changed Original file line number Diff line number Diff line change
1
+ import java .util .Scanner ;
2
+
3
+ public class Main2 {
4
+ public static void main (String [] args ) {
5
+ Scanner scanner = new Scanner (System .in );
6
+ int P = scanner .nextInt ();
7
+ for (int i = 0 ; i < P ; i ++) {
8
+ int j = scanner .nextInt ();
9
+ long n = scanner .nextLong ();
10
+ System .out .print (j +" " );
11
+ getN (n );
12
+ }
13
+ }
14
+
15
+ private static void getN (long i ) {
16
+ String str = Long .toBinaryString (i );
17
+ int p = 1 ,q = 1 ;
18
+ for (int j = 1 ; j < str .length (); j ++) {
19
+ if (str .charAt (j ) == '0' ){
20
+ //go left:
21
+ q = p + q ;
22
+ }else {
23
+ //go right:
24
+ p = p + q ;
25
+ }
26
+ }
27
+ System .out .println (p +"/" +q );
28
+ }
29
+ }
Original file line number Diff line number Diff line change
1
+ import java .math .BigInteger ;
2
+ import java .util .Scanner ;
3
+
4
+ public class Mine {
5
+ private static String ans = "" ;
6
+
7
+ public static void main (String [] args ) {
8
+ Scanner scanner = new Scanner (System .in );
9
+ int P = scanner .nextInt ();
10
+ for (int i = 0 ; i < P ; i ++) {
11
+ scanner .nextInt ();
12
+ String s = scanner .next ();
13
+ int p = Integer .parseInt (s .split ("/" )[0 ]);
14
+ int q = Integer .parseInt (s .split ("/" )[1 ]);
15
+ System .out .print (i + 1 + " " );
16
+ Calc (p , q , 1 );
17
+ ans = "1" + ans ;
18
+ System .out .println (new BigInteger (ans , 2 ).longValue ());
19
+ ans = "" ;
20
+ }
21
+ }
22
+
23
+ private static int Calc (int p , int q , int i ) {
24
+ if (p == 1 && q == 1 ) {
25
+ return i ;
26
+ }
27
+ if (p > q ) {
28
+ // right child:
29
+ ans = "1" + ans ;
30
+ return Calc (p - q , q , i + 1 );
31
+ } else {
32
+ // left child:
33
+ ans = "0" + ans ;
34
+ return Calc (p , q - p , i + 1 );
35
+ }
36
+ }
37
+
38
+ }
Original file line number Diff line number Diff line change
1
+ import java .util .Collections ;
2
+ import java .util .Scanner ;
3
+
4
+ public class Main3 {
5
+ public static void main (String [] args ) {
6
+ Scanner scanner = new Scanner (System .in );
7
+ String line = scanner .nextLine ();
8
+ int h = Integer .parseInt (line .split (" " )[0 ]);
9
+ String instructions = "" ;
10
+ if (line .split (" " ).length > 1 ) {
11
+ instructions = line .split (" " )[1 ];
12
+ }
13
+ long pivot = 1 ;
14
+ for (int i = 0 ; i < instructions .length (); i ++) {
15
+ if (instructions .charAt (i ) == 'L' ){
16
+ pivot = 2 *pivot ;
17
+ }else if (instructions .charAt (i ) == 'R' ){
18
+ pivot = 2 *pivot + 1 ;
19
+ }
20
+ }
21
+ long ans = (((long )Math .pow (2 ,h +1 )) - pivot );
22
+ System .out .print (ans );
23
+ }
24
+ }
You can’t perform that action at this time.
0 commit comments