#include <cstdio> #include <vector> #include <string> #include <unordered_map> using namespace std; typedef long long lol; typedef pair<int, int> pii; struct Key { int first; lol second; Key(int a, lol b) : first(a), second(b) {} bool operator==(const Key &oth) const { return first == oth.first && second == oth.second; } }; template <> struct std::hash<Key> { std::size_t operator()(const Key &key) const { return (hash<int>()(key.first)) ^ (hash<lol>()(key.second)); } }; unordered_map<Key, pii> mem; int minMulti(int len, lol n) { if (0 == n) return 0; if (1 == n) return len; if (n <= 9) { if (1 == len) return 2; if (2 == len && 2 == n) return 4; return len + 3; } Key key(len, n); if (mem.count(key) > 0) return mem[key].first; int res = 987654321; int b = 0; for (int mn=2; mn<=9; ++mn) { int cur = 3 + minMulti(len, n / mn) + minMulti(len, n % mn); if (cur < res) { res = cur; b = mn; } } mem[key] = { res, b }; return res; } int apply(lol n, const char *s, int sz, char *buf) { if (0 == n) return 0; if (1 == n) { for (int i=0; i<sz; ++i) buf[i] = s[i]; return sz; } if (n <= 9) { if (1 == sz) { buf[0] = "0123456789"[n]; buf[1] = s[0]; return 2; } if (2 == sz && 2 == n) { buf[0] = s[0]; buf[1] = s[1]; buf[2] = s[0]; buf[3] = s[1]; return 4; } buf[0] = "0123456789"[n]; buf[1] = '['; for (int i=0; i<sz; ++i) buf[2 + i] = s[i]; buf[sz + 2] = ']'; return 3 + sz; } Key key(sz, n); int cnt = minMulti(sz, n); int mn = mem[key].second; int pos = apply(n % mn, s, sz, buf); buf[pos + 0] = "0123456789"[mn]; buf[pos + 1] = '['; pos += 2; pos += apply(n / mn, s, sz, buf + pos); buf[pos++] = ']'; return pos; } int apply(lol n, const string &s, char *buf) { if (0 == n) return 0; if (1 == n) { for (int i=0; i<s.size(); ++i) buf[i] = s[i]; return s.size(); } if (n <= 9) { if (1 == s.size()) { buf[0] = "0123456789"[n]; buf[1] = s[0]; return 2; } if (2 == s.size() && 2 == n) { buf[0] = s[0]; buf[1] = s[1]; buf[2] = s[0]; buf[3] = s[1]; return 4; } buf[0] = "0123456789"[n]; buf[1] = '['; for (int i=0; i<s.size(); ++i) buf[2 + i] = s[i]; buf[s.size() + 2] = ']'; return 3 + s.size(); } int sz = s.size(); Key key(sz, n); int cnt = minMulti(sz, n); int mn = mem[key].second; int pos = apply(n % mn, s, buf); buf[pos + 0] = "0123456789"[mn]; buf[pos + 1] = '['; pos += 2; pos += apply(n / mn, s, buf + pos); buf[pos++] = ']'; return pos; } string sD; string sF; string sDB; string sBF; string sEACD; string sBDBFB; string sBDBFF; char bufPR[200000]; int applyPR(lol n, char *buf) { if (1 == n) { return apply(1, sEACD, buf); } int pos = 0; pos += apply(n, sF, bufPR + pos); pos += apply(n, sDB, bufPR + pos); bufPR[pos++] = 'D'; bufPR[pos] = 0; pos = apply(n, bufPR, pos, buf); return pos; } int applyTR(lol n, char *buf) { if (n <= 2) { if (1 == n) { buf[0] = 'B'; return 1; } if (2 == n) { return apply(1, sBDBFB, buf); } } lol k = (n - 1) / 2; int pos = 0; buf[pos++] = '2'; buf[pos++] = '['; pos += applyTR(k, buf + pos); buf[pos++] = ']'; pos += apply(k, sD, buf + pos); pos += applyPR(k, buf + pos); if (n % 2 != 0) { pos += apply(k*2, sBF, buf + pos); buf[pos++] = 'B'; } else { pos += apply(k*2, sBDBFF, buf + pos); pos += apply(1, sBDBFB, buf + pos); } return pos; } void init() { sD = string("D"); sF = string("F"); sDB = string("DB"); sBF = string("BF"); sEACD = string("EACD"); sBDBFB = string("BDBFB"); sBDBFF = string("BDBFF"); } char res[900000]; int main() { init(); lol n; scanf("%lld", &n); int pos = 0; pos += applyTR(n, res + pos); pos += apply(n, sD, res + pos); pos += apply(n, sF, res + pos); res[pos] = 0; printf("%s\n", res); return 0; }
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 200 201 202 203 204 205 206 207 208 | #include <cstdio> #include <vector> #include <string> #include <unordered_map> using namespace std; typedef long long lol; typedef pair<int, int> pii; struct Key { int first; lol second; Key(int a, lol b) : first(a), second(b) {} bool operator==(const Key &oth) const { return first == oth.first && second == oth.second; } }; template <> struct std::hash<Key> { std::size_t operator()(const Key &key) const { return (hash<int>()(key.first)) ^ (hash<lol>()(key.second)); } }; unordered_map<Key, pii> mem; int minMulti(int len, lol n) { if (0 == n) return 0; if (1 == n) return len; if (n <= 9) { if (1 == len) return 2; if (2 == len && 2 == n) return 4; return len + 3; } Key key(len, n); if (mem.count(key) > 0) return mem[key].first; int res = 987654321; int b = 0; for (int mn=2; mn<=9; ++mn) { int cur = 3 + minMulti(len, n / mn) + minMulti(len, n % mn); if (cur < res) { res = cur; b = mn; } } mem[key] = { res, b }; return res; } int apply(lol n, const char *s, int sz, char *buf) { if (0 == n) return 0; if (1 == n) { for (int i=0; i<sz; ++i) buf[i] = s[i]; return sz; } if (n <= 9) { if (1 == sz) { buf[0] = "0123456789"[n]; buf[1] = s[0]; return 2; } if (2 == sz && 2 == n) { buf[0] = s[0]; buf[1] = s[1]; buf[2] = s[0]; buf[3] = s[1]; return 4; } buf[0] = "0123456789"[n]; buf[1] = '['; for (int i=0; i<sz; ++i) buf[2 + i] = s[i]; buf[sz + 2] = ']'; return 3 + sz; } Key key(sz, n); int cnt = minMulti(sz, n); int mn = mem[key].second; int pos = apply(n % mn, s, sz, buf); buf[pos + 0] = "0123456789"[mn]; buf[pos + 1] = '['; pos += 2; pos += apply(n / mn, s, sz, buf + pos); buf[pos++] = ']'; return pos; } int apply(lol n, const string &s, char *buf) { if (0 == n) return 0; if (1 == n) { for (int i=0; i<s.size(); ++i) buf[i] = s[i]; return s.size(); } if (n <= 9) { if (1 == s.size()) { buf[0] = "0123456789"[n]; buf[1] = s[0]; return 2; } if (2 == s.size() && 2 == n) { buf[0] = s[0]; buf[1] = s[1]; buf[2] = s[0]; buf[3] = s[1]; return 4; } buf[0] = "0123456789"[n]; buf[1] = '['; for (int i=0; i<s.size(); ++i) buf[2 + i] = s[i]; buf[s.size() + 2] = ']'; return 3 + s.size(); } int sz = s.size(); Key key(sz, n); int cnt = minMulti(sz, n); int mn = mem[key].second; int pos = apply(n % mn, s, buf); buf[pos + 0] = "0123456789"[mn]; buf[pos + 1] = '['; pos += 2; pos += apply(n / mn, s, buf + pos); buf[pos++] = ']'; return pos; } string sD; string sF; string sDB; string sBF; string sEACD; string sBDBFB; string sBDBFF; char bufPR[200000]; int applyPR(lol n, char *buf) { if (1 == n) { return apply(1, sEACD, buf); } int pos = 0; pos += apply(n, sF, bufPR + pos); pos += apply(n, sDB, bufPR + pos); bufPR[pos++] = 'D'; bufPR[pos] = 0; pos = apply(n, bufPR, pos, buf); return pos; } int applyTR(lol n, char *buf) { if (n <= 2) { if (1 == n) { buf[0] = 'B'; return 1; } if (2 == n) { return apply(1, sBDBFB, buf); } } lol k = (n - 1) / 2; int pos = 0; buf[pos++] = '2'; buf[pos++] = '['; pos += applyTR(k, buf + pos); buf[pos++] = ']'; pos += apply(k, sD, buf + pos); pos += applyPR(k, buf + pos); if (n % 2 != 0) { pos += apply(k*2, sBF, buf + pos); buf[pos++] = 'B'; } else { pos += apply(k*2, sBDBFF, buf + pos); pos += apply(1, sBDBFB, buf + pos); } return pos; } void init() { sD = string("D"); sF = string("F"); sDB = string("DB"); sBF = string("BF"); sEACD = string("EACD"); sBDBFB = string("BDBFB"); sBDBFF = string("BDBFF"); } char res[900000]; int main() { init(); lol n; scanf("%lld", &n); int pos = 0; pos += applyTR(n, res + pos); pos += apply(n, sD, res + pos); pos += apply(n, sF, res + pos); res[pos] = 0; printf("%s\n", res); return 0; } |