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
#include <iostream>
#include <unordered_map>
#include <cassert>
#include <random>

using namespace std;

// #define DEBUG
#ifdef DEBUG
#define dassert(x) assert(x);
#define df(...) printf(__VA_ARGS__)
#else
#define dassert(x)
#define df(...)
#endif

#define x first
#define y second
#define mp make_pair
#define pb push_back
#define ir(x, a, b) ((a) <= (x) && (x) <= (b))
#define vec vector
#define sz(x) (ll)x.size()
#define foru(i, n) for (ll i = 0; (i) < (n); ++(i))
#define all(x) (x).begin(), (x).end()

typedef int64_t ll;

const string l =  "D";
const string r =  "A";
const string ld = "C";
const string lu = "E";
const string ru = "F";
const string rd = "B";

using choice = pair<pair<ll, ll>, ll>;
    
unordered_map<ll, choice> M;

choice calc(ll n) {
    if (M.count(n) > 0) {
        return M[n];
    }
    
    ll choice = -1;
    pair<ll, ll> cost = {1e18, 1e18};
    
    for (ll i = 2; i <= 9; ++i) {
        pair<ll, ll> c0 = calc(n/i).x;
        pair<ll, ll> c1 = calc(n%i).x;
        pair<ll, ll> c = {c0.x + c1.x, c0.y + c1.y};
        if (c < cost) {
            cost = c;
            choice = i;
        }
    }
            
    M[n] = {cost, choice};
    return {cost, choice};
}

string wrap(const string& s, ll n) {
    if (n == 0) return "";
    else if (n == 1) return s;
    else return to_string(n) + "[" + s + "]";
}

string loop(const string& x, ll n) {
    if (n < 10) return wrap(x, n);
    else {
        calc(n);
        ll div = M[n].y;
        return wrap(loop(x, n/div), div) + loop(x, n%div);
    }
}

// def block(n):
//     return (LD.loop(n) + (LU + R).loop(n) + LU).loop(n)
string block(ll n) {
    return loop(loop(ld, n) + loop(lu + r, n) + lu, n);
}

string combine(string s, ll n) {
    return loop(s, 2) + loop(lu, n) + block(n);
}

string tri(ll n) {
    // cout << n << endl;
    if (n == 1) return r;
    if (n == 2) return r + lu + r + ld + r;
    else if (n % 2 == 0) {
        ll x = n/2-1;
        string t = tri(x);
        string a = r+ru+l+rd + loop(r+ld+lu+ld+r, n-2) + r;
        string b = loop(r+ld, n-2)+r + loop(lu, n-1) + loop(r+ld, n-1)+r;
        if (b.size() < a.size()) a=b;
        return combine(t, x) + a;
    } else {
        ll x = (n-1)/2;
        string t = tri(x);
        string a = loop(r+ld, n-1)+r;
        return combine(t, x) + a;
    }
}
//
// def tri(n):
//     if n == 1:
//         return R
//     if n == 2:
//         return R+LU+R+LD+R
//     elif n % 2 == 0:
//         x = n//2 - 1
//         t = tri(x)
//         a = R+RU+L+RD + (R+LD+LU+LD+R).loop(n-2) + R
//         b = (R+LD).loop(n-2)+R + LU.loop(n-1) + (R+LD).loop(n-1)+R
//         return combine(t, x) + min(a, b)
//         # return b
//     else:
//         x = (n-1)//2
//         t = tri(x)
//         a = (R + LD).loop(n-1)+R
//         return combine(t, x) + a

int main() {
    ios::sync_with_stdio(0); cin.tie(0); cout.tie(0);
    M[0] = {{0, 0}, 0};
    M[1] = {{1, 0}, 1};
    for (ll i = 2; i <= 9; ++i) {
        M[i] = {{1, 1}, i};
    }
    
#ifdef TEST
    random_device rd;
    mt19937_64 rng(rd());
    for (;;) {
        ll n = uniform_int_distribution<ll>(1, ll(1e18))(rng);
        assert((tri(n) + loop(lu, n) + loop(ld, n)).size() < 100000);
        cout << "correct" << endl;
    }
#else
    ll n;
    cin >> n;
    cout << tri(n) + loop(lu, n) + loop(ld, n);
#endif
    
    
    return 0;
}