#include <stdio.h> unsigned long pow2(unsigned long exp) { unsigned long result = 1; unsigned long base = 2; while (exp) { if (exp & 1) { result *= base; } exp >>= 1; base *= base; } return result; } char left_letter(char c) { if (c == 'a') { return 'b'; } return 'a'; } char right_letter(char c) { if (c == 'c') { return 'b'; } return 'c'; } unsigned long right_child(unsigned long nr, unsigned long level) { return nr + pow2(level); } int main() { unsigned long n, k, npow, nwords, nr, rchild, level; char last_letter; scanf("%lu %lu\n", &n, &k); if (n <= 62) { npow = pow2(n); nwords = 3*npow-3; if (nwords < k) { printf("NIE\n"); return 0; } if (k <= npow-1) { last_letter = 'a'; } else { if (k <= 2*npow-2) { last_letter = 'b'; k -= npow - 1; } else { last_letter = 'c'; k -= 2*npow - 2; } } } else { last_letter = 'a'; } printf("%c", last_letter); nr = 1; level = n-1; while (nr != k) { if (level > 62) { nr++; last_letter = left_letter(last_letter); } else { rchild = right_child(nr, level); if (k < rchild) { nr++; last_letter = left_letter(last_letter); } else { nr = rchild; last_letter = right_letter(last_letter); } } level--; printf("%c", last_letter); } printf("\n"); 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 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 | #include <stdio.h> unsigned long pow2(unsigned long exp) { unsigned long result = 1; unsigned long base = 2; while (exp) { if (exp & 1) { result *= base; } exp >>= 1; base *= base; } return result; } char left_letter(char c) { if (c == 'a') { return 'b'; } return 'a'; } char right_letter(char c) { if (c == 'c') { return 'b'; } return 'c'; } unsigned long right_child(unsigned long nr, unsigned long level) { return nr + pow2(level); } int main() { unsigned long n, k, npow, nwords, nr, rchild, level; char last_letter; scanf("%lu %lu\n", &n, &k); if (n <= 62) { npow = pow2(n); nwords = 3*npow-3; if (nwords < k) { printf("NIE\n"); return 0; } if (k <= npow-1) { last_letter = 'a'; } else { if (k <= 2*npow-2) { last_letter = 'b'; k -= npow - 1; } else { last_letter = 'c'; k -= 2*npow - 2; } } } else { last_letter = 'a'; } printf("%c", last_letter); nr = 1; level = n-1; while (nr != k) { if (level > 62) { nr++; last_letter = left_letter(last_letter); } else { rchild = right_child(nr, level); if (k < rchild) { nr++; last_letter = left_letter(last_letter); } else { nr = rchild; last_letter = right_letter(last_letter); } } level--; printf("%c", last_letter); } printf("\n"); return 0; } |