#include <bits/stdc++.h> using namespace std; bool flip = false; string up_r() { return !flip ? "F" : "C"; } string down_r() { return !flip ? "B" : "A"; } string up_l() { return !flip ? "E" : "D"; } string down_l() { return !flip ? "C" : "F"; } string left() { return !flip ? "D" : "E"; } string right() { return !flip ? "A" : "B"; } void no_top(long long n); map<long long, pair<int,int>> cache[1000123]; pair<int,int> dp(long long n, int len) { if (n == 0) { return {0,0}; } if (n == 1) { return {len, 1}; } auto it = cache[len].find(n); if (it != cache[len].end()) { return it -> second; } pair<int,int> best{INT_MAX, -1}; for (int d = 2; d <= min(9LL, n); d++) { if (d == 7 && n > 1'000'000) { // speed hack continue; } int tmp = dp(n/d, len).first + 3 + dp(n%d, len).first; best = min(best, {tmp, d}); } return cache[len][n] = best; } string REP(long long n, string s) { if (n == 0) { return ""; } if (n == 1) { return s; } pair<int,int> best = dp(n, s.length()); int d = best.second; string total = to_string(d) + "[" + REP(n/d, s) + "]" + REP(n%d, s); assert((int) total.length() == best.first); return total; } void parallelogram(long long h, long long w) { string row = REP(w, up_r() + down_r()) + up_r() + REP(w, left()); cout << REP(h, row); } void no_bottom(long long n) { if (n == 0) { return; } if (n == 1) { cout << up_r() + down_r(); return; } if (n % 2 == 0) { // bottom layer cout << REP(n-1, up_r() + down_r()) + up_r() + REP(n-1, left()); no_bottom(n-1); cout << down_r(); return; } // odd n long long k = n / 2; parallelogram(k, k); cout << REP(k+1, up_r()); flip = !flip; cout << "2["; no_top(k+1); // no_top(k+1); cout << "]"; flip = !flip; cout << down_r(); } void no_top(long long n) { if (n == 0) { return; } if (n == 1) { return; } cout << REP(n-1, right()) + REP(n-1, up_r() + left()) + REP(n-2, down_r() + left()) + down_r(); no_bottom(n-2); } void full(long long n) { no_bottom(n); cout << REP(n, left()); } int main(int argc, char* argv[]) { ios_base::sync_with_stdio(0); cin.tie(0); // full(atoll(argv[1])); long long n; cin >> n; full(n); cout << 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 | #include <bits/stdc++.h> using namespace std; bool flip = false; string up_r() { return !flip ? "F" : "C"; } string down_r() { return !flip ? "B" : "A"; } string up_l() { return !flip ? "E" : "D"; } string down_l() { return !flip ? "C" : "F"; } string left() { return !flip ? "D" : "E"; } string right() { return !flip ? "A" : "B"; } void no_top(long long n); map<long long, pair<int,int>> cache[1000123]; pair<int,int> dp(long long n, int len) { if (n == 0) { return {0,0}; } if (n == 1) { return {len, 1}; } auto it = cache[len].find(n); if (it != cache[len].end()) { return it -> second; } pair<int,int> best{INT_MAX, -1}; for (int d = 2; d <= min(9LL, n); d++) { if (d == 7 && n > 1'000'000) { // speed hack continue; } int tmp = dp(n/d, len).first + 3 + dp(n%d, len).first; best = min(best, {tmp, d}); } return cache[len][n] = best; } string REP(long long n, string s) { if (n == 0) { return ""; } if (n == 1) { return s; } pair<int,int> best = dp(n, s.length()); int d = best.second; string total = to_string(d) + "[" + REP(n/d, s) + "]" + REP(n%d, s); assert((int) total.length() == best.first); return total; } void parallelogram(long long h, long long w) { string row = REP(w, up_r() + down_r()) + up_r() + REP(w, left()); cout << REP(h, row); } void no_bottom(long long n) { if (n == 0) { return; } if (n == 1) { cout << up_r() + down_r(); return; } if (n % 2 == 0) { // bottom layer cout << REP(n-1, up_r() + down_r()) + up_r() + REP(n-1, left()); no_bottom(n-1); cout << down_r(); return; } // odd n long long k = n / 2; parallelogram(k, k); cout << REP(k+1, up_r()); flip = !flip; cout << "2["; no_top(k+1); // no_top(k+1); cout << "]"; flip = !flip; cout << down_r(); } void no_top(long long n) { if (n == 0) { return; } if (n == 1) { return; } cout << REP(n-1, right()) + REP(n-1, up_r() + left()) + REP(n-2, down_r() + left()) + down_r(); no_bottom(n-2); } void full(long long n) { no_bottom(n); cout << REP(n, left()); } int main(int argc, char* argv[]) { ios_base::sync_with_stdio(0); cin.tie(0); // full(atoll(argv[1])); long long n; cin >> n; full(n); cout << endl; } |