import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.util.StringTokenizer; public class slo { static long[] readLongsLine(int amount) throws IOException { BufferedReader reader = new BufferedReader(new InputStreamReader(System.in)); StringTokenizer tokenizer = new StringTokenizer(reader.readLine()); long[] result = new long[amount]; for (int i = 0; i < amount; i++) { result[i] = Long.parseLong(tokenizer.nextToken()); } return result; } private static long treeSize(int n) { return (long) (Math.pow(2, n) - 1); } private static char nextChar(char previousChar, boolean right) { if (!right) { switch (previousChar) { case 'a': return 'b'; case 'b': return 'a'; case 'c': return 'a'; } } else { switch (previousChar) { case 'a': return 'c'; case 'b': return 'c'; case 'c': return 'b'; } } return 'x'; } private static char[] kWordInternal(char[] buffer, String directions) { for (int pos = 1; pos < directions.length(); pos++) { buffer[pos] = nextChar(buffer[pos-1], directions.charAt(pos) == '1'); } return buffer; } static String kWord(int n, long k) { char[] result = new char[n]; long treeSize = treeSize(n); if (k <= treeSize) { result[0] = 'a'; } else if (k > treeSize && k <= 2 * treeSize) { result[0] = 'b'; k = k - treeSize; } else if (k > 2 * treeSize && k <= 3 * treeSize) { result[0] = 'c'; k = k - 2 * treeSize; } else { return "NIE"; } return new String(kWordInternal(result, Long.toBinaryString(k))); } public static void main(String[] args) throws IOException { long[] line = readLongsLine(2); int n = (int) line[0]; long k = line[1]; System.out.println(kWord(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 | import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.util.StringTokenizer; public class slo { static long[] readLongsLine(int amount) throws IOException { BufferedReader reader = new BufferedReader(new InputStreamReader(System.in)); StringTokenizer tokenizer = new StringTokenizer(reader.readLine()); long[] result = new long[amount]; for (int i = 0; i < amount; i++) { result[i] = Long.parseLong(tokenizer.nextToken()); } return result; } private static long treeSize(int n) { return (long) (Math.pow(2, n) - 1); } private static char nextChar(char previousChar, boolean right) { if (!right) { switch (previousChar) { case 'a': return 'b'; case 'b': return 'a'; case 'c': return 'a'; } } else { switch (previousChar) { case 'a': return 'c'; case 'b': return 'c'; case 'c': return 'b'; } } return 'x'; } private static char[] kWordInternal(char[] buffer, String directions) { for (int pos = 1; pos < directions.length(); pos++) { buffer[pos] = nextChar(buffer[pos-1], directions.charAt(pos) == '1'); } return buffer; } static String kWord(int n, long k) { char[] result = new char[n]; long treeSize = treeSize(n); if (k <= treeSize) { result[0] = 'a'; } else if (k > treeSize && k <= 2 * treeSize) { result[0] = 'b'; k = k - treeSize; } else if (k > 2 * treeSize && k <= 3 * treeSize) { result[0] = 'c'; k = k - 2 * treeSize; } else { return "NIE"; } return new String(kWordInternal(result, Long.toBinaryString(k))); } public static void main(String[] args) throws IOException { long[] line = readLongsLine(2); int n = (int) line[0]; long k = line[1]; System.out.println(kWord(n, k)); } } |