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
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
#include <iostream>
#include <vector>
#include <string>
#include <functional>
#include <variant>
#include <cassert>
using namespace std;

string decorate(string substr, long long mult) {
    if (mult == 1)
        return substr;
    if (mult < 10) {
        if (substr.length() > 1)
            return string(1, mult + '0') + "[" + substr + "]";
        else
            return string(1, mult + '0') + substr;
    }
    int rest = mult % 9;
    string prefix = rest ? decorate(substr, rest) : "";
    string inner_suffix = decorate(substr, mult / 9);
    if (inner_suffix.length() == 1)
        return prefix + "9" + inner_suffix;
    else
        return prefix + "9[" + inner_suffix + "]";
}

class TriangleBuilder {
    static constexpr char DirSeq[6] = {'A', 'B', 'C', 'D', 'E', 'F'};
    int rotation = 0;

    struct IRNode;
    typedef pair<IRNode*, long long> PR;
    typedef variant<char, PR> VR;
    struct IRNode {
        vector<VR> values;
    };
    vector<IRNode*> S;

    public:
    TriangleBuilder() {
        push_frame();
    }

    ~TriangleBuilder() {
        assert(S.size() == 1);

        function <void(IRNode*)> R = [&](IRNode *node) {
            for (auto& vr : node->values)
                if (holds_alternative<PR>(vr))
                    R(get<PR>(vr).first);
            delete node;
        };

        R(S.back());
    }

    void rotate(int delta) {
        rotation = (rotation - delta + 18) % 6;
    }

    void push_frame() {
        S.push_back(new IRNode);
    }

    void single_move(char dir) {
        dir -= 'A';
        S.back()->values.push_back(VR(DirSeq[(dir + rotation) % 6]));
    }

    void pop_frame(long long mult) {
        IRNode* top = S.back();
        S.pop_back();
        S.back()->values.push_back(VR{PR{top, mult}});
    }

    string to_string() {
        function<string(IRNode*)> R = [&](IRNode *node) -> string {
            string result;
            for (auto& vr : node->values)
                if (holds_alternative<char>(vr))
                    result.push_back(get<char>(vr));
                else 
                    result += decorate(R(get<PR>(vr).first), get<PR>(vr).second);
            return result;
        };

        assert(S.size() == 1);
        return R(S.back());
    }
};

void TheRecursion(TriangleBuilder& TB, long long n) {
    if (n <= 4) { // FIXME good constant
        while (n > 1) {
            TB.push_frame();
            {
                TB.single_move('E');
                TB.single_move('C');
            }
            TB.pop_frame(n - 1);
            TB.single_move('E');
            TB.push_frame();
            {
                TB.single_move('A');
            }
            TB.pop_frame(n - 1);

            n--;
        }
        TB.single_move('E');

        return;
    }

    // sub-triangle size
    long long stsize = (n - 1) / 2;

    // dual triangle
    TB.push_frame();
    {
        TheRecursion(TB, stsize);
        /* TB.push_frame(); */
        /* { */
        /*     TB.single_move('E'); */
        /* } */
        /* TB.pop_frame(stsize); */
    }
    TB.pop_frame(2);

    // go down
    TB.push_frame();
    {
        TB.single_move('C');
    }
    TB.pop_frame(stsize);

    // the rhomboid
    TB.push_frame();
    {
        TB.push_frame();
        {
            TB.single_move('A');
        }
        TB.pop_frame(stsize);

        TB.push_frame();
        {
            TB.single_move('C');
            TB.single_move('E');
        }
        TB.pop_frame(stsize);

        TB.single_move('C');
    }
    TB.pop_frame(stsize);

    // go up
    TB.push_frame();
    {
        TB.single_move('E');
        if (n % 2 == 0) {
            TB.single_move('C');
            TB.single_move('E');
            TB.single_move('A');
        }
        TB.single_move('A');
    }
    TB.pop_frame(2 * stsize);

    // finish
    TB.single_move('E');
    if (n % 2 == 0) {
        TB.single_move('C');
        TB.single_move('E');
        TB.single_move('A');
        TB.single_move('E');
    }
}

int main() {
    ios::sync_with_stdio(false);
    cin.tie(nullptr);

    long long n;
    cin >> n;

    TriangleBuilder TB;
    TB.push_frame();
    TB.single_move('C');
    TB.pop_frame(n);
    TB.push_frame();
    TB.single_move('A');
    TB.pop_frame(n);
    TheRecursion(TB, n);

    cout << TB.to_string() << endl;

    return 0;
}