#include <cstdio> using namespace std; int main() { int n; long long k; scanf("%d %lld", &n, &k); long long slow = 3 * ((1 << n) - 1); if (slow < k) { printf("NIE\n"); } else { char last = 'a'; slow /= 3; while (k > slow) { ++last; k -= slow; } ++slow; // slow = 2 ^ n printf("%c", last); char available[4]; while (k > 1) { --k; --slow; slow /= 2; available[0] = 'a'; available[1] = 'b'; available[2] = 'c'; available[last - 'a'] = '\0'; for (int i = 0; i < 3; ++i) if (available[i] == '\0') { available[i] = available[i+1]; available[i+1] = '\0'; } last = available[0]; if (slow < k) { k -= slow; last = available[1]; } printf("%c", last); } 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 | #include <cstdio> using namespace std; int main() { int n; long long k; scanf("%d %lld", &n, &k); long long slow = 3 * ((1 << n) - 1); if (slow < k) { printf("NIE\n"); } else { char last = 'a'; slow /= 3; while (k > slow) { ++last; k -= slow; } ++slow; // slow = 2 ^ n printf("%c", last); char available[4]; while (k > 1) { --k; --slow; slow /= 2; available[0] = 'a'; available[1] = 'b'; available[2] = 'c'; available[last - 'a'] = '\0'; for (int i = 0; i < 3; ++i) if (available[i] == '\0') { available[i] = available[i+1]; available[i+1] = '\0'; } last = available[0]; if (slow < k) { k -= slow; last = available[1]; } printf("%c", last); } printf("\n"); } return 0; } |