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
// slo.cpp : Defines the entry point for the console application.
//

#include <iostream>
#include <string>

using namespace std;

bool ok = false;

void gen_word(string &s, int max_tot_len, long long idx_in_suffixes)
{
	if(idx_in_suffixes == 0) {
		cout << s << endl;
		ok = true;
		return;
	} else
		idx_in_suffixes--;
	if(s.size() < max_tot_len) {
		for(char next_char = 'a'; next_char <= 'c'; next_char++) {
			if(!s.empty() && s[s.size()-1]==next_char)
				continue;
			long long tot_num_with_curr_start = max_tot_len - s.size() <= 60 ? (1LL << (max_tot_len - s.size())) - 1LL : ((1LL << 62) + (1LL << 61));
			if(idx_in_suffixes >= tot_num_with_curr_start) {
				idx_in_suffixes -= tot_num_with_curr_start;
			} else {
				s.push_back(next_char);
				gen_word(s, max_tot_len, idx_in_suffixes);
				break;
			}
		}
	}
}


int main(int argc, char* argv[])
{
	int n;
	long long k;
	cin >> n >> k;
	string s = "";
	gen_word(s, n, k);
	if(!ok)
		cout << "NIE\n";
	return 0;
}