import java.util.HashMap; import java.util.Map; import java.util.Scanner; public class slo { static Map<Character, char[]> except = new HashMap<Character, char[]> (){{ put('a', new char[]{'b','c'}); put('b', new char[]{'a','c'}); put('c', new char[]{'a','b'}); } }; public static void main(String[] args) { Scanner s = new Scanner(System.in); int n = s.nextInt(); long k = s.nextLong(); System.out.println(solve(n,k-1)); } static String solve(int n, long k) { long possibilities = (1L << n) - 1; if (k < possibilities) { return 'a' + solve1('a', n-1, k); } else if (k < 2 * possibilities) { return 'b' + solve1('b', n-1, k - possibilities); } else if (k < 3 * possibilities) { return 'c' + solve1('b', n-1, k - 2 * possibilities); } else return "NIE"; } static String solve1(char prevLetter, int n, long k) { if (k == 0) return ""; long possibilities = (1L << n) - 1 ; if (k < possibilities) { char letter = except.get(prevLetter)[0]; return letter + solve1(letter, n-1, k-1); } else { char letter = except.get(prevLetter)[1]; return letter + solve1(letter, n-1, k - possibilities - 1); } } }
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 | import java.util.HashMap; import java.util.Map; import java.util.Scanner; public class slo { static Map<Character, char[]> except = new HashMap<Character, char[]> (){{ put('a', new char[]{'b','c'}); put('b', new char[]{'a','c'}); put('c', new char[]{'a','b'}); } }; public static void main(String[] args) { Scanner s = new Scanner(System.in); int n = s.nextInt(); long k = s.nextLong(); System.out.println(solve(n,k-1)); } static String solve(int n, long k) { long possibilities = (1L << n) - 1; if (k < possibilities) { return 'a' + solve1('a', n-1, k); } else if (k < 2 * possibilities) { return 'b' + solve1('b', n-1, k - possibilities); } else if (k < 3 * possibilities) { return 'c' + solve1('b', n-1, k - 2 * possibilities); } else return "NIE"; } static String solve1(char prevLetter, int n, long k) { if (k == 0) return ""; long possibilities = (1L << n) - 1 ; if (k < possibilities) { char letter = except.get(prevLetter)[0]; return letter + solve1(letter, n-1, k-1); } else { char letter = except.get(prevLetter)[1]; return letter + solve1(letter, n-1, k - possibilities - 1); } } } |