#include <cstdio> using namespace std; long long int k, size = 1; int n, exp; void pre() { while (exp < n && size - 1 < k) { size *= 2; ++exp; } size -= 1; } void printLetter(int level, long long int t_size, char letter, long long int node) { while (node != k) { printf("%c", letter); if (level > exp) { t_size = level - 1 == exp ? size : 0; node += 1; letter = letter == 'a' ? 'b' : 'a'; } else { if (k <= node + t_size / 2) { letter = letter == 'a' ? 'b' : 'a'; node += 1; } else { letter = letter == 'c' ? 'b' : 'c'; node += t_size / 2 + 1; } t_size /= 2; } level -= 1; } printf("%c\n", letter); } void solve() { if (exp == n && 3 * size < k) { printf("NIE\n"); } else { char l = 'a'; if (exp == n) { l = k > 2 * size ? 'c' : k > size ? 'b' : 'a'; k -= k > 2 * size ? 2 * size : k > size ? size : 0; } printLetter(n, exp < n ? 0 : size, l, 1); } } void readInput() { scanf("%d %lld", &n, &k); } int main() { readInput(); pre(); solve(); return 0; }
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 | #include <cstdio> using namespace std; long long int k, size = 1; int n, exp; void pre() { while (exp < n && size - 1 < k) { size *= 2; ++exp; } size -= 1; } void printLetter(int level, long long int t_size, char letter, long long int node) { while (node != k) { printf("%c", letter); if (level > exp) { t_size = level - 1 == exp ? size : 0; node += 1; letter = letter == 'a' ? 'b' : 'a'; } else { if (k <= node + t_size / 2) { letter = letter == 'a' ? 'b' : 'a'; node += 1; } else { letter = letter == 'c' ? 'b' : 'c'; node += t_size / 2 + 1; } t_size /= 2; } level -= 1; } printf("%c\n", letter); } void solve() { if (exp == n && 3 * size < k) { printf("NIE\n"); } else { char l = 'a'; if (exp == n) { l = k > 2 * size ? 'c' : k > size ? 'b' : 'a'; k -= k > 2 * size ? 2 * size : k > size ? size : 0; } printLetter(n, exp < n ? 0 : size, l, 1); } } void readInput() { scanf("%d %lld", &n, &k); } int main() { readInput(); pre(); solve(); return 0; } |