#include <iostream> #include <algorithm> using namespace std; long long s[1000001]; // how many good continuations words with <= k characters void count(int n) { s[0] = 1; s[1] = 3; for (int i = 0; i < n; ++i) { if (i > 1) { s[i] = 2*s[i-1] + 1; } // cout << i << " -> " << s[i] << endl; } } char first(char p) { if (p != 'a') return 'a'; else return 'b'; } char second(char p) { if (!p) return 'b'; else if (p != 'c') return 'c'; else return 'b'; } int main() { long long n, k; cin >> n >> k; count(62); char prev = 0; if (n < 62 && 3*s[n-1] < k) { cout << "NIE" << endl; return 0; } ++k; while (n > 0) { --k; if (k == 0) break; if (n > 60 || s[n-1] >= k) { cout << (prev = first(prev)); } else if (2*s[n-1] >= k) { cout << (prev = second(prev)); k -= s[n-1]; } else { cout << (prev = 'c'); k -= 2*s[n-1]; } --n; } cout << 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 | #include <iostream> #include <algorithm> using namespace std; long long s[1000001]; // how many good continuations words with <= k characters void count(int n) { s[0] = 1; s[1] = 3; for (int i = 0; i < n; ++i) { if (i > 1) { s[i] = 2*s[i-1] + 1; } // cout << i << " -> " << s[i] << endl; } } char first(char p) { if (p != 'a') return 'a'; else return 'b'; } char second(char p) { if (!p) return 'b'; else if (p != 'c') return 'c'; else return 'b'; } int main() { long long n, k; cin >> n >> k; count(62); char prev = 0; if (n < 62 && 3*s[n-1] < k) { cout << "NIE" << endl; return 0; } ++k; while (n > 0) { --k; if (k == 0) break; if (n > 60 || s[n-1] >= k) { cout << (prev = first(prev)); } else if (2*s[n-1] >= k) { cout << (prev = second(prev)); k -= s[n-1]; } else { cout << (prev = 'c'); k -= 2*s[n-1]; } --n; } cout << endl; return 0; } |