#include <cstdio> #include <algorithm> #define DEBUG false using namespace std; inline long long pow(int a, int b) { long long c = 1; while (b--) { c *= a; } return c; } void rec(long long current, long long k, int prev_letter) { if (current == 0) return; int n_letter = (prev_letter + 1) % 3, nn_letter = (prev_letter + 2) % 3; if (n_letter > nn_letter) { swap(n_letter, nn_letter); } long long mid = current >> 1; if (DEBUG) { printf("current %lld (mid: %lld); k: %lld\n", current, mid, k); printf("letter: %d; %d\n", n_letter, nn_letter); } if (k == 1) { printf("%c", (char)((int)'a' + n_letter)); return; } else if (k == mid + 1) { printf("%c", (char)((int)'a' + nn_letter)); return; } if (k <= mid) { printf("%c", (char)((int)'a' + n_letter)); rec(mid-1, k-1, n_letter); } else { printf("%c", (char)((int)'a' + nn_letter)); rec(mid-1, k-mid-1, nn_letter); } } int main() { int n; long long k; scanf("%d %lld", &n, &k); long long all = (pow(2, n) - 1); if (3*all < k) { printf("NIE\n"); return 0; } if (DEBUG) { printf("all: %lld\n", all); } // divide by 3 parts if (k == 1) { printf("a\n"); return 0; } else if (k == all + 1) { printf("b\n"); return 0; } else if (k == 2*all + 1) { printf("c\n"); return 0; } if (k > 1 && k <= all) { printf("a"); rec(all-1, k-1, 0); } else if (k > all+1 && k <= 2*all) { printf("b"); rec(all-1, k-all-1, 1); } else if (k > 2*all+1 && k <= 3*all) { printf("c"); rec(all-1, k-2*all-1, 2); } 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 | #include <cstdio> #include <algorithm> #define DEBUG false using namespace std; inline long long pow(int a, int b) { long long c = 1; while (b--) { c *= a; } return c; } void rec(long long current, long long k, int prev_letter) { if (current == 0) return; int n_letter = (prev_letter + 1) % 3, nn_letter = (prev_letter + 2) % 3; if (n_letter > nn_letter) { swap(n_letter, nn_letter); } long long mid = current >> 1; if (DEBUG) { printf("current %lld (mid: %lld); k: %lld\n", current, mid, k); printf("letter: %d; %d\n", n_letter, nn_letter); } if (k == 1) { printf("%c", (char)((int)'a' + n_letter)); return; } else if (k == mid + 1) { printf("%c", (char)((int)'a' + nn_letter)); return; } if (k <= mid) { printf("%c", (char)((int)'a' + n_letter)); rec(mid-1, k-1, n_letter); } else { printf("%c", (char)((int)'a' + nn_letter)); rec(mid-1, k-mid-1, nn_letter); } } int main() { int n; long long k; scanf("%d %lld", &n, &k); long long all = (pow(2, n) - 1); if (3*all < k) { printf("NIE\n"); return 0; } if (DEBUG) { printf("all: %lld\n", all); } // divide by 3 parts if (k == 1) { printf("a\n"); return 0; } else if (k == all + 1) { printf("b\n"); return 0; } else if (k == 2*all + 1) { printf("c\n"); return 0; } if (k > 1 && k <= all) { printf("a"); rec(all-1, k-1, 0); } else if (k > all+1 && k <= 2*all) { printf("b"); rec(all-1, k-all-1, 1); } else if (k > 2*all+1 && k <= 3*all) { printf("c"); rec(all-1, k-2*all-1, 2); } printf("\n"); return 0; } |