diff --git a/2024/7/main-1.cpp b/2024/7/main-1.cpp new file mode 100644 index 0000000..815d394 --- /dev/null +++ b/2024/7/main-1.cpp @@ -0,0 +1,46 @@ +#include +using namespace std; + +typedef unsigned long long ull; + +inline ull testn(vector &ns, ull b) { + ull v = ns[0]; + for (int i = 1; i < ns.size(); i++) { + if (b & 1) { + v += ns[i]; + } else { + v *= ns[i]; + } + b >>= 1; + } + return v; +} + +int main() { + ios::sync_with_stdio(0); + cin.tie(0); + + ull sum = 0; + string line; + while (getline(cin, line) && !line.empty()) { + istringstream l(line); + ull n; + int a; + vector ns; + l >> n; + l.ignore(); + while (l >> a) { + ns.push_back(a); + } + + ull btot = 1 << (ns.size() - 1); + for (ull b = 0; b <= btot; b++) { + if (testn(ns, b) == n) { + sum += n; + break; + } + } + } + + cout << sum << '\n'; +} diff --git a/2024/7/main-2.cpp b/2024/7/main-2.cpp new file mode 100644 index 0000000..aff3d9d --- /dev/null +++ b/2024/7/main-2.cpp @@ -0,0 +1,52 @@ +#include +using namespace std; + +typedef unsigned long long ull; + +inline ull testn(vector &ns, ull b) { + ull v = ns[0]; + for (int i = 1; i < ns.size(); i++) { + switch (b % 3) { + case 0: + v += ns[i]; + break; + case 1: + v *= ns[i]; + break; + case 2: + v *= pow(10, (int)log10(ns[i]) + 1); + v += ns[i]; + } + b /= 3; + } + return v; +} + +int main() { + ios::sync_with_stdio(0); + cin.tie(0); + + ull sum = 0; + string line; + while (getline(cin, line) && !line.empty()) { + istringstream l(line); + ull n; + int a; + vector ns; + l >> n; + l.ignore(); + while (l >> a) { + ns.push_back(a); + } + + ull btot = pow(3, (ns.size() - 1)); + for (ull b = 0; b <= btot; b++) { + if (testn(ns, b) == n) { + sum += n; + break; + } + } + } + + cout << sum << '\n'; +}