#include <cstdio> #include <cinttypes> #include <string> #include <vector> using namespace std; void DrawTriangle(string& instructions, int64_t size); void DrawParallelogram(string& instructions, int64_t sizeX, int64_t sizeY); void DrawRightZigZag(string& instructions, int64_t size); void DrawRight(string& instructions, int64_t size); void DrawDownLeft(string& instructions, int64_t size); void DrawLeftZig(string& instructions, int64_t size); string SimpleRepeatPattern(string pattern, int times); string RepeatPattern(string pattern, int64_t times); void DrawTriangle1(string& instructions) { instructions += "AE"; } void DrawTriangle2(string& instructions) { instructions += "AEACAEE"; } void DrawTriangleUpTo9(string& instructions, int64_t size) { int64_t sizeX, sizeY; sizeX = size / 2; sizeY = size - sizeX; if ((sizeY) % 2 == 1) { sizeY++; sizeX--; } DrawParallelogram(instructions, sizeX, sizeY); DrawTriangle(instructions, sizeY); DrawLeftZig(instructions, sizeX); DrawTriangle(instructions, sizeX-1); } void DrawTriangle(string& instructions, int64_t size) { int threshold = 2; if (size == 0) { return; } if (size == 1) { DrawTriangle1(instructions); return; } if (size == 2) { DrawTriangle2(instructions); return; } if (size <= threshold) { DrawTriangleUpTo9(instructions, size); return; } int64_t r = size % threshold; if(r != 0) { DrawParallelogram(instructions, r, size - r); } int64_t x = size / threshold; for(int i = threshold-1; i >= 1; i--) { DrawParallelogram(instructions, x, i*x); } DrawRight(instructions, x); string trianglePattern; DrawLeftZig(trianglePattern, x); DrawTriangle(trianglePattern, x-1); instructions += SimpleRepeatPattern(trianglePattern, threshold); if(r != 0) { DrawLeftZig(instructions, r); DrawTriangle(instructions, r-1); } } void DrawParallelogram(string& instructions, int64_t sizeX, int64_t sizeY) { if(sizeX == 1) { instructions += RepeatPattern("AE", sizeY); instructions += "A"; } else { string pattern; DrawRightZigZag(pattern, sizeX); DrawLeftZig(pattern, sizeX); instructions += RepeatPattern(pattern, sizeY/2); if((sizeY % 2) == 1) { DrawRightZigZag(instructions, sizeX); } else { DrawRight(instructions, sizeX); } } DrawDownLeft(instructions, sizeY); } void DrawRightZigZag(string& instructions, int64_t size) { instructions += RepeatPattern("AEAC", size-1); instructions += "AEA"; } void DrawRight(string& instructions, int64_t size) { instructions += RepeatPattern("A", size); } void DrawDownLeft(string& instructions, int64_t size) { instructions += RepeatPattern("C", size); } void DrawLeftZig(string& instructions, int64_t size) { instructions += RepeatPattern("EC", size-1); instructions += "E"; } string SimpleRepeatPattern(string pattern, int times) { string result; if(times == 0) { return result; } if(times == 1) { return pattern; } result += ('0' + times); if(pattern.size() == 1) { result += pattern; } else { result += '[' + pattern + ']'; } return result; } string RepeatPattern(string pattern, int64_t times) { string result; vector<int> polynomial; if (times == 0) { return result; } while(times > 0) { polynomial.push_back(times % 9); times /= 9; } if (polynomial.size() == 1) { return SimpleRepeatPattern(pattern, polynomial.back()); } for(int i = 2; i < polynomial.size(); i++) { result += "9["; } if (polynomial.back() != 1) { result += "9[" + SimpleRepeatPattern(pattern, polynomial.back()) + "]"; } else { result += SimpleRepeatPattern(pattern, 9); } for(int i = polynomial.size() - 2; i >= 0; i--) { result += (i == polynomial.size() - 2) ? "" : "]"; result += SimpleRepeatPattern(pattern, polynomial[i]); } return result; } int main() { int64_t size; string instructions; scanf("%lld", &size); DrawTriangle(instructions, size); DrawDownLeft(instructions, size); printf("%s\n", instructions.c_str()); 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 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 | #include <cstdio> #include <cinttypes> #include <string> #include <vector> using namespace std; void DrawTriangle(string& instructions, int64_t size); void DrawParallelogram(string& instructions, int64_t sizeX, int64_t sizeY); void DrawRightZigZag(string& instructions, int64_t size); void DrawRight(string& instructions, int64_t size); void DrawDownLeft(string& instructions, int64_t size); void DrawLeftZig(string& instructions, int64_t size); string SimpleRepeatPattern(string pattern, int times); string RepeatPattern(string pattern, int64_t times); void DrawTriangle1(string& instructions) { instructions += "AE"; } void DrawTriangle2(string& instructions) { instructions += "AEACAEE"; } void DrawTriangleUpTo9(string& instructions, int64_t size) { int64_t sizeX, sizeY; sizeX = size / 2; sizeY = size - sizeX; if ((sizeY) % 2 == 1) { sizeY++; sizeX--; } DrawParallelogram(instructions, sizeX, sizeY); DrawTriangle(instructions, sizeY); DrawLeftZig(instructions, sizeX); DrawTriangle(instructions, sizeX-1); } void DrawTriangle(string& instructions, int64_t size) { int threshold = 2; if (size == 0) { return; } if (size == 1) { DrawTriangle1(instructions); return; } if (size == 2) { DrawTriangle2(instructions); return; } if (size <= threshold) { DrawTriangleUpTo9(instructions, size); return; } int64_t r = size % threshold; if(r != 0) { DrawParallelogram(instructions, r, size - r); } int64_t x = size / threshold; for(int i = threshold-1; i >= 1; i--) { DrawParallelogram(instructions, x, i*x); } DrawRight(instructions, x); string trianglePattern; DrawLeftZig(trianglePattern, x); DrawTriangle(trianglePattern, x-1); instructions += SimpleRepeatPattern(trianglePattern, threshold); if(r != 0) { DrawLeftZig(instructions, r); DrawTriangle(instructions, r-1); } } void DrawParallelogram(string& instructions, int64_t sizeX, int64_t sizeY) { if(sizeX == 1) { instructions += RepeatPattern("AE", sizeY); instructions += "A"; } else { string pattern; DrawRightZigZag(pattern, sizeX); DrawLeftZig(pattern, sizeX); instructions += RepeatPattern(pattern, sizeY/2); if((sizeY % 2) == 1) { DrawRightZigZag(instructions, sizeX); } else { DrawRight(instructions, sizeX); } } DrawDownLeft(instructions, sizeY); } void DrawRightZigZag(string& instructions, int64_t size) { instructions += RepeatPattern("AEAC", size-1); instructions += "AEA"; } void DrawRight(string& instructions, int64_t size) { instructions += RepeatPattern("A", size); } void DrawDownLeft(string& instructions, int64_t size) { instructions += RepeatPattern("C", size); } void DrawLeftZig(string& instructions, int64_t size) { instructions += RepeatPattern("EC", size-1); instructions += "E"; } string SimpleRepeatPattern(string pattern, int times) { string result; if(times == 0) { return result; } if(times == 1) { return pattern; } result += ('0' + times); if(pattern.size() == 1) { result += pattern; } else { result += '[' + pattern + ']'; } return result; } string RepeatPattern(string pattern, int64_t times) { string result; vector<int> polynomial; if (times == 0) { return result; } while(times > 0) { polynomial.push_back(times % 9); times /= 9; } if (polynomial.size() == 1) { return SimpleRepeatPattern(pattern, polynomial.back()); } for(int i = 2; i < polynomial.size(); i++) { result += "9["; } if (polynomial.back() != 1) { result += "9[" + SimpleRepeatPattern(pattern, polynomial.back()) + "]"; } else { result += SimpleRepeatPattern(pattern, 9); } for(int i = polynomial.size() - 2; i >= 0; i--) { result += (i == polynomial.size() - 2) ? "" : "]"; result += SimpleRepeatPattern(pattern, polynomial[i]); } return result; } int main() { int64_t size; string instructions; scanf("%lld", &size); DrawTriangle(instructions, size); DrawDownLeft(instructions, size); printf("%s\n", instructions.c_str()); return 0; } |