#include <cstdio> /* MAX_EXPONENT required to make sure we don't overflow on 1ull << n and the like 2^61 = > 1E18 >= k */ const int MAX_EXPONENT = 61; int slowo(unsigned long long n, unsigned long long k) { if (n < MAX_EXPONENT+1 && k > 3*(1ull<<n) - 3) { printf("NIE"); } else { int choices[3][3] = {{0, 1, 2}, {1, 0, 2}, {2, 0, 1}}; int* cur; char chars[] = "abc"; // choose the first letter if (n > MAX_EXPONENT || k < (1ull << n)) { // a cur = choices[0]; } else if (k < (1ull << (n+1))-1) { // b cur = choices[1]; k = k - (1ull << n) + 1; } else { // c cur = choices[2]; k = k - (1ull << (n+1)) + 2; } // append the rest unsigned long long i = n-1; do { printf("%c", chars[cur[0]]); if (k == 1) break; if (i > MAX_EXPONENT || k <= (1ull << i)) { cur = choices[cur[1]]; k = k - 1; } else { cur = choices[cur[2]]; k = k - (1ull << i); } i -= 1; } while (true); } return 0; } int main() { unsigned long long n, k; scanf("%llu %llu", &n, &k); slowo(n, k); 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 | #include <cstdio> /* MAX_EXPONENT required to make sure we don't overflow on 1ull << n and the like 2^61 = > 1E18 >= k */ const int MAX_EXPONENT = 61; int slowo(unsigned long long n, unsigned long long k) { if (n < MAX_EXPONENT+1 && k > 3*(1ull<<n) - 3) { printf("NIE"); } else { int choices[3][3] = {{0, 1, 2}, {1, 0, 2}, {2, 0, 1}}; int* cur; char chars[] = "abc"; // choose the first letter if (n > MAX_EXPONENT || k < (1ull << n)) { // a cur = choices[0]; } else if (k < (1ull << (n+1))-1) { // b cur = choices[1]; k = k - (1ull << n) + 1; } else { // c cur = choices[2]; k = k - (1ull << (n+1)) + 2; } // append the rest unsigned long long i = n-1; do { printf("%c", chars[cur[0]]); if (k == 1) break; if (i > MAX_EXPONENT || k <= (1ull << i)) { cur = choices[cur[1]]; k = k - 1; } else { cur = choices[cur[2]]; k = k - (1ull << i); } i -= 1; } while (true); } return 0; } int main() { unsigned long long n, k; scanf("%llu %llu", &n, &k); slowo(n, k); return 0; } |