#include <cstdio> #include <cstring> #include <cmath> #include <cassert> #include <iostream> #include <algorithm> #include <iterator> #include <string> #include <vector> #include <queue> #include <bitset> #include <utility> #include <stack> using namespace std; typedef long long LL; typedef pair<int,int> PII; typedef vector<int> VI; #define MP make_pair #define FOR(v,p,k) for(int v=(p);v<=(k);++v) #define FORD(v,p,k) for(int v=(p);v>=(k);--v) #define REP(i,n) for(int i=0;i<(n);++i) #define VAR(v,i) __typeof(i) v=(i) #define FOREACH(i,c) for(VAR(i,(c).begin());i!=(c).end();++i) #define PB push_back #define ST first #define ND second #define SIZE(x) (int)x.size() #define ALL(c) c.begin(),c.end() #define ODD(x) ((x)%2) #define EVEN(x) (!(ODD(x))) class Slo { int const n, alphabet_cnt; LL const max_value; vector<LL> words_count_after_letter; void init_words_counts() { words_count_after_letter[0]=1; FOR(i,1,n-1) { words_count_after_letter[i] = min(max_value, (alphabet_cnt-1)*words_count_after_letter[i-1] + 1); //printf("words: %lld\n", words_count_after_letter[i]); } } inline LL get_words_count(int nn) const { return words_count_after_letter[nn]; } public: Slo(int n, int alphabet_cnt = 3, LL max_value = 1000000000000000009LL) : n(n), alphabet_cnt(alphabet_cnt), max_value(max_value), words_count_after_letter(n) { init_words_counts(); } LL get_all_words_count() const { return alphabet_cnt * get_words_count(n-1); } VI find_kth_word(LL k) const { VI res; res.reserve(n); int i=n-1; int prev_letter=alphabet_cnt+9; while (k > 0) { LL cnt = get_words_count(i); REP(letter, alphabet_cnt) { //printf("k=%lld, cnt=%lld, letter=%d\n", k, cnt, letter); if (prev_letter == letter) continue; if (k > cnt) { k -= cnt; } else { --k; res.PB(letter); //printf("letter=%d\n",letter); prev_letter=letter; break; } } --i; } return res; } }; int main() { int n; LL k; scanf("%d%lld", &n, &k); Slo slo(n); if (slo.get_all_words_count() < k) { printf("NIE\n"); } else { VI res_ints = slo.find_kth_word(k); string res; res.reserve(n); FOREACH(it, res_ints) { res.PB('a'+*it); } printf("%s\n", res.c_str()); } 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 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 | #include <cstdio> #include <cstring> #include <cmath> #include <cassert> #include <iostream> #include <algorithm> #include <iterator> #include <string> #include <vector> #include <queue> #include <bitset> #include <utility> #include <stack> using namespace std; typedef long long LL; typedef pair<int,int> PII; typedef vector<int> VI; #define MP make_pair #define FOR(v,p,k) for(int v=(p);v<=(k);++v) #define FORD(v,p,k) for(int v=(p);v>=(k);--v) #define REP(i,n) for(int i=0;i<(n);++i) #define VAR(v,i) __typeof(i) v=(i) #define FOREACH(i,c) for(VAR(i,(c).begin());i!=(c).end();++i) #define PB push_back #define ST first #define ND second #define SIZE(x) (int)x.size() #define ALL(c) c.begin(),c.end() #define ODD(x) ((x)%2) #define EVEN(x) (!(ODD(x))) class Slo { int const n, alphabet_cnt; LL const max_value; vector<LL> words_count_after_letter; void init_words_counts() { words_count_after_letter[0]=1; FOR(i,1,n-1) { words_count_after_letter[i] = min(max_value, (alphabet_cnt-1)*words_count_after_letter[i-1] + 1); //printf("words: %lld\n", words_count_after_letter[i]); } } inline LL get_words_count(int nn) const { return words_count_after_letter[nn]; } public: Slo(int n, int alphabet_cnt = 3, LL max_value = 1000000000000000009LL) : n(n), alphabet_cnt(alphabet_cnt), max_value(max_value), words_count_after_letter(n) { init_words_counts(); } LL get_all_words_count() const { return alphabet_cnt * get_words_count(n-1); } VI find_kth_word(LL k) const { VI res; res.reserve(n); int i=n-1; int prev_letter=alphabet_cnt+9; while (k > 0) { LL cnt = get_words_count(i); REP(letter, alphabet_cnt) { //printf("k=%lld, cnt=%lld, letter=%d\n", k, cnt, letter); if (prev_letter == letter) continue; if (k > cnt) { k -= cnt; } else { --k; res.PB(letter); //printf("letter=%d\n",letter); prev_letter=letter; break; } } --i; } return res; } }; int main() { int n; LL k; scanf("%d%lld", &n, &k); Slo slo(n); if (slo.get_all_words_count() < k) { printf("NIE\n"); } else { VI res_ints = slo.find_kth_word(k); string res; res.reserve(n); FOREACH(it, res_ints) { res.PB('a'+*it); } printf("%s\n", res.c_str()); } return 0; } |