#include <bits/stdc++.h> using namespace std; #define REP(i, n) for (int i = 0; i < (n); ++i) #define EACH(it, cont) for (auto &it : cont) #define FOR(i, b, e) for (int i = (b); i <= (e); ++i) #define FORD(i, b, e) for (int i = (b); i >= (e); --i) #define ALL(c) begin(c), end(c) template <class T, class U> bool remin(T &a, const U &b) { return b < a ? a = b, true : false; } template <class T, class U> bool remax(T &a, const U &b) { return b > a ? a = b, true : false; } const int inf = 1000000001; using ll = long long; // simply multiply 's' by 'n' // all the block ifs are just an opt string f(ll n, const string &s) { if (n == 0) { return ""; } if (n == 1) { return s; } if (s.size() <= 2 && n == 2) { return s + s; } if (s.size() == 1) { if (n <= 9) return char(n+'0') + s; if ( n <= 19 || n == 22 || n == 23 || n == 26 || n == 29 || n == 33 || n == 34 || n == 39 || n == 46 || n == 58 || n == 62 || n == 69 || n == 82 || n == 86 || n == 87 || n == 93 || n == 94 ) { return f(9, s) + f(n-9, s); } } if (n <= 9) { return char(n+'0') + ("[" + s + "]"); } if (s.size() == 2) { if ( n == 13 || n == 17 || n == 58 || n == 61 || n == 69 || n == 71 || n == 74 || n == 92 /* only if post processing opt enabled */ // || n == 23, 34, 38, 44, 50, 51, 66, 73, 85, 91, 94, 99 ) { return f(n-1, s) + s; } } if (s.size() == 3) { if (n == 11 || n == 23) { return f(n-2, s) + f(2, s); } } if (s.size() >= 2) { if ( n == 22 || n == 26 || n == 29 || n == 31 || n == 33 || n == 41 || n == 43 || n == 46 || n == 55 || n == 57 || n == 65 || n == 82 || n == 97 || n == 101 || n == 103 || n == 106 || n == 107 || n == 113 || n == 121 || n == 151 || n == 157 ) { return f(n-1, s) + s; } } FORD (d, 9, 2) { if (n % d == 0) { return f(d, f(n/d, s)); } } int r = n % 9; return f(9, f(n/9, s)) + f(r, s); } void say(const string &s) { printf("%s", s.c_str()); } void all_butJ(ll n); void do_F(ll n); void do_GH(ll n1, ll n2); void do_rigg(int h, ll w); // divide whole thing into 4 blocks: // , // /F\ . // /-x-\ . // /G|H|I\ . // --------- // or without edges: // F // GHI // where all are triangles approx 1/4 size of N // and H is upside-down. // leftmost edge of GF is called J // // 1st step is to print GH in very efficient way // // then I is recurisvely the same problem // then F is recurisvely the same problem // Finally we are left with J. // // Observation that enables us to move from O(n) -> O(log n) is: // replace sol(I)+sol(F) by 2[sol(I')], where I' and F' are sligthly modified. void all(ll n) { all_butJ(n); // J say(f(n, "C")); } // recursive helper, must finish with cursor on the very top // (,) on the ASCII-art above void all_butJ(ll n) { // smallest/stop case if (n == 1) { say("AE"); return; } if (n == 2) { say("AEACAEE"); return; } // for odd n, F is smaller than I by 1 ll n1 = n / 2; // width of GHI ll n2 = n-n1; // height of GHI if (n >= 4 && n % 2 == 0) { n1--; n2++; } do_GH(n1, n2); if (n2 <= 2) { all_butJ(n2); do_F(n1-1); return; } // rigg to the right /-\_/-\_/-\_ int rigg = 0; if (n1 + 1 == n2) { rigg = 1; } else if (n1 + 2 == n2) { rigg = 2; } if (rigg) { do_rigg(rigg, n1); } else { do_rigg(0, 1); } // GH painted, I & F left, but bottom lines already done // and cursor is on the bad side, so do zig-zag to the left say("2["); do_F(n1-1); // 2[F] == IF say("]"); } // rigg to the right /-\_/-\_/-\_ void do_rigg(int h, ll w) { if (h == 1) { say(f(w, "AEAC")+"AE"); } else if (h == 2) { say(f(w, "AEAEACC")+"AEACAEE"); } else if (h == 0) { say("AE"); } } void do_GH(ll n1, ll n2) { string up = f(n2, "AE"); string dn = f(n2, "C"); string right = f(n1, up + "A" + dn); say(right); // GH painted, n1 x n2 shape, currsor at the bottom right of G // ALL edges of H done, left edge of G left (pun, hehe) } void do_F(ll n1) { // do do zig-zag say("E"); // one up say(f(n1, "CE")); // we are on the good side, but with one row of pyramid painted already if (!n1) return; // problem is the same as before now all_butJ(n1); } int main() { ll n; cin >> n; all(n); printf("\n"); }
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 209 | #include <bits/stdc++.h> using namespace std; #define REP(i, n) for (int i = 0; i < (n); ++i) #define EACH(it, cont) for (auto &it : cont) #define FOR(i, b, e) for (int i = (b); i <= (e); ++i) #define FORD(i, b, e) for (int i = (b); i >= (e); --i) #define ALL(c) begin(c), end(c) template <class T, class U> bool remin(T &a, const U &b) { return b < a ? a = b, true : false; } template <class T, class U> bool remax(T &a, const U &b) { return b > a ? a = b, true : false; } const int inf = 1000000001; using ll = long long; // simply multiply 's' by 'n' // all the block ifs are just an opt string f(ll n, const string &s) { if (n == 0) { return ""; } if (n == 1) { return s; } if (s.size() <= 2 && n == 2) { return s + s; } if (s.size() == 1) { if (n <= 9) return char(n+'0') + s; if ( n <= 19 || n == 22 || n == 23 || n == 26 || n == 29 || n == 33 || n == 34 || n == 39 || n == 46 || n == 58 || n == 62 || n == 69 || n == 82 || n == 86 || n == 87 || n == 93 || n == 94 ) { return f(9, s) + f(n-9, s); } } if (n <= 9) { return char(n+'0') + ("[" + s + "]"); } if (s.size() == 2) { if ( n == 13 || n == 17 || n == 58 || n == 61 || n == 69 || n == 71 || n == 74 || n == 92 /* only if post processing opt enabled */ // || n == 23, 34, 38, 44, 50, 51, 66, 73, 85, 91, 94, 99 ) { return f(n-1, s) + s; } } if (s.size() == 3) { if (n == 11 || n == 23) { return f(n-2, s) + f(2, s); } } if (s.size() >= 2) { if ( n == 22 || n == 26 || n == 29 || n == 31 || n == 33 || n == 41 || n == 43 || n == 46 || n == 55 || n == 57 || n == 65 || n == 82 || n == 97 || n == 101 || n == 103 || n == 106 || n == 107 || n == 113 || n == 121 || n == 151 || n == 157 ) { return f(n-1, s) + s; } } FORD (d, 9, 2) { if (n % d == 0) { return f(d, f(n/d, s)); } } int r = n % 9; return f(9, f(n/9, s)) + f(r, s); } void say(const string &s) { printf("%s", s.c_str()); } void all_butJ(ll n); void do_F(ll n); void do_GH(ll n1, ll n2); void do_rigg(int h, ll w); // divide whole thing into 4 blocks: // , // /F\ . // /-x-\ . // /G|H|I\ . // --------- // or without edges: // F // GHI // where all are triangles approx 1/4 size of N // and H is upside-down. // leftmost edge of GF is called J // // 1st step is to print GH in very efficient way // // then I is recurisvely the same problem // then F is recurisvely the same problem // Finally we are left with J. // // Observation that enables us to move from O(n) -> O(log n) is: // replace sol(I)+sol(F) by 2[sol(I')], where I' and F' are sligthly modified. void all(ll n) { all_butJ(n); // J say(f(n, "C")); } // recursive helper, must finish with cursor on the very top // (,) on the ASCII-art above void all_butJ(ll n) { // smallest/stop case if (n == 1) { say("AE"); return; } if (n == 2) { say("AEACAEE"); return; } // for odd n, F is smaller than I by 1 ll n1 = n / 2; // width of GHI ll n2 = n-n1; // height of GHI if (n >= 4 && n % 2 == 0) { n1--; n2++; } do_GH(n1, n2); if (n2 <= 2) { all_butJ(n2); do_F(n1-1); return; } // rigg to the right /-\_/-\_/-\_ int rigg = 0; if (n1 + 1 == n2) { rigg = 1; } else if (n1 + 2 == n2) { rigg = 2; } if (rigg) { do_rigg(rigg, n1); } else { do_rigg(0, 1); } // GH painted, I & F left, but bottom lines already done // and cursor is on the bad side, so do zig-zag to the left say("2["); do_F(n1-1); // 2[F] == IF say("]"); } // rigg to the right /-\_/-\_/-\_ void do_rigg(int h, ll w) { if (h == 1) { say(f(w, "AEAC")+"AE"); } else if (h == 2) { say(f(w, "AEAEACC")+"AEACAEE"); } else if (h == 0) { say("AE"); } } void do_GH(ll n1, ll n2) { string up = f(n2, "AE"); string dn = f(n2, "C"); string right = f(n1, up + "A" + dn); say(right); // GH painted, n1 x n2 shape, currsor at the bottom right of G // ALL edges of H done, left edge of G left (pun, hehe) } void do_F(ll n1) { // do do zig-zag say("E"); // one up say(f(n1, "CE")); // we are on the good side, but with one row of pyramid painted already if (!n1) return; // problem is the same as before now all_butJ(n1); } int main() { ll n; cin >> n; all(n); printf("\n"); } |