import java.util.Scanner; class slo { public static char[] chars = new char[]{'a', 'b', 'c'}; public static void main(String[] args) { Scanner s = new Scanner(System.in); int n = s.nextInt(); long k = s.nextLong(); char[] str = new char[n]; boolean res = process(str, n, k); System.out.println(res ? new String(str) : "NIE"); } public static boolean process(char[] str, int n, long k){ char curC; long t; int ci; long tk; if(n > 60){ str[0] = 'a'; k--; }else{ t = (2L << n - 1) - 1; if(k > 3 * t){ return false; } ci = (int) (k / (t + 1)); k -= t * ci + 1; str[0] = chars[ci]; if(k <= 0) return true; } curC = str[0]; for(int i = 1; i < n ; i++){ if(n - i > 60){ str[i] = nextChar(curC, 0); curC = str[i]; k--; continue; } t = (2L << n - 1 - i) - 1; ci = (int) (k / (t + 1)); str[i] = nextChar(curC, ci); curC = str[i]; k -= t * ci + 1; if(k <= 0) return true; } return false; } public static char nextChar(char prevChar, int idx){ int i = (prevChar << 1) + idx; switch(i){ case 196: case 198: return 'a'; case 194: case 199: return 'b'; case 195: case 197: return 'c'; default: return (char) 0; } } }
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 | import java.util.Scanner; class slo { public static char[] chars = new char[]{'a', 'b', 'c'}; public static void main(String[] args) { Scanner s = new Scanner(System.in); int n = s.nextInt(); long k = s.nextLong(); char[] str = new char[n]; boolean res = process(str, n, k); System.out.println(res ? new String(str) : "NIE"); } public static boolean process(char[] str, int n, long k){ char curC; long t; int ci; long tk; if(n > 60){ str[0] = 'a'; k--; }else{ t = (2L << n - 1) - 1; if(k > 3 * t){ return false; } ci = (int) (k / (t + 1)); k -= t * ci + 1; str[0] = chars[ci]; if(k <= 0) return true; } curC = str[0]; for(int i = 1; i < n ; i++){ if(n - i > 60){ str[i] = nextChar(curC, 0); curC = str[i]; k--; continue; } t = (2L << n - 1 - i) - 1; ci = (int) (k / (t + 1)); str[i] = nextChar(curC, ci); curC = str[i]; k -= t * ci + 1; if(k <= 0) return true; } return false; } public static char nextChar(char prevChar, int idx){ int i = (prevChar << 1) + idx; switch(i){ case 196: case 198: return 'a'; case 194: case 199: return 'b'; case 195: case 197: return 'c'; default: return (char) 0; } } } |