#include <bits/stdc++.h> using namespace std; #define endl "\n" #define mp make_pair #define st first #define nd second #define pii pair<int, int> #define vi vector<int> #define pb push_back #define _upgrade ios_base::sync_with_stdio(0), cout.setf(ios::fixed), cout.precision(10), cin.tie(0), cout.tie(0); #define rep(i, n) for (int i = 0; i < (n); ++i) #define fwd(i, a, b) for (int i = (a); i < (b); ++i) #define trav(a, x) for (auto &a : x) #define all(c) (c).begin(), (c).end() #define sz(X) (int)((X).size()) typedef long double ld; typedef unsigned long long ull; typedef long long ll; #ifdef LOCAL ostream &operator<<(ostream &out, string str) { for (char c : str) out << c; return out; } template <class L, class R> ostream &operator<<(ostream &out, pair<L, R> p) { return out << "(" << p.st << ", " << p.nd << ")"; } template <class L, class R, class S> ostream &operator<<(ostream &out, tuple<L, R, S> p) { auto &[a, b, c] = p; return out << "(" << a << ", " << b << ", " << c << ")"; } template <class T> auto operator<<(ostream &out, T a) -> decltype(a.begin(), out) { out << '{'; for (auto it = a.begin(); it != a.end(); it = next(it)) out << (it != a.begin() ? ", " : "") << *it; return out << '}'; } void dump() { cerr << "\n"; } template <class T, class... Ts> void dump(T a, Ts... x) { cerr << a << ", "; dump(x...); } #define debug(...) cerr << "[" #__VA_ARGS__ "]: ", dump(__VA_ARGS__) #else #define debug(...) 42 #endif #define int long long const int BASE = 9; namespace EXP { typedef __int128_t lll; const int MAXN = 1e18; // pow(9, 4) + 22; set<int> S1; void pre() { for (lll i = 1; i < MAXN; i *= 2) for (lll j = i; j < MAXN; j *= 3) for (lll k = j; k < MAXN; k *= 5) for (lll l = k; l < MAXN; l *= 7) S1.insert(l); debug(S1); } pair<int, vector<int>> get(int n) { auto itr = S1.upper_bound(n); auto x = *(--itr); auto rest = n - x; assert(x <= n); debug(x); vector<int> res; for (int t = 9; t > 1; t--) while (x % t == 0) { x /= t; res.push_back(t); } assert(x == 1); return {rest, res}; } string multiply_experimental(string s, int t) { if (t == 0) return ""; auto [rest, X] = get(t); string res = ""; for (auto x : X) res += to_string(x) + "["; res += s; rep(i, sz(X)) res += "]"; debug(t, X, sz(X)); return res + multiply_experimental(s, rest); } } // namespace EXP string multiply_small(string s, int t) { assert(t <= BASE && t >= 0); if (t == 0) return ""; if (t == 1) return s; if (sz(s) == 1) return to_string(t) + s; else return to_string(t) + "[" + s + "]"; } string multiply_normal(string s, int t) { assert(t >= 0); if (t == 0) return ""; int rest = t / BASE; auto x = t % BASE; return multiply_normal(multiply_small(s, BASE), rest) + multiply_small(s, x); } string multiply(string s, int t) { auto s1 = multiply_normal(s, t); auto s2 = EXP::multiply_experimental(s, t); return sz(s1) > sz(s2) ? s2 : s1; } string draw_triangle(int n); string draw_traingle_BL_filled(int n) { if (n == 0) return ""; if (n == 1) return "B"; auto S1 = multiply("BD", n - 1); auto S2 = multiply("BF", n - 1); auto S3 = draw_triangle(n - 2); return S1 + S2 + S3 + "B"; } ////////////////////////////////// NORMAL TRIANGLES string make_n_even(int n) { auto S1 = multiply("D", n); auto S2 = multiply("FB", n - 1); debug("ME", n, sz(S1), sz(S2)); return S1 + S2 + "F"; } string draw_triangle(int n) { debug("DT", n); if (n == 0) return ""; if (n & 1) { auto S1 = make_n_even(n); auto S2 = draw_triangle(n - 1); return S1 + S2 + "B"; } int t = n / 2; auto S1 = multiply("D", n); auto zyg = multiply("FB", t); auto bck = multiply("D", t); auto S2 = multiply(zyg + "F" + bck, t); auto S3 = multiply("F", t); auto TR = draw_traingle_BL_filled(t); auto S4 = multiply(TR, 2); debug(t, sz(S1), sz(S2), sz(S3), sz(S4)); return S1 + S2 + S3 + S4; } //////////////////////////////////// int32_t main() { _upgrade; EXP::pre(); int n; cin >> n; auto S = draw_triangle(n); debug(sz(S)); cout << S << endl; }
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 | #include <bits/stdc++.h> using namespace std; #define endl "\n" #define mp make_pair #define st first #define nd second #define pii pair<int, int> #define vi vector<int> #define pb push_back #define _upgrade ios_base::sync_with_stdio(0), cout.setf(ios::fixed), cout.precision(10), cin.tie(0), cout.tie(0); #define rep(i, n) for (int i = 0; i < (n); ++i) #define fwd(i, a, b) for (int i = (a); i < (b); ++i) #define trav(a, x) for (auto &a : x) #define all(c) (c).begin(), (c).end() #define sz(X) (int)((X).size()) typedef long double ld; typedef unsigned long long ull; typedef long long ll; #ifdef LOCAL ostream &operator<<(ostream &out, string str) { for (char c : str) out << c; return out; } template <class L, class R> ostream &operator<<(ostream &out, pair<L, R> p) { return out << "(" << p.st << ", " << p.nd << ")"; } template <class L, class R, class S> ostream &operator<<(ostream &out, tuple<L, R, S> p) { auto &[a, b, c] = p; return out << "(" << a << ", " << b << ", " << c << ")"; } template <class T> auto operator<<(ostream &out, T a) -> decltype(a.begin(), out) { out << '{'; for (auto it = a.begin(); it != a.end(); it = next(it)) out << (it != a.begin() ? ", " : "") << *it; return out << '}'; } void dump() { cerr << "\n"; } template <class T, class... Ts> void dump(T a, Ts... x) { cerr << a << ", "; dump(x...); } #define debug(...) cerr << "[" #__VA_ARGS__ "]: ", dump(__VA_ARGS__) #else #define debug(...) 42 #endif #define int long long const int BASE = 9; namespace EXP { typedef __int128_t lll; const int MAXN = 1e18; // pow(9, 4) + 22; set<int> S1; void pre() { for (lll i = 1; i < MAXN; i *= 2) for (lll j = i; j < MAXN; j *= 3) for (lll k = j; k < MAXN; k *= 5) for (lll l = k; l < MAXN; l *= 7) S1.insert(l); debug(S1); } pair<int, vector<int>> get(int n) { auto itr = S1.upper_bound(n); auto x = *(--itr); auto rest = n - x; assert(x <= n); debug(x); vector<int> res; for (int t = 9; t > 1; t--) while (x % t == 0) { x /= t; res.push_back(t); } assert(x == 1); return {rest, res}; } string multiply_experimental(string s, int t) { if (t == 0) return ""; auto [rest, X] = get(t); string res = ""; for (auto x : X) res += to_string(x) + "["; res += s; rep(i, sz(X)) res += "]"; debug(t, X, sz(X)); return res + multiply_experimental(s, rest); } } // namespace EXP string multiply_small(string s, int t) { assert(t <= BASE && t >= 0); if (t == 0) return ""; if (t == 1) return s; if (sz(s) == 1) return to_string(t) + s; else return to_string(t) + "[" + s + "]"; } string multiply_normal(string s, int t) { assert(t >= 0); if (t == 0) return ""; int rest = t / BASE; auto x = t % BASE; return multiply_normal(multiply_small(s, BASE), rest) + multiply_small(s, x); } string multiply(string s, int t) { auto s1 = multiply_normal(s, t); auto s2 = EXP::multiply_experimental(s, t); return sz(s1) > sz(s2) ? s2 : s1; } string draw_triangle(int n); string draw_traingle_BL_filled(int n) { if (n == 0) return ""; if (n == 1) return "B"; auto S1 = multiply("BD", n - 1); auto S2 = multiply("BF", n - 1); auto S3 = draw_triangle(n - 2); return S1 + S2 + S3 + "B"; } ////////////////////////////////// NORMAL TRIANGLES string make_n_even(int n) { auto S1 = multiply("D", n); auto S2 = multiply("FB", n - 1); debug("ME", n, sz(S1), sz(S2)); return S1 + S2 + "F"; } string draw_triangle(int n) { debug("DT", n); if (n == 0) return ""; if (n & 1) { auto S1 = make_n_even(n); auto S2 = draw_triangle(n - 1); return S1 + S2 + "B"; } int t = n / 2; auto S1 = multiply("D", n); auto zyg = multiply("FB", t); auto bck = multiply("D", t); auto S2 = multiply(zyg + "F" + bck, t); auto S3 = multiply("F", t); auto TR = draw_traingle_BL_filled(t); auto S4 = multiply(TR, 2); debug(t, sz(S1), sz(S2), sz(S3), sz(S4)); return S1 + S2 + S3 + S4; } //////////////////////////////////// int32_t main() { _upgrade; EXP::pre(); int n; cin >> n; auto S = draw_triangle(n); debug(sz(S)); cout << S << endl; } |