import java.io.BufferedWriter; import java.io.IOException; import java.io.OutputStreamWriter; import java.math.BigInteger; import java.util.Scanner; public class slo { private static BigInteger L,K,L3,L32,LeftCount,h,start,end; private static int n; private static char c; private static BufferedWriter log = new BufferedWriter(new OutputStreamWriter(System.out)); public static void main(String[] args) throws IOException { Scanner sc=new Scanner(System.in); n=sc.nextInt(); K=BigInteger.valueOf(sc.nextLong()); L3=BigInteger.ONE.shiftLeft(n).subtract(BigInteger.ONE); L32=L3.shiftLeft(1); L=L3.add(L32); if (L.subtract(K).signum()>=0) { if (L3.compareTo(K)>=0){ c='a'; }else if (L32.compareTo(K)>=0){ K=K.subtract(L3); c='b'; }else{ K=K.subtract(L32); c='c'; } start=BigInteger.ONE; end=L3; SearchTree(); log.flush(); } else { System.out.println("NIE"); } sc.close(); } private static void SearchTree() throws IOException{ log.append(c); if (K.compareTo(start)!=0) { LeftCount=end.subtract(start).shiftRight(1); h=start.add(LeftCount); if (K.compareTo(h)>0) { c=getWorseChar(c); start=h.add(BigInteger.ONE); SearchTree(); } else { c=getBetterChar(c); start=start.add(BigInteger.ONE); end=h; SearchTree(); } } } private static char getBetterChar(char c){ return c=='a'?'b':'a'; } private static char getWorseChar(char c){ return c=='c'?'b':'c'; } }
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 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 | import java.io.BufferedWriter; import java.io.IOException; import java.io.OutputStreamWriter; import java.math.BigInteger; import java.util.Scanner; public class slo { private static BigInteger L,K,L3,L32,LeftCount,h,start,end; private static int n; private static char c; private static BufferedWriter log = new BufferedWriter(new OutputStreamWriter(System.out)); public static void main(String[] args) throws IOException { Scanner sc=new Scanner(System.in); n=sc.nextInt(); K=BigInteger.valueOf(sc.nextLong()); L3=BigInteger.ONE.shiftLeft(n).subtract(BigInteger.ONE); L32=L3.shiftLeft(1); L=L3.add(L32); if (L.subtract(K).signum()>=0) { if (L3.compareTo(K)>=0){ c='a'; }else if (L32.compareTo(K)>=0){ K=K.subtract(L3); c='b'; }else{ K=K.subtract(L32); c='c'; } start=BigInteger.ONE; end=L3; SearchTree(); log.flush(); } else { System.out.println("NIE"); } sc.close(); } private static void SearchTree() throws IOException{ log.append(c); if (K.compareTo(start)!=0) { LeftCount=end.subtract(start).shiftRight(1); h=start.add(LeftCount); if (K.compareTo(h)>0) { c=getWorseChar(c); start=h.add(BigInteger.ONE); SearchTree(); } else { c=getBetterChar(c); start=start.add(BigInteger.ONE); end=h; SearchTree(); } } } private static char getBetterChar(char c){ return c=='a'?'b':'a'; } private static char getWorseChar(char c){ return c=='c'?'b':'c'; } } |