#include <iostream> #include <string> #include <algorithm> #include <vector> #include <cassert> using namespace std; typedef unsigned long long ull; string SillyAdd(string longerNumber, string shorterNumber) { if (longerNumber.size() < shorterNumber.size()) swap(longerNumber, shorterNumber); reverse(longerNumber.begin(), longerNumber.end()); reverse(shorterNumber.begin(), shorterNumber.end()); vector<int> resultantDigits; for (int i = 0; i < longerNumber.size(); ++i) { if (i >= shorterNumber.size()) resultantDigits.push_back(longerNumber[i] - '0'); else { resultantDigits.push_back(longerNumber[i] + shorterNumber[i] - 2 * '0'); } } reverse(resultantDigits.begin(), resultantDigits.end()); string result; for (int i = 0; i < resultantDigits.size(); ++i) result += to_string(resultantDigits[i]); return result; } class SimpleSolver { public: ull Solve(const ull number) { ull result = 0; string stringNumber = to_string(number); for (ull a = 0; a <= number; ++a) { string aString = to_string(a); for (ull b = 0; b <= number; ++b) { string bString = to_string(b); if (stringNumber == SillyAdd(aString, bString)) ++result; } } return result; } }; class AdvacedSolver { public: ull Solve(const ull number) { return Solve(to_string(number)); } private: ull Solve(const string& number) { if (number.size() == 0) return 0; if (number[0] == '0' && number.size() > 1) return Solve(Trim0(number)); if (number.size() == 1) return ComputeForNumberOfLength1(number); if (number.size() == 2) return ComputeForNumberOfLength2(number); const ull a = ComputeForNumberOfLength1(number.substr(0, 1)) * Solve(number.substr(1)); const ull b = ComputeForNumberOfLength2WithSumGreaterThen9(number.substr(0, 2)) * Solve(number.substr(2)); return a + b; } ull ComputeForNumberOfLength2(const string& stringNumber) { if (stringNumber[0] == '0') return ComputeForNumberOfLength1(stringNumber.substr(1)); ull number = (stringNumber[0] - '0') * 10 + stringNumber[1] - '0'; const ull result = (stringNumber[0] - '0' + 1) * (stringNumber[1] - '0' + 1); if (number > 19) return result; return result + 19 - number; } ull ComputeForNumberOfLength2WithSumGreaterThen9(const string& stringNumber) { ull number = (stringNumber[0] - '0') * 10 + stringNumber[1] - '0'; if (number >= 19) return 0; return 19 - number; } ull ComputeForNumberOfLength1(const string& number) { return number[0] - '0' + 1; } string Trim0(const string& input) { int pos = 0; while (pos < input.size() && input[pos] == '0') ++pos; return pos == input.size() ? "0" : input.substr(pos); } }; //#define DEBUG int main() { SimpleSolver simpleSolver; AdvacedSolver advancedSolver; #ifdef DEBUG assert(simpleSolver.Solve(112) == 50); for (int i = 0; i <= 9; ++i) assert(simpleSolver.Solve(i) == i + 1); assert(simpleSolver.Solve(12) == 13); assert(simpleSolver.Solve(11) == 12); assert(simpleSolver.Solve(21) == 6); assert(simpleSolver.Solve(100) == 11); assert(simpleSolver.Solve(1010) == 121); cout << simpleSolver.Solve(11) << endl; cout << simpleSolver.Solve(112) << endl; cout << simpleSolver.Solve(21) << endl; cout << "---\n"; for (int i = 0; i <= 9; ++i) assert(advancedSolver.Solve(i) == i + 1); assert(advancedSolver.Solve(12) == 13); assert(advancedSolver.Solve(11) == 12); assert(advancedSolver.Solve(21) == 6); assert(advancedSolver.Solve(100) == 11); assert(advancedSolver.Solve(1000) == 11); cout << advancedSolver.Solve(112) << endl; for (int i = 1000; i < 1200; ++i) { ull ss = simpleSolver.Solve(i); ull as = advancedSolver.Solve(i); if (ss != as) cout << "Uups! (i=" << i << ") " << ss << " != " << as << endl; } cout << "Done!" << endl; cout << advancedSolver.Solve(1822187) << endl; cout << advancedSolver.Solve(1111111111) << endl; cout << advancedSolver.Solve(11111111111111111) << endl; #endif ull number; cin >> number; cout << advancedSolver.Solve(number) << endl; #ifdef DEBUG system("pause"); #endif }
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 | #include <iostream> #include <string> #include <algorithm> #include <vector> #include <cassert> using namespace std; typedef unsigned long long ull; string SillyAdd(string longerNumber, string shorterNumber) { if (longerNumber.size() < shorterNumber.size()) swap(longerNumber, shorterNumber); reverse(longerNumber.begin(), longerNumber.end()); reverse(shorterNumber.begin(), shorterNumber.end()); vector<int> resultantDigits; for (int i = 0; i < longerNumber.size(); ++i) { if (i >= shorterNumber.size()) resultantDigits.push_back(longerNumber[i] - '0'); else { resultantDigits.push_back(longerNumber[i] + shorterNumber[i] - 2 * '0'); } } reverse(resultantDigits.begin(), resultantDigits.end()); string result; for (int i = 0; i < resultantDigits.size(); ++i) result += to_string(resultantDigits[i]); return result; } class SimpleSolver { public: ull Solve(const ull number) { ull result = 0; string stringNumber = to_string(number); for (ull a = 0; a <= number; ++a) { string aString = to_string(a); for (ull b = 0; b <= number; ++b) { string bString = to_string(b); if (stringNumber == SillyAdd(aString, bString)) ++result; } } return result; } }; class AdvacedSolver { public: ull Solve(const ull number) { return Solve(to_string(number)); } private: ull Solve(const string& number) { if (number.size() == 0) return 0; if (number[0] == '0' && number.size() > 1) return Solve(Trim0(number)); if (number.size() == 1) return ComputeForNumberOfLength1(number); if (number.size() == 2) return ComputeForNumberOfLength2(number); const ull a = ComputeForNumberOfLength1(number.substr(0, 1)) * Solve(number.substr(1)); const ull b = ComputeForNumberOfLength2WithSumGreaterThen9(number.substr(0, 2)) * Solve(number.substr(2)); return a + b; } ull ComputeForNumberOfLength2(const string& stringNumber) { if (stringNumber[0] == '0') return ComputeForNumberOfLength1(stringNumber.substr(1)); ull number = (stringNumber[0] - '0') * 10 + stringNumber[1] - '0'; const ull result = (stringNumber[0] - '0' + 1) * (stringNumber[1] - '0' + 1); if (number > 19) return result; return result + 19 - number; } ull ComputeForNumberOfLength2WithSumGreaterThen9(const string& stringNumber) { ull number = (stringNumber[0] - '0') * 10 + stringNumber[1] - '0'; if (number >= 19) return 0; return 19 - number; } ull ComputeForNumberOfLength1(const string& number) { return number[0] - '0' + 1; } string Trim0(const string& input) { int pos = 0; while (pos < input.size() && input[pos] == '0') ++pos; return pos == input.size() ? "0" : input.substr(pos); } }; //#define DEBUG int main() { SimpleSolver simpleSolver; AdvacedSolver advancedSolver; #ifdef DEBUG assert(simpleSolver.Solve(112) == 50); for (int i = 0; i <= 9; ++i) assert(simpleSolver.Solve(i) == i + 1); assert(simpleSolver.Solve(12) == 13); assert(simpleSolver.Solve(11) == 12); assert(simpleSolver.Solve(21) == 6); assert(simpleSolver.Solve(100) == 11); assert(simpleSolver.Solve(1010) == 121); cout << simpleSolver.Solve(11) << endl; cout << simpleSolver.Solve(112) << endl; cout << simpleSolver.Solve(21) << endl; cout << "---\n"; for (int i = 0; i <= 9; ++i) assert(advancedSolver.Solve(i) == i + 1); assert(advancedSolver.Solve(12) == 13); assert(advancedSolver.Solve(11) == 12); assert(advancedSolver.Solve(21) == 6); assert(advancedSolver.Solve(100) == 11); assert(advancedSolver.Solve(1000) == 11); cout << advancedSolver.Solve(112) << endl; for (int i = 1000; i < 1200; ++i) { ull ss = simpleSolver.Solve(i); ull as = advancedSolver.Solve(i); if (ss != as) cout << "Uups! (i=" << i << ") " << ss << " != " << as << endl; } cout << "Done!" << endl; cout << advancedSolver.Solve(1822187) << endl; cout << advancedSolver.Solve(1111111111) << endl; cout << advancedSolver.Solve(11111111111111111) << endl; #endif ull number; cin >> number; cout << advancedSolver.Solve(number) << endl; #ifdef DEBUG system("pause"); #endif } |