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 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301
|
#include <bits/stdc++.h> using namespace std; #define _SEL3(_1, _2, _3, ...) _1, _2, _3 #define SEL3(...) _SEL3(__VA_ARGS__) #define _CPROD1_4_HELPER(a, b, c, d, ...) \ a, __VA_ARGS__, b, __VA_ARGS__, c, __VA_ARGS__, d, __VA_ARGS__ #define _CPROD2_4_HELPER(a, b, c, d, ...) \ _CPROD1_4_HELPER(a, b, c, d, a, __VA_ARGS__), \ _CPROD1_4_HELPER(a, b, c, d, b, __VA_ARGS__), \ _CPROD1_4_HELPER(a, b, c, d, c, __VA_ARGS__), \ _CPROD1_4_HELPER(a, b, c, d, d, __VA_ARGS__) #define CPROD3_4(a, b, c, d) \ _CPROD2_4_HELPER(a, b, c, d, a), _CPROD2_4_HELPER(a, b, c, d, b), \ _CPROD2_4_HELPER(a, b, c, d, c), _CPROD2_4_HELPER(a, b, c, d, d) #define RMAB(a, b, ...) _RM_##b(RM##a, __VA_ARGS__) #define OPANDS CPROD3_4(+, -, *, /) #define OPS0 SEL3(OPANDS) #define OPS1 SEL3(RMAB(3, 1, OPANDS)) #define OPS2 SEL3(RMAB(3, 2, OPANDS)) #define OPS3 SEL3(RMAB(3, 3, OPANDS)) #define OPS4 SEL3(RMAB(3, 4, OPANDS)) #define OPS5 SEL3(RMAB(3, 5, OPANDS)) #define OPS6 SEL3(RMAB(3, 6, OPANDS)) #define OPS7 SEL3(RMAB(3, 7, OPANDS)) #define OPS8 SEL3(RMAB(3, 8, OPANDS)) #define OPS9 SEL3(RMAB(3, 9, OPANDS)) #define OPS10 SEL3(RMAB(3, 10, OPANDS)) #define OPS11 SEL3(RMAB(3, 11, OPANDS)) #define OPS12 SEL3(RMAB(3, 12, OPANDS)) #define OPS13 SEL3(RMAB(3, 13, OPANDS)) #define OPS14 SEL3(RMAB(3, 14, OPANDS)) #define OPS15 SEL3(RMAB(3, 15, OPANDS)) #define OPS16 SEL3(RMAB(3, 16, OPANDS)) #define OPS17 SEL3(RMAB(3, 17, OPANDS)) #define OPS18 SEL3(RMAB(3, 18, OPANDS)) #define OPS19 SEL3(RMAB(3, 19, OPANDS)) #define OPS20 SEL3(RMAB(3, 20, OPANDS)) #define OPS21 SEL3(RMAB(3, 21, OPANDS)) #define OPS22 SEL3(RMAB(3, 22, OPANDS)) #define OPS23 SEL3(RMAB(3, 23, OPANDS)) #define OPS24 SEL3(RMAB(3, 24, OPANDS)) #define OPS25 SEL3(RMAB(3, 25, OPANDS)) #define OPS26 SEL3(RMAB(3, 26, OPANDS)) #define OPS27 SEL3(RMAB(3, 27, OPANDS)) #define OPS28 SEL3(RMAB(3, 28, OPANDS)) #define OPS29 SEL3(RMAB(3, 29, OPANDS)) #define OPS30 SEL3(RMAB(3, 30, OPANDS)) #define OPS31 SEL3(RMAB(3, 31, OPANDS)) #define OPS32 SEL3(RMAB(3, 32, OPANDS)) #define OPS33 SEL3(RMAB(3, 33, OPANDS)) #define OPS34 SEL3(RMAB(3, 34, OPANDS)) #define OPS35 SEL3(RMAB(3, 35, OPANDS)) #define OPS36 SEL3(RMAB(3, 36, OPANDS)) #define OPS37 SEL3(RMAB(3, 37, OPANDS)) #define OPS38 SEL3(RMAB(3, 38, OPANDS)) #define OPS39 SEL3(RMAB(3, 39, OPANDS)) #define OPS40 SEL3(RMAB(3, 40, OPANDS)) #define OPS41 SEL3(RMAB(3, 41, OPANDS)) #define OPS42 SEL3(RMAB(3, 42, OPANDS)) #define OPS43 SEL3(RMAB(3, 43, OPANDS)) #define OPS44 SEL3(RMAB(3, 44, OPANDS)) #define OPS45 SEL3(RMAB(3, 45, OPANDS)) #define OPS46 SEL3(RMAB(3, 46, OPANDS)) #define OPS47 SEL3(RMAB(3, 47, OPANDS)) #define OPS48 SEL3(RMAB(3, 48, OPANDS)) #define OPS49 SEL3(RMAB(3, 49, OPANDS)) #define OPS50 SEL3(RMAB(3, 50, OPANDS)) #define OPS51 SEL3(RMAB(3, 51, OPANDS)) #define OPS52 SEL3(RMAB(3, 52, OPANDS)) #define OPS53 SEL3(RMAB(3, 53, OPANDS)) #define OPS54 SEL3(RMAB(3, 54, OPANDS)) #define OPS55 SEL3(RMAB(3, 55, OPANDS)) #define OPS56 SEL3(RMAB(3, 56, OPANDS)) #define OPS57 SEL3(RMAB(3, 57, OPANDS)) #define OPS58 SEL3(RMAB(3, 58, OPANDS)) #define OPS59 SEL3(RMAB(3, 59, OPANDS)) #define OPS60 SEL3(RMAB(3, 60, OPANDS)) #define OPS61 SEL3(RMAB(3, 61, OPANDS)) #define OPS62 SEL3(RMAB(3, 62, OPANDS)) #define OPS63 RMAB(3, 63, OPANDS) #define ___CALC0(a, b, c, d, opab, opbc, opcd) \ Node(0, \ a, \ b, \ c, \ d, \ #opab, \ #opbc, \ #opcd, \ ((double(a) opab double(b))opbc double(c))opcd double(d)) #define ___CALC1(a, b, c, d, opab, opbc, opcd) \ Node(1, \ a, \ b, \ c, \ d, \ #opab, \ #opbc, \ #opcd, \ (double(a) opab(double(b) opbc double(c)))opcd double(d)) #define ___CALC2(a, b, c, d, opab, opbc, opcd) \ Node(2, \ a, \ b, \ c, \ d, \ #opab, \ #opbc, \ #opcd, \ double(a) opab((double(b) opbc double(c))opcd double(d))) #define ___CALC3(a, b, c, d, opab, opbc, opcd) \ Node(3, \ a, \ b, \ c, \ d, \ #opab, \ #opbc, \ #opcd, \ double(a) opab(double(b) opbc(double(c) opcd double(d)))) #define ___CALC4(a, b, c, d, opab, opbc, opcd) \ Node(4, \ a, \ b, \ c, \ d, \ #opab, \ #opbc, \ #opcd, \ ((double(a) opab double(b))opbc(double(c) opcd double(d)))) #define ___CALC(a, b, c, d, opab, opbc, opcd) \ ___CALC0(a, b, c, d, opab, opbc, opcd), \ ___CALC1(a, b, c, d, opab, opbc, opcd), \ ___CALC2(a, b, c, d, opab, opbc, opcd), \ ___CALC3(a, b, c, d, opab, opbc, opcd), \ ___CALC4(a, b, c, d, opab, opbc, opcd) #define __CALC(...) ___CALC(__VA_ARGS__) #define _CALC(a, b, c, d) \ __CALC(a, b, c, d, OPS0), __CALC(a, b, c, d, OPS1), \ __CALC(a, b, c, d, OPS2), __CALC(a, b, c, d, OPS3), \ __CALC(a, b, c, d, OPS4), __CALC(a, b, c, d, OPS5), \ __CALC(a, b, c, d, OPS6), __CALC(a, b, c, d, OPS7), \ __CALC(a, b, c, d, OPS8), __CALC(a, b, c, d, OPS9), \ __CALC(a, b, c, d, OPS10), __CALC(a, b, c, d, OPS11), \ __CALC(a, b, c, d, OPS12), __CALC(a, b, c, d, OPS13), \ __CALC(a, b, c, d, OPS14), __CALC(a, b, c, d, OPS15), \ __CALC(a, b, c, d, OPS16), __CALC(a, b, c, d, OPS17), \ __CALC(a, b, c, d, OPS18), __CALC(a, b, c, d, OPS19), \ __CALC(a, b, c, d, OPS20), __CALC(a, b, c, d, OPS21), \ __CALC(a, b, c, d, OPS22), __CALC(a, b, c, d, OPS23), \ __CALC(a, b, c, d, OPS24), __CALC(a, b, c, d, OPS25), \ __CALC(a, b, c, d, OPS26), __CALC(a, b, c, d, OPS27), \ __CALC(a, b, c, d, OPS28), __CALC(a, b, c, d, OPS29), \ __CALC(a, b, c, d, OPS30), __CALC(a, b, c, d, OPS31), \ __CALC(a, b, c, d, OPS32), __CALC(a, b, c, d, OPS33), \ __CALC(a, b, c, d, OPS34), __CALC(a, b, c, d, OPS35), \ __CALC(a, b, c, d, OPS36), __CALC(a, b, c, d, OPS37), \ __CALC(a, b, c, d, OPS38), __CALC(a, b, c, d, OPS39), \ __CALC(a, b, c, d, OPS40), __CALC(a, b, c, d, OPS41), \ __CALC(a, b, c, d, OPS42), __CALC(a, b, c, d, OPS43), \ __CALC(a, b, c, d, OPS44), __CALC(a, b, c, d, OPS45), \ __CALC(a, b, c, d, OPS46), __CALC(a, b, c, d, OPS47), \ __CALC(a, b, c, d, OPS48), __CALC(a, b, c, d, OPS49), \ __CALC(a, b, c, d, OPS50), __CALC(a, b, c, d, OPS51), \ __CALC(a, b, c, d, OPS52), __CALC(a, b, c, d, OPS53), \ __CALC(a, b, c, d, OPS54), __CALC(a, b, c, d, OPS55), \ __CALC(a, b, c, d, OPS56), __CALC(a, b, c, d, OPS57), \ __CALC(a, b, c, d, OPS58), __CALC(a, b, c, d, OPS59), \ __CALC(a, b, c, d, OPS60), __CALC(a, b, c, d, OPS61), \ __CALC(a, b, c, d, OPS62), __CALC(a, b, c, d, OPS63), #define CALC(...) _CALC(__VA_ARGS__) int dict[256]; dict['+'] = 0; dict['-'] = 1; dict['*'] = 2; dict['/'] = 3; return 0; } (); struct Node { u64 hash; double data; Node(const int &type, i64 a, i64 b, i64 c, i64 d, const char *opab, const char *opbc, const char *opcd, const double &_data) : data(_data) { switch (type) { case 0: break; case 1: if (!valid(dict[*opab], dict[*opbc])) { data = 0; break; } break; case 2: if (!valid(dict[*opab], dict[*opcd])) { data = 0; break; } break; case 3: if (!valid(dict[*opab], dict[*opbc]) || !valid(dict[*opbc], dict[*opcd])) { data = 0; break; } break; case 4: if (!valid(dict[*opbc], dict[*opcd])) { data = 0; break; } if (!(dict[*opbc] & 1)) { } else { } break; } hash = 0; (hash += a) <<= 7; (hash += b) <<= 7; (hash += c) <<= 7; (hash += d) <<= 2; (hash += dict[*opab]) <<= 2; (hash += dict[*opbc]) <<= 2; (hash += dict[*opcd]) <<= 3; hash += type; #undef minmax } constexpr bool valid(const int &a, const int &b) const { if (a == 0) return b > 1; if (a == 2) return b < 2; return true; } bool operator<(const Node &rhs) const { return hash < rhs.hash; } }; void decode(u64 hash) { #define _decode(val, n) \ val = hash & ((1 << n) - 1); \ hash >>= n static const char _[] = "+-*/"; int type, a, b, c, d, opab, opbc, opcd; _decode(type, 3); _decode(opcd, 2); _decode(opbc, 2); _decode(opab, 2); _decode(d, 7); _decode(c, 7); _decode(b, 7); _decode(a, 7); switch (type) { case 0: cerr << "(((" << a << " " << _[opab] << " " << b << ") " << _[opbc] << " " << c << ") " << _[opcd] << " " << d << ")" << endl; break; case 1: cerr << "((" << a << " " << _[opab] << " (" << b << " " << _[opbc] << " " << c << ")) " << _[opcd] << " " << d << ")" << endl; break; case 2: cerr << "(" << a << " " << _[opab] << " ((" << b << " " << _[opbc] << " " << c << ") " << _[opcd] << " " << d << "))" << endl; break; case 3: cerr << "(" << a << " " << _[opab] << " (" << b << " " << _[opbc] << " (" << c << " " << _[opcd] << " " << d << ")))" << endl; break; case 4: cerr << "((" << a << " " << _[opab] << " " << b << ") " << _[opbc] << " (" << c << " " << _[opcd] << " " << d << "))" << endl; break; } #undef _decode } int main() { i64 a, b, c, d; cin >> a >> b >> c >> d; set<Node> s; vector<double> v{a, b, c, d}; sort(v.begin(), v.end()); do { s.merge(decltype(s){CALC(v[0], v[1], v[2], v[3])}); } while (next_permutation(v.begin(), v.end())); int cnt = 0; for (const auto &[hash, data] : s) if (abs(data - 24) <= 1e-7) { ++cnt; } cout << cnt; return 0; }
|