#include <iostream> #include <string> #include <vector> using namespace std; const uint64_t M=2000000000000000000; vector<bool> preorderBinaryWordOf(uint32_t n, uint64_t k) { vector<bool> r; while (n>0) { if (k==0) { return r; } k--; uint64_t m=M; if (n<64) { m=1; m<<=n; m--; } r.push_back(k>=m); n--; k%=m; } return r; } char charFor(int i) { switch (i) { case 0: return 'a'; case 1: return 'b'; default: return 'c'; } } char charFor(char last, bool b) { switch (last) { case 'a': return b?'c':'b'; case 'b': return b?'c':'a'; default: return b?'b':'a'; } } string main2(uint32_t n, uint64_t k) { k--; uint64_t m=M; if (n<64) { m=1; m<<=n; m--; } if (k>=m*3) { return "NIE"; } string r; vector<bool> w = preorderBinaryWordOf(n-1, k%m); char last = charFor(k/m); r+=last; for(vector<bool>::iterator i = w.begin(); i!=w.end(); i++) { last=charFor(last, *i); r+=last; } return r; } int main() { uint32_t n; uint64_t k; cin>>n; cin>>k; cout<<main2(n,k)<<endl; 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 | #include <iostream> #include <string> #include <vector> using namespace std; const uint64_t M=2000000000000000000; vector<bool> preorderBinaryWordOf(uint32_t n, uint64_t k) { vector<bool> r; while (n>0) { if (k==0) { return r; } k--; uint64_t m=M; if (n<64) { m=1; m<<=n; m--; } r.push_back(k>=m); n--; k%=m; } return r; } char charFor(int i) { switch (i) { case 0: return 'a'; case 1: return 'b'; default: return 'c'; } } char charFor(char last, bool b) { switch (last) { case 'a': return b?'c':'b'; case 'b': return b?'c':'a'; default: return b?'b':'a'; } } string main2(uint32_t n, uint64_t k) { k--; uint64_t m=M; if (n<64) { m=1; m<<=n; m--; } if (k>=m*3) { return "NIE"; } string r; vector<bool> w = preorderBinaryWordOf(n-1, k%m); char last = charFor(k/m); r+=last; for(vector<bool>::iterator i = w.begin(); i!=w.end(); i++) { last=charFor(last, *i); r+=last; } return r; } int main() { uint32_t n; uint64_t k; cin>>n; cin>>k; cout<<main2(n,k)<<endl; return 0; } |