#include <cmath> #include <cstdio> #include <vector> #include <iostream> #include <algorithm> using namespace std; typedef long long int lint; const lint MAX_K = 1000000000000000000LL; lint pow2(int exp) { if (exp > 62) return 2 * MAX_K; lint res = 1; while(exp--) { res *= 2; } return res; } char first(char c) { switch(c) { case 'a': return 'b'; case 'b': return 'a'; case 'c': return 'a'; default: throw "Error in `first`"; } } char second(char c) { switch(c) { case 'a': return 'c'; case 'b': return 'c'; case 'c': return 'b'; default: throw "Error in `first`"; } } string solve(int n, lint k, string prefix) { int l = prefix.size(); lint subtreeSize = pow2(n - l) - 1; if (l == 0) { if (k <= subtreeSize) return solve(n, k, "a"); else if (k <= 2*subtreeSize) return solve(n, k - subtreeSize, "b"); else return solve(n, k - 2*subtreeSize, "c"); } if (k == 1) return prefix; if (n == l) return "NIE"; if (subtreeSize >= k-1) return solve(n, k-1, prefix + first(prefix[l-1])); else return solve(n, k - 1 - subtreeSize, prefix + second(prefix[l-1])); } int main() { int n; lint k; cin >> n >> k; cout << solve(n, k, "") << endl; }
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 <cmath> #include <cstdio> #include <vector> #include <iostream> #include <algorithm> using namespace std; typedef long long int lint; const lint MAX_K = 1000000000000000000LL; lint pow2(int exp) { if (exp > 62) return 2 * MAX_K; lint res = 1; while(exp--) { res *= 2; } return res; } char first(char c) { switch(c) { case 'a': return 'b'; case 'b': return 'a'; case 'c': return 'a'; default: throw "Error in `first`"; } } char second(char c) { switch(c) { case 'a': return 'c'; case 'b': return 'c'; case 'c': return 'b'; default: throw "Error in `first`"; } } string solve(int n, lint k, string prefix) { int l = prefix.size(); lint subtreeSize = pow2(n - l) - 1; if (l == 0) { if (k <= subtreeSize) return solve(n, k, "a"); else if (k <= 2*subtreeSize) return solve(n, k - subtreeSize, "b"); else return solve(n, k - 2*subtreeSize, "c"); } if (k == 1) return prefix; if (n == l) return "NIE"; if (subtreeSize >= k-1) return solve(n, k-1, prefix + first(prefix[l-1])); else return solve(n, k - 1 - subtreeSize, prefix + second(prefix[l-1])); } int main() { int n; lint k; cin >> n >> k; cout << solve(n, k, "") << endl; } |