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
#include <iostream>
using namespace std;

char printNext(char last, bool left)
{
    char next = 'a';
    if (last == 'a')
    {
        next = (left ? 'b' : 'c');
    }
    else if (last == 'b')
    {
        next = (left ? 'a' : 'c');
    }
    else
    {
        next = (left ? 'a' : 'b');
    }
    cout << next;
    return next;
}

int main()
{
    uint64_t n, k;
    cin >> n >> k;
    
    if (n < 60 && k > 3 * ((1 << n) - 1))
    {
        cout << "NIE" << endl;
        return 0;
    } 
    
    ++n;
    
    char last = 'c';
    uint64_t root = 0;
    while (true)
    {
        if (n > 62)
        {
            last = printNext(last, true);
            ++root;
            if (root == k)
                return 0;
            --n;
            continue;
        }
        
        uint64_t left = root + 1;
        uint64_t right = left + ((uint64_t)1 << (n - 1)) - 1;
        uint64_t third = right + ((uint64_t)1 << (n - 1)) - 1;
        
        if (k < right)
        {
            last = printNext(last, true);
            if (left == k)
                return 0;
            
            root = left;
        }
        else if (root == 0 && k >= third)
        {
            last = printNext('a', false);
            if (third == k)
                return 0;
            
            root = third;
        }
        else 
        {
            last = printNext(last, false);
            if (right == k)
                return 0;
            
            root = right;
        }
        
        --n;
    }
    return 0;
}