import java.math.BigInteger; import java.util.Scanner; /** * https://sio2.mimuw.edu.pl/c/pa-2016-1/p/slo/ * Created by mateusz.szablak on 15.11.2016. */ public class slo { private static char[] ALPHABET = { 'a', 'b', 'c'}; public static String solve(final Integer n, final Long k) { StringBuilder sb = new StringBuilder(); BigInteger childUnderSingleRoot = new BigInteger("1").subtract(new BigInteger("2").pow(n)).multiply(new BigInteger("-1")); BigInteger bi_k = new BigInteger(k.toString()); Character lastChar; if (childUnderSingleRoot.multiply(new BigInteger("3")).compareTo(bi_k) == -1) { return "NIE"; } else { BigInteger firstRootIdx = BigInteger.ONE; BigInteger secondRootIdx = firstRootIdx.add(childUnderSingleRoot); BigInteger thirdRootIdx = secondRootIdx.add(childUnderSingleRoot); BigInteger parentIdx; if (bi_k.compareTo(thirdRootIdx) >= 0) { lastChar = ALPHABET[2]; parentIdx = thirdRootIdx; } else if (bi_k.compareTo(secondRootIdx) >= 0) { lastChar = ALPHABET[1]; parentIdx = secondRootIdx; } else { lastChar = ALPHABET[0]; parentIdx = firstRootIdx; } sb.append(lastChar); while (! (parentIdx.compareTo(bi_k) == 0)) { childUnderSingleRoot = childUnderSingleRoot.divide(new BigInteger("2")); BigInteger leftRootIdx = parentIdx.add(BigInteger.ONE); BigInteger rightRootIdx = leftRootIdx.add(childUnderSingleRoot); if (bi_k.compareTo(rightRootIdx) >= 0) { parentIdx = rightRootIdx; if (lastChar.equals(ALPHABET[0]) || lastChar.equals(ALPHABET[1])) { lastChar = ALPHABET[2]; } else //if (lastChar.equals('c')) { lastChar = ALPHABET[1]; } } else { parentIdx = leftRootIdx; if (lastChar.equals(ALPHABET[0])) { lastChar = ALPHABET[1]; } else //if (lastChar.equals('b') || lastChar.equals('c')) { lastChar = ALPHABET[0]; } } sb.append(lastChar); } } return sb.toString(); } public static void main(String[] args) { Scanner scanner = new Scanner(System.in); Integer n = scanner.nextInt(); Long k = scanner.nextLong(); System.out.println(solve(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 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 | import java.math.BigInteger; import java.util.Scanner; /** * https://sio2.mimuw.edu.pl/c/pa-2016-1/p/slo/ * Created by mateusz.szablak on 15.11.2016. */ public class slo { private static char[] ALPHABET = { 'a', 'b', 'c'}; public static String solve(final Integer n, final Long k) { StringBuilder sb = new StringBuilder(); BigInteger childUnderSingleRoot = new BigInteger("1").subtract(new BigInteger("2").pow(n)).multiply(new BigInteger("-1")); BigInteger bi_k = new BigInteger(k.toString()); Character lastChar; if (childUnderSingleRoot.multiply(new BigInteger("3")).compareTo(bi_k) == -1) { return "NIE"; } else { BigInteger firstRootIdx = BigInteger.ONE; BigInteger secondRootIdx = firstRootIdx.add(childUnderSingleRoot); BigInteger thirdRootIdx = secondRootIdx.add(childUnderSingleRoot); BigInteger parentIdx; if (bi_k.compareTo(thirdRootIdx) >= 0) { lastChar = ALPHABET[2]; parentIdx = thirdRootIdx; } else if (bi_k.compareTo(secondRootIdx) >= 0) { lastChar = ALPHABET[1]; parentIdx = secondRootIdx; } else { lastChar = ALPHABET[0]; parentIdx = firstRootIdx; } sb.append(lastChar); while (! (parentIdx.compareTo(bi_k) == 0)) { childUnderSingleRoot = childUnderSingleRoot.divide(new BigInteger("2")); BigInteger leftRootIdx = parentIdx.add(BigInteger.ONE); BigInteger rightRootIdx = leftRootIdx.add(childUnderSingleRoot); if (bi_k.compareTo(rightRootIdx) >= 0) { parentIdx = rightRootIdx; if (lastChar.equals(ALPHABET[0]) || lastChar.equals(ALPHABET[1])) { lastChar = ALPHABET[2]; } else //if (lastChar.equals('c')) { lastChar = ALPHABET[1]; } } else { parentIdx = leftRootIdx; if (lastChar.equals(ALPHABET[0])) { lastChar = ALPHABET[1]; } else //if (lastChar.equals('b') || lastChar.equals('c')) { lastChar = ALPHABET[0]; } } sb.append(lastChar); } } return sb.toString(); } public static void main(String[] args) { Scanner scanner = new Scanner(System.in); Integer n = scanner.nextInt(); Long k = scanner.nextLong(); System.out.println(solve(n, k)); } } |