#include <cstdio> #include <vector> #include <algorithm> #include <string> #include <cstring> #include <sstream> #include <iostream> #include <map> #include <cmath> #include <set> #include <queue> #include <numeric> #include <ctime> using namespace std; typedef long long LL; #define INF 1000000000 #define SZ(v) ((int)(v).size()) #define FOR(i,a,b) for(int i=(a);i<(b);i++) #define FORE(i,a,b) for(int i=(a);i<=(b);i++) #define FS first #define SD second #define MP make_pair #define ZERO(t) memset(t, 0, sizeof(t)) #define MINUS(t) memset(t, -1, sizeof(t)) #define MOD 1000000007 #define MIN(_a,_b) ((_a)<(_b)?(_a):(_b)) #define MAX(_a,_b) ((_a)>(_b)?(_a):(_b)) int BEST[10000][2]; int M[10000][2]; int go(int k, int s) { if (BEST[k][s] != -1) return BEST[k][s]; if (k == 0) return 0; if (k == 1) return s; int best = INF; best = s + go(k - 1, s); M[k][s] = 1; FORE(i,2,MIN(9, k)) { int cand = (s == 1) ? 2 : 5; cand += go(k - i, s); if (cand < best) { M[k][s] = i; best = cand; } } FORE(i,2,9) { if (k % i == 0) { int cand = go(k / i, s) + 3; if (cand < best) { M[k][s] = 10 + i; best = cand; } } } return BEST[k][s] = best; } void show(string s, int k) { if (k == 0) return; if (k == 1) { printf("%s", s.c_str()); } else { go(k, SZ(s)); int m = M[k][SZ(s)]; if (m == 1) { printf("%s", s.c_str()); show(s, k - 1); } else if (m <= 9) { if (SZ(s) == 1) printf("%d%s", m, s.c_str()); else printf("%d[%s]", m, s.c_str()); show(s, k - m); } else { m -= 10; printf("%d[",m); show(s, k / m); printf("]"); } } } int main() { int n;scanf("%d", &n); MINUS(BEST); for (int i = n; i >= 1; --i) { show("FB", i - 1); show("F", 1); show("D", i - 1); } show("B", n); show("D", 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 | #include <cstdio> #include <vector> #include <algorithm> #include <string> #include <cstring> #include <sstream> #include <iostream> #include <map> #include <cmath> #include <set> #include <queue> #include <numeric> #include <ctime> using namespace std; typedef long long LL; #define INF 1000000000 #define SZ(v) ((int)(v).size()) #define FOR(i,a,b) for(int i=(a);i<(b);i++) #define FORE(i,a,b) for(int i=(a);i<=(b);i++) #define FS first #define SD second #define MP make_pair #define ZERO(t) memset(t, 0, sizeof(t)) #define MINUS(t) memset(t, -1, sizeof(t)) #define MOD 1000000007 #define MIN(_a,_b) ((_a)<(_b)?(_a):(_b)) #define MAX(_a,_b) ((_a)>(_b)?(_a):(_b)) int BEST[10000][2]; int M[10000][2]; int go(int k, int s) { if (BEST[k][s] != -1) return BEST[k][s]; if (k == 0) return 0; if (k == 1) return s; int best = INF; best = s + go(k - 1, s); M[k][s] = 1; FORE(i,2,MIN(9, k)) { int cand = (s == 1) ? 2 : 5; cand += go(k - i, s); if (cand < best) { M[k][s] = i; best = cand; } } FORE(i,2,9) { if (k % i == 0) { int cand = go(k / i, s) + 3; if (cand < best) { M[k][s] = 10 + i; best = cand; } } } return BEST[k][s] = best; } void show(string s, int k) { if (k == 0) return; if (k == 1) { printf("%s", s.c_str()); } else { go(k, SZ(s)); int m = M[k][SZ(s)]; if (m == 1) { printf("%s", s.c_str()); show(s, k - 1); } else if (m <= 9) { if (SZ(s) == 1) printf("%d%s", m, s.c_str()); else printf("%d[%s]", m, s.c_str()); show(s, k - m); } else { m -= 10; printf("%d[",m); show(s, k / m); printf("]"); } } } int main() { int n;scanf("%d", &n); MINUS(BEST); for (int i = n; i >= 1; --i) { show("FB", i - 1); show("F", 1); show("D", i - 1); } show("B", n); show("D", n); printf("\n"); } |