#include <bits/stdc++.h> using namespace std; typedef unsigned uint; typedef long long ll; typedef unsigned long long ull; typedef long double ldbl; typedef pair<int, int> pii; typedef pair<uint, uint> puu; typedef pair<ll, ll> pll; typedef pair<ull, ull> pull; typedef pair<double, double> pdd; typedef vector<int> vi; typedef vector<uint> vu; typedef vector<ll> vll; typedef vector<ull> vull; typedef vector<pii> vpii; typedef vector<puu> vpuu; typedef vector<pll> vpll; typedef vector<pull> vpull; typedef vector<string> vstr; typedef vector<double> vdbl; typedef vector<ldbl> vldbl; #define pb push_back #define ppb pop_back #define pfr push_front #define ppfr pop_front #define emp emplace #define empb emplace_back #define be begin #define rbe rbegin #define all(x) (x).be(), (x).end() #define rall(x) (x).rbe(), (x).rend() #define fir first #define sec second #define mkp make_pair #define brif(cond) if (cond) break #define ctif(cond) if (cond) continue #define retif(cond) if (cond) return void canhazfast() {ios_base::sync_with_stdio(false);cin.tie(nullptr);cout.tie(nullptr);} template<typename T> T gcd(T a, T b) {return b ? gcd(b, a%b) : a;} template<typename T> T extgcd(T a, T b, T &x, T &y) { T x0 = 1, y0 = 0, x1 = 0, y1 = 1; while (b) { T q = a/b; a %= b; swap(a, b); x0 -= q*x1; swap(x0, x1); y0 -= q*y1; swap(y0, y1); } x = x0; y = y0; return a; } int ctz(uint x) {return __builtin_ctz(x);} int ctzll(ull x) {return __builtin_ctzll(x);} int clz(uint x) {return __builtin_clz(x);} int clzll(ull x) {return __builtin_clzll(x);} int popcnt(uint x) {return __builtin_popcount(x);} int popcntll(ull x) {return __builtin_popcountll(x);} int bsr(uint x) {return 31^clz(x);} int bsrll(ull x) {return 63^clzll(x);} /// CCW string closed_tri(ll n); /// V: BR -> TL, E: all string open_tri(ll n); /// V: BR -> TL, E: TR string rect(ll w, ll h); /// V: TL -> BL, E: T, L, R void append_small_repeat(string &dst, const string &src, int k) { switch (k) { case 0: return; case 1: dst += src; return; case 2: if (src.size() == 2) { dst += src; dst += src; return; } } dst += k+'0'; if (src.size() > 1) { dst += '['; dst += src; dst += ']'; } else { dst += src[0]; } } string repeat(const string &s, ll n) { string res; int depth = 0; while (n > 9) { int r = 0, d = 0; for (int i = 9; i >= 2; --i) { ll q = n/i; r = n%i; if (r == 0) { n = q; d = i; break; } } if (d == 0) { d = 9; r = n%9; n /= 9; } append_small_repeat(res, s, r); res += d+'0'; res += '['; ++depth; } append_small_repeat(res, s, n); for (int i = 0; i < depth; ++i) res += ']'; return res; } string closed_tri(ll n) { retif(n == 1) "ECA"; retif(n == 3) "3ECACEC2ACECEC3A"; ///int d = min(n, 2LL); constexpr int d = 2; ll q = n/d, r = n%d; string res = repeat(open_tri(q), d); if (r) { res += open_tri(r); res += repeat("C", r); res += rect(r, q); } else { res += repeat("C", q); } for (int i = 1; i < d; ++i) res += rect(i*q+r, q); ///res += rect(q+r, q); res += repeat("A", n); return res; } string open_tri(ll n) { retif(n == 1) "E"; retif(n == 2) "ECEAE"; return 'E'+closed_tri(n-2)+repeat("CE", n-1)+repeat("AE", n-1); } string rect_rmaj(ll w, ll h) { string row = repeat("A", w)+repeat("CE", w)+'C'; string alt_row = repeat("A", w)+repeat("CE", w/2)+'C'+repeat("EC", w-w/2); if (alt_row.size() < row.size()) swap(row, alt_row); return repeat(row, h); } string rect_cmaj(ll w, ll h) { string col = repeat("EA", h-1)+'E'; ///string alt_col = ... col += repeat("C", h); return repeat("A", w)+repeat("C", h)+repeat(col, w); } string rect(ll w, ll h) { string r = rect_rmaj(w, h); string c = rect_cmaj(w, h); return c.size() < r.size() ? c : r; } int main() { canhazfast(); ll n; string ans; cin >> n; ans = closed_tri(n); cout << ans << '\n'; //cerr << "len = " << ans.size() << '\n'; /**mt19937_64 rng(chrono::steady_clock::now().time_since_epoch().count()); uniform_int_distribution<ll> dist(1LL, 1000000000000000000LL); constexpr int iters = 2e5; size_t lmax = 0; for (int i = 0; i < iters; ++i) { n = dist(rng); ans = closed_tri(n); size_t len = ans.size(); if (lmax < len) { lmax = len; cerr << "lmax = " << lmax << '\n'; } if ((i+1)%10000 == 0) cerr << i+1 << " iters\n"; }**/ 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 | #include <bits/stdc++.h> using namespace std; typedef unsigned uint; typedef long long ll; typedef unsigned long long ull; typedef long double ldbl; typedef pair<int, int> pii; typedef pair<uint, uint> puu; typedef pair<ll, ll> pll; typedef pair<ull, ull> pull; typedef pair<double, double> pdd; typedef vector<int> vi; typedef vector<uint> vu; typedef vector<ll> vll; typedef vector<ull> vull; typedef vector<pii> vpii; typedef vector<puu> vpuu; typedef vector<pll> vpll; typedef vector<pull> vpull; typedef vector<string> vstr; typedef vector<double> vdbl; typedef vector<ldbl> vldbl; #define pb push_back #define ppb pop_back #define pfr push_front #define ppfr pop_front #define emp emplace #define empb emplace_back #define be begin #define rbe rbegin #define all(x) (x).be(), (x).end() #define rall(x) (x).rbe(), (x).rend() #define fir first #define sec second #define mkp make_pair #define brif(cond) if (cond) break #define ctif(cond) if (cond) continue #define retif(cond) if (cond) return void canhazfast() {ios_base::sync_with_stdio(false);cin.tie(nullptr);cout.tie(nullptr);} template<typename T> T gcd(T a, T b) {return b ? gcd(b, a%b) : a;} template<typename T> T extgcd(T a, T b, T &x, T &y) { T x0 = 1, y0 = 0, x1 = 0, y1 = 1; while (b) { T q = a/b; a %= b; swap(a, b); x0 -= q*x1; swap(x0, x1); y0 -= q*y1; swap(y0, y1); } x = x0; y = y0; return a; } int ctz(uint x) {return __builtin_ctz(x);} int ctzll(ull x) {return __builtin_ctzll(x);} int clz(uint x) {return __builtin_clz(x);} int clzll(ull x) {return __builtin_clzll(x);} int popcnt(uint x) {return __builtin_popcount(x);} int popcntll(ull x) {return __builtin_popcountll(x);} int bsr(uint x) {return 31^clz(x);} int bsrll(ull x) {return 63^clzll(x);} /// CCW string closed_tri(ll n); /// V: BR -> TL, E: all string open_tri(ll n); /// V: BR -> TL, E: TR string rect(ll w, ll h); /// V: TL -> BL, E: T, L, R void append_small_repeat(string &dst, const string &src, int k) { switch (k) { case 0: return; case 1: dst += src; return; case 2: if (src.size() == 2) { dst += src; dst += src; return; } } dst += k+'0'; if (src.size() > 1) { dst += '['; dst += src; dst += ']'; } else { dst += src[0]; } } string repeat(const string &s, ll n) { string res; int depth = 0; while (n > 9) { int r = 0, d = 0; for (int i = 9; i >= 2; --i) { ll q = n/i; r = n%i; if (r == 0) { n = q; d = i; break; } } if (d == 0) { d = 9; r = n%9; n /= 9; } append_small_repeat(res, s, r); res += d+'0'; res += '['; ++depth; } append_small_repeat(res, s, n); for (int i = 0; i < depth; ++i) res += ']'; return res; } string closed_tri(ll n) { retif(n == 1) "ECA"; retif(n == 3) "3ECACEC2ACECEC3A"; ///int d = min(n, 2LL); constexpr int d = 2; ll q = n/d, r = n%d; string res = repeat(open_tri(q), d); if (r) { res += open_tri(r); res += repeat("C", r); res += rect(r, q); } else { res += repeat("C", q); } for (int i = 1; i < d; ++i) res += rect(i*q+r, q); ///res += rect(q+r, q); res += repeat("A", n); return res; } string open_tri(ll n) { retif(n == 1) "E"; retif(n == 2) "ECEAE"; return 'E'+closed_tri(n-2)+repeat("CE", n-1)+repeat("AE", n-1); } string rect_rmaj(ll w, ll h) { string row = repeat("A", w)+repeat("CE", w)+'C'; string alt_row = repeat("A", w)+repeat("CE", w/2)+'C'+repeat("EC", w-w/2); if (alt_row.size() < row.size()) swap(row, alt_row); return repeat(row, h); } string rect_cmaj(ll w, ll h) { string col = repeat("EA", h-1)+'E'; ///string alt_col = ... col += repeat("C", h); return repeat("A", w)+repeat("C", h)+repeat(col, w); } string rect(ll w, ll h) { string r = rect_rmaj(w, h); string c = rect_cmaj(w, h); return c.size() < r.size() ? c : r; } int main() { canhazfast(); ll n; string ans; cin >> n; ans = closed_tri(n); cout << ans << '\n'; //cerr << "len = " << ans.size() << '\n'; /**mt19937_64 rng(chrono::steady_clock::now().time_since_epoch().count()); uniform_int_distribution<ll> dist(1LL, 1000000000000000000LL); constexpr int iters = 2e5; size_t lmax = 0; for (int i = 0; i < iters; ++i) { n = dist(rng); ans = closed_tri(n); size_t len = ans.size(); if (lmax < len) { lmax = len; cerr << "lmax = " << lmax << '\n'; } if ((i+1)%10000 == 0) cerr << i+1 << " iters\n"; }**/ return 0; } |