import java.io.BufferedReader; import java.io.InputStream; import java.io.InputStreamReader; import java.io.IOException; import java.io.PrintWriter; import java.util.StringTokenizer; import java.util.ArrayList; import java.math.BigInteger; public class slo{ /*Petr template*/ static class InputReader { public BufferedReader reader; public StringTokenizer tokenizer; public InputReader(InputStream stream) { reader = new BufferedReader(new InputStreamReader(stream), 32768); tokenizer = null; } public String next() { while (tokenizer == null || !tokenizer.hasMoreTokens()) { try { tokenizer = new StringTokenizer(reader.readLine()); } catch (IOException e) { throw new RuntimeException(e); } } return tokenizer.nextToken(); } public int nextInt() { return Integer.parseInt(next()); } } public static void main(String args[]){ InputReader reader = new InputReader(System.in); PrintWriter writer = new PrintWriter(System.out); int n =0; n = reader.nextInt(); BigInteger k = new BigInteger(reader.next()); ArrayList<BigInteger> values = new ArrayList<>(64); values.add(BigInteger.ONE); for(int i=0;i<64;++i){ values.add(values.get(i).add(values.get(i).add(BigInteger.ONE))); } if(n < 64){ BigInteger nos = values.get(n-1).multiply(BigInteger.valueOf(3l)); if(k.compareTo(nos) > 0){ writer.printf("NIE"); writer.flush(); writer.close(); return; } } int lastLetter = 1; int letters[] = {1,2,0,2,0,1}; if(n > 64){ long tmp = k.longValue(); while(n > 64){ lastLetter = (lastLetter + 1)%2; n--; tmp--; if(lastLetter == 0){ writer.print("a"); } else { writer.print("b"); } if(tmp == 0){ writer.flush(); writer.close(); return; } } k = BigInteger.valueOf(tmp); } else { n--; BigInteger aVal = values.get(n); BigInteger bVal = values.get(n).add(values.get(n)); lastLetter = 0; if(k.compareTo(aVal)<=0){ writer.print("a"); lastLetter = 0; k = k.subtract(BigInteger.ONE); } else if(k.compareTo(bVal)<=0){ writer.print("b"); k = k.subtract(aVal.add(BigInteger.ONE)); lastLetter = 1; } else { writer.print("c"); k = k.subtract(bVal.add(BigInteger.ONE)); lastLetter = 2; } } while(k.compareTo(BigInteger.ZERO) > 0){ n--; int id = 2*lastLetter; if(k.compareTo(values.get(n))>0){ lastLetter = letters[id + 1]; k = k.subtract(values.get(n).add(BigInteger.ONE)); } else { k = k.subtract(BigInteger.ONE); lastLetter = letters[id]; } if(lastLetter == 0){ writer.printf("a"); } if(lastLetter == 1){ writer.printf("b"); } if(lastLetter == 2){ writer.printf("c"); } } writer.flush(); writer.close(); } }
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 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 | import java.io.BufferedReader; import java.io.InputStream; import java.io.InputStreamReader; import java.io.IOException; import java.io.PrintWriter; import java.util.StringTokenizer; import java.util.ArrayList; import java.math.BigInteger; public class slo{ /*Petr template*/ static class InputReader { public BufferedReader reader; public StringTokenizer tokenizer; public InputReader(InputStream stream) { reader = new BufferedReader(new InputStreamReader(stream), 32768); tokenizer = null; } public String next() { while (tokenizer == null || !tokenizer.hasMoreTokens()) { try { tokenizer = new StringTokenizer(reader.readLine()); } catch (IOException e) { throw new RuntimeException(e); } } return tokenizer.nextToken(); } public int nextInt() { return Integer.parseInt(next()); } } public static void main(String args[]){ InputReader reader = new InputReader(System.in); PrintWriter writer = new PrintWriter(System.out); int n =0; n = reader.nextInt(); BigInteger k = new BigInteger(reader.next()); ArrayList<BigInteger> values = new ArrayList<>(64); values.add(BigInteger.ONE); for(int i=0;i<64;++i){ values.add(values.get(i).add(values.get(i).add(BigInteger.ONE))); } if(n < 64){ BigInteger nos = values.get(n-1).multiply(BigInteger.valueOf(3l)); if(k.compareTo(nos) > 0){ writer.printf("NIE"); writer.flush(); writer.close(); return; } } int lastLetter = 1; int letters[] = {1,2,0,2,0,1}; if(n > 64){ long tmp = k.longValue(); while(n > 64){ lastLetter = (lastLetter + 1)%2; n--; tmp--; if(lastLetter == 0){ writer.print("a"); } else { writer.print("b"); } if(tmp == 0){ writer.flush(); writer.close(); return; } } k = BigInteger.valueOf(tmp); } else { n--; BigInteger aVal = values.get(n); BigInteger bVal = values.get(n).add(values.get(n)); lastLetter = 0; if(k.compareTo(aVal)<=0){ writer.print("a"); lastLetter = 0; k = k.subtract(BigInteger.ONE); } else if(k.compareTo(bVal)<=0){ writer.print("b"); k = k.subtract(aVal.add(BigInteger.ONE)); lastLetter = 1; } else { writer.print("c"); k = k.subtract(bVal.add(BigInteger.ONE)); lastLetter = 2; } } while(k.compareTo(BigInteger.ZERO) > 0){ n--; int id = 2*lastLetter; if(k.compareTo(values.get(n))>0){ lastLetter = letters[id + 1]; k = k.subtract(values.get(n).add(BigInteger.ONE)); } else { k = k.subtract(BigInteger.ONE); lastLetter = letters[id]; } if(lastLetter == 0){ writer.printf("a"); } if(lastLetter == 1){ writer.printf("b"); } if(lastLetter == 2){ writer.printf("c"); } } writer.flush(); writer.close(); } } |