import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; /** * Created by Kamil Miekus on 17.11.2016. */ public class slo { public static final long MAX_N = 1_000_000L; public static final long MAX_K = 1_000_000_000_000_000_000L; public static void main(String[] args) { String line = null; try(BufferedReader reader = new BufferedReader(new InputStreamReader(System.in))) { line = reader.readLine(); } catch (IOException e) { e.printStackTrace(); } String[] arg = line.split(" "); printWord(Long.parseLong(arg[0]), Long.parseLong(arg[1])); } static void printWord(long n, long k) { long temp = k; long mid = 0L; long start = 1L; long end = getMaximumNumberOfWordsForN(n); char currentChar = 'a'; if ((n < 1 || n > MAX_N) || (k < 1 || k > MAX_K)) { System.out.print("NIE"); return; } if (getMaximumNumberOfWordsForN(n) < k) { System.out.print("NIE"); return; } StringBuilder word = new StringBuilder(); for (long i = 1L; i <= n; i++) { if (i == 1L) { temp = end / 3L; if (k > 2L*temp) { currentChar = 'c'; start = 2L*temp + 1L; } else if (k > temp) { currentChar = 'b'; start = temp + 1L; end = 2L * temp; } else { currentChar = 'a'; start = 1L; end = temp; } word.append(currentChar); if (k == 1L || k == temp + 1L || k == 2L*temp + 1L) { System.out.print(word); return; } } else { mid = (end - start) / 2L + start; if (k > mid) { currentChar = resolveNextChar(currentChar, false); start = mid; } else { currentChar = resolveNextChar(currentChar, true); end = mid; } word.append(currentChar); if (k == start + 1L || k == mid + 1L) { System.out.print(word); return; } start++; } } System.out.print(word); } static long getMaximumNumberOfWordsForN(long n) { return 3L * (2L * (1L - power(2, n - 1)) / -1L + 1L); } static long power(long a, long b) { long result = 1L; for (long i = 0; i < b; i++) { result *= a; } return result; } static char resolveNextChar(char someChar, boolean isLower) { if (someChar == 'a' && isLower) { return 'b'; } else if (someChar == 'a' && !isLower) { return 'c'; } else if (someChar == 'b' && isLower) { return 'a'; } else if (someChar == 'b' && !isLower) { return 'c'; } else if (someChar == 'c' && isLower) { return 'a'; } else { return 'b'; } } }
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 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 | import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; /** * Created by Kamil Miekus on 17.11.2016. */ public class slo { public static final long MAX_N = 1_000_000L; public static final long MAX_K = 1_000_000_000_000_000_000L; public static void main(String[] args) { String line = null; try(BufferedReader reader = new BufferedReader(new InputStreamReader(System.in))) { line = reader.readLine(); } catch (IOException e) { e.printStackTrace(); } String[] arg = line.split(" "); printWord(Long.parseLong(arg[0]), Long.parseLong(arg[1])); } static void printWord(long n, long k) { long temp = k; long mid = 0L; long start = 1L; long end = getMaximumNumberOfWordsForN(n); char currentChar = 'a'; if ((n < 1 || n > MAX_N) || (k < 1 || k > MAX_K)) { System.out.print("NIE"); return; } if (getMaximumNumberOfWordsForN(n) < k) { System.out.print("NIE"); return; } StringBuilder word = new StringBuilder(); for (long i = 1L; i <= n; i++) { if (i == 1L) { temp = end / 3L; if (k > 2L*temp) { currentChar = 'c'; start = 2L*temp + 1L; } else if (k > temp) { currentChar = 'b'; start = temp + 1L; end = 2L * temp; } else { currentChar = 'a'; start = 1L; end = temp; } word.append(currentChar); if (k == 1L || k == temp + 1L || k == 2L*temp + 1L) { System.out.print(word); return; } } else { mid = (end - start) / 2L + start; if (k > mid) { currentChar = resolveNextChar(currentChar, false); start = mid; } else { currentChar = resolveNextChar(currentChar, true); end = mid; } word.append(currentChar); if (k == start + 1L || k == mid + 1L) { System.out.print(word); return; } start++; } } System.out.print(word); } static long getMaximumNumberOfWordsForN(long n) { return 3L * (2L * (1L - power(2, n - 1)) / -1L + 1L); } static long power(long a, long b) { long result = 1L; for (long i = 0; i < b; i++) { result *= a; } return result; } static char resolveNextChar(char someChar, boolean isLower) { if (someChar == 'a' && isLower) { return 'b'; } else if (someChar == 'a' && !isLower) { return 'c'; } else if (someChar == 'b' && isLower) { return 'a'; } else if (someChar == 'b' && !isLower) { return 'c'; } else if (someChar == 'c' && isLower) { return 'a'; } else { return 'b'; } } } |