#include <cstdio> #include <string> #include <iostream> using namespace std; char next(char f) { switch(f) { case 'a': return 'b'; case 'b': return 'a'; case 'c': return 'a'; default: return 'a'; } } char next2(char f) { switch(f) { case 'a': return 'c'; case 'b': return 'c'; case 'c': return 'b'; default: return 'b'; } } string A; void generate(long long n, long long k, char f) { //printf("%c %lld %lld\n", f, n, k); if(k == 0) return; if(n == 0) { if(k > 0) A = "NIE"; return; } if (n > 62 || (k < 1ll << n)) { A.push_back(next(f)); generate(n - 1, k - 1, next(f)); } else { k -= (1ll << n) - 1; A.push_back(next2(f)); generate(n - 1, k - 1, next2(f)); } } int main() { int n; long long k; scanf("%d%lld", &n, &k); generate(n, k, ' '); cout << A << "\n"; }
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 | #include <cstdio> #include <string> #include <iostream> using namespace std; char next(char f) { switch(f) { case 'a': return 'b'; case 'b': return 'a'; case 'c': return 'a'; default: return 'a'; } } char next2(char f) { switch(f) { case 'a': return 'c'; case 'b': return 'c'; case 'c': return 'b'; default: return 'b'; } } string A; void generate(long long n, long long k, char f) { //printf("%c %lld %lld\n", f, n, k); if(k == 0) return; if(n == 0) { if(k > 0) A = "NIE"; return; } if (n > 62 || (k < 1ll << n)) { A.push_back(next(f)); generate(n - 1, k - 1, next(f)); } else { k -= (1ll << n) - 1; A.push_back(next2(f)); generate(n - 1, k - 1, next2(f)); } } int main() { int n; long long k; scanf("%d%lld", &n, &k); generate(n, k, ' '); cout << A << "\n"; } |