#include <cstdlib> #include <stdio.h> #include <iostream> #include <vector> #include <set> #include <map> #include <queue> #include <algorithm> long long n, k; char letters[] = { 'a', 'b', 'c', 'd', 'e' }; char GetLetter(int index, char previousLetter = 'x') { if (previousLetter == 'a') return letters[index + 1]; if (previousLetter == 'c') return letters[index]; if (previousLetter == 'b') return index == 0 ? letters[index] : letters[index + 1]; return letters[index]; } void solve() { char letter, previousLetter = 'x'; long long cycleLength; bool end = false, skip = false; long letterIndex; if (n > 61) { std::cout << "a"; skip = true; for (int i = 0; i < (n - 61) / 2; i++) std::cout << "ba"; if ((n - 61) % 2 == 1) { std::cout << "b"; previousLetter = 'b'; } else previousLetter = 'a'; k = k - (n - 61) - 1;n = 61; } cycleLength = (long long)pow(2, n) - 1; if (k > 3*cycleLength) { std::cout << "NIE" << std::endl; return; } if (!skip) { letterIndex = k <= cycleLength ? 0 : (k <= 2 * cycleLength ? 1 : 2); letter = GetLetter(letterIndex); std::cout << letter; previousLetter = letter; k = k - letterIndex * cycleLength - 1; end = k == 0 || n == 1; } while (!end) { cycleLength = (long long)pow(2, n) - 2; letterIndex = k <= (cycleLength / 2) ? 0 : 1; if (k == 1)letterIndex = 0; letter = GetLetter(letterIndex, previousLetter); previousLetter = letter; std::cout << letter; n--;k = k - letterIndex * (cycleLength / 2) - 1; end = k == 0 || cycleLength == 1 || n == 1; } std::cout << std::endl; } int main() { std::ios_base::sync_with_stdio(0); std::cin >> n >> k; solve(); 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 74 75 76 77 78 79 80 81 | #include <cstdlib> #include <stdio.h> #include <iostream> #include <vector> #include <set> #include <map> #include <queue> #include <algorithm> long long n, k; char letters[] = { 'a', 'b', 'c', 'd', 'e' }; char GetLetter(int index, char previousLetter = 'x') { if (previousLetter == 'a') return letters[index + 1]; if (previousLetter == 'c') return letters[index]; if (previousLetter == 'b') return index == 0 ? letters[index] : letters[index + 1]; return letters[index]; } void solve() { char letter, previousLetter = 'x'; long long cycleLength; bool end = false, skip = false; long letterIndex; if (n > 61) { std::cout << "a"; skip = true; for (int i = 0; i < (n - 61) / 2; i++) std::cout << "ba"; if ((n - 61) % 2 == 1) { std::cout << "b"; previousLetter = 'b'; } else previousLetter = 'a'; k = k - (n - 61) - 1;n = 61; } cycleLength = (long long)pow(2, n) - 1; if (k > 3*cycleLength) { std::cout << "NIE" << std::endl; return; } if (!skip) { letterIndex = k <= cycleLength ? 0 : (k <= 2 * cycleLength ? 1 : 2); letter = GetLetter(letterIndex); std::cout << letter; previousLetter = letter; k = k - letterIndex * cycleLength - 1; end = k == 0 || n == 1; } while (!end) { cycleLength = (long long)pow(2, n) - 2; letterIndex = k <= (cycleLength / 2) ? 0 : 1; if (k == 1)letterIndex = 0; letter = GetLetter(letterIndex, previousLetter); previousLetter = letter; std::cout << letter; n--;k = k - letterIndex * (cycleLength / 2) - 1; end = k == 0 || cycleLength == 1 || n == 1; } std::cout << std::endl; } int main() { std::ios_base::sync_with_stdio(0); std::cin >> n >> k; solve(); return 0; } |