#include <string> #include <vector> #include <iostream> #include <unordered_map> typedef long long ll; int main() { ll n, k; ll maxK = 1000000000LL *1000000000LL; std::cin >> n >> k; std::vector< ll > powsum(n); powsum[n-1] = 1; for(int i = n-2; i >= 0; i--) { powsum[i] = std::min(powsum[i+1] * 2 + 1, maxK); } std::string res = ""; k--; int letterInd = k / powsum[0]; if(letterInd == 0) { res += "a"; } else if(letterInd == 1) { res += "b"; } else if(letterInd == 2) { res += "c"; } else { printf("NIE\n"); return 0; } k -= letterInd * powsum[0]; std::unordered_map< char, char > firstNext {{'a', 'b'}, {'b', 'a'}, {'c', 'a'}}; std::unordered_map< char, char > secondNext {{'a', 'c'}, {'b', 'c'}, {'c', 'b'}}; for(int i = 1; i < n && k != 0; i++) { k--; letterInd = k / powsum[i]; if(letterInd == 0) { res += firstNext[res.back()]; } else { res += secondNext[res.back()]; } k -= letterInd * powsum[i]; } std::cout << res << std::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 | #include <string> #include <vector> #include <iostream> #include <unordered_map> typedef long long ll; int main() { ll n, k; ll maxK = 1000000000LL *1000000000LL; std::cin >> n >> k; std::vector< ll > powsum(n); powsum[n-1] = 1; for(int i = n-2; i >= 0; i--) { powsum[i] = std::min(powsum[i+1] * 2 + 1, maxK); } std::string res = ""; k--; int letterInd = k / powsum[0]; if(letterInd == 0) { res += "a"; } else if(letterInd == 1) { res += "b"; } else if(letterInd == 2) { res += "c"; } else { printf("NIE\n"); return 0; } k -= letterInd * powsum[0]; std::unordered_map< char, char > firstNext {{'a', 'b'}, {'b', 'a'}, {'c', 'a'}}; std::unordered_map< char, char > secondNext {{'a', 'c'}, {'b', 'c'}, {'c', 'b'}}; for(int i = 1; i < n && k != 0; i++) { k--; letterInd = k / powsum[i]; if(letterInd == 0) { res += firstNext[res.back()]; } else { res += secondNext[res.back()]; } k -= letterInd * powsum[i]; } std::cout << res << std::endl; return 0; } |