import java.util.*; public class slo { private final String letters = "abc"; private final int MAX = 61; private boolean ltcmp(long k, long n) { if(n >= MAX) return true; return k <= (1L<<n) - 1; } private String calc(long n, long k) { if(n <= MAX && k > 3L*((1L<<n) - 1)) return "NIE"; int len = 0; char[] res = new char[(int)n]; for(int i=0; i < 3; i++) { if(ltcmp(k,n)) { res[len++] = letters.charAt(i); break; } else { k -= (1L<<n) - 1; } } n--; char c = 'x'; while(k > 1) { k -= 1; if(ltcmp(k,n)) { c = (res[len-1] == 'a') ? 'b' : 'a'; } else { k -= (1L<<n) - 1; c = (res[len-1] == 'c') ? 'b' : 'c'; } res[len++] = c; n--; } return String.valueOf(res, 0, len); } public static void main(String[] args) { slo s = new slo(); Scanner sc = new Scanner(System.in); long n = sc.nextLong(); long k = sc.nextLong(); System.out.println(s.calc(n,k)); } }
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 | import java.util.*; public class slo { private final String letters = "abc"; private final int MAX = 61; private boolean ltcmp(long k, long n) { if(n >= MAX) return true; return k <= (1L<<n) - 1; } private String calc(long n, long k) { if(n <= MAX && k > 3L*((1L<<n) - 1)) return "NIE"; int len = 0; char[] res = new char[(int)n]; for(int i=0; i < 3; i++) { if(ltcmp(k,n)) { res[len++] = letters.charAt(i); break; } else { k -= (1L<<n) - 1; } } n--; char c = 'x'; while(k > 1) { k -= 1; if(ltcmp(k,n)) { c = (res[len-1] == 'a') ? 'b' : 'a'; } else { k -= (1L<<n) - 1; c = (res[len-1] == 'c') ? 'b' : 'c'; } res[len++] = c; n--; } return String.valueOf(res, 0, len); } public static void main(String[] args) { slo s = new slo(); Scanner sc = new Scanner(System.in); long n = sc.nextLong(); long k = sc.nextLong(); System.out.println(s.calc(n,k)); } } |