#include <iostream> #include <string> #include <vector> std::vector<unsigned long long> generate_array() { std::vector<unsigned long long> result; for (int n = 0; n < 19; n++) { int c = 0; for (int i = 0; i < 10; i++) { if ((0 <= n - i) && (n - i) < 10) { c++; } } result.push_back(static_cast<unsigned long long>(c)); } return result; } unsigned long long func(const std::vector<unsigned long long> &val, const std::string &s) { if (s.empty()) { return 1; } if (s.size() == 1) { return val[static_cast<size_t>(atoi(s.c_str()))]; } unsigned long long result = 0; if (s.size() >= 2 && s[0] == '1' && s[1] < '9') { result = val[static_cast<size_t>(atoi(s.substr(0, 2).c_str()))] * func(val, s.substr(2)); } result += val[static_cast<size_t>(atoi(s.substr(0, 1).c_str()))] * func(val, s.substr(1)); return result; } int main() { const auto VAL = generate_array(); std::string s; std::cin >> s; std::cout << func(VAL, s) << std::endl; 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 | #include <iostream> #include <string> #include <vector> std::vector<unsigned long long> generate_array() { std::vector<unsigned long long> result; for (int n = 0; n < 19; n++) { int c = 0; for (int i = 0; i < 10; i++) { if ((0 <= n - i) && (n - i) < 10) { c++; } } result.push_back(static_cast<unsigned long long>(c)); } return result; } unsigned long long func(const std::vector<unsigned long long> &val, const std::string &s) { if (s.empty()) { return 1; } if (s.size() == 1) { return val[static_cast<size_t>(atoi(s.c_str()))]; } unsigned long long result = 0; if (s.size() >= 2 && s[0] == '1' && s[1] < '9') { result = val[static_cast<size_t>(atoi(s.substr(0, 2).c_str()))] * func(val, s.substr(2)); } result += val[static_cast<size_t>(atoi(s.substr(0, 1).c_str()))] * func(val, s.substr(1)); return result; } int main() { const auto VAL = generate_array(); std::string s; std::cin >> s; std::cout << func(VAL, s) << std::endl; return 0; } |