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
#include <iostream>
#include <cmath>

using namespace std;

int n, k;

long long nmin, nmax;
short decisions[(int) 10e6 + 7];
char word[(int) 10e6 + 7];
int i = 0, j = 1;
char first;

int main()
{
    ios_base::sync_with_stdio(false);
    cin >> n >> k;

    long long x = (pow(2, n) - 1);
    if (k <= x) {
        nmin = 1;
        nmax = x;
        first = 'a';
    } else if (k <= 2 * x) {
        nmin = x + 1;
        nmax = 2 * x;
        first = 'b';
    } else if (k <= 3 * x) {
        nmin = 2* x + 1;
        nmax = 3 * x;
        first = 'c';
    } else {
        cout << "NIE\n";
        return 0;
    }

    while (nmax != nmin) {
        int s = (nmax + nmin) / 2;
        //cout << nmin << " " << nmax << endl;
        if (k <= s) {
            nmax = s;
            decisions[i] = 0;
            //cout << "0\n";
        } else {
            nmin = s + 1;
            decisions[i] = 1;
            //cout << "1\n";
        }
        //cout << "turn " << i << endl;
        ++i;
    }

    word[0] = first;

    //cout << "==============\n";
    for (; j <= i; ++j) {
        //cout << j << " " << decisions[j] << endl;
        if (decisions[j-1] == 0) {
            if (word[j-1] == 'a') {
                word[j] = 'b';
            } else {
                word[j] = 'a';
            }
        } else {
            if (word[j-1] != 'c') {
                word[j] = 'c';
            } else {
                word[j] = 'b';
            }
        }
    }

    cout << word << "\n";

    return 0;
}