#include <iostream> void calc(int i, unsigned long long mem[], const int ones[], const int twos[]) { if (i > 0) { if (mem[i - 1] == 0) { calc(i - 1, mem, ones, twos); } mem[i] = (ones[i] + 1) * mem[i - 1]; if (twos[i - 1] < 19 && twos[i - 1] > 9) { if (i > 1) { mem[i] += (19 - twos[i - 1]) * mem[i - 2]; } else if (i == 1) { mem[i] += 19 - twos[i - 1]; } } } } int main() { unsigned long long n; std::cin >> n; unsigned long long copy = n; int size = 0; while (copy != 0) { ++size; copy /= 10; } unsigned long long mem[size]; int ones[size], twos[size - 1], i = 0; while (n != 0) { ones[i] = static_cast<int>(n % 10); if (n > 9) { twos[i] = static_cast<int>(n % 100); } mem[i] = 0; n /= 10; ++i; } mem[0] = ones[0] + 1; calc(size - 1, mem, ones, twos); std::cout << mem[size - 1]; 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 | #include <iostream> void calc(int i, unsigned long long mem[], const int ones[], const int twos[]) { if (i > 0) { if (mem[i - 1] == 0) { calc(i - 1, mem, ones, twos); } mem[i] = (ones[i] + 1) * mem[i - 1]; if (twos[i - 1] < 19 && twos[i - 1] > 9) { if (i > 1) { mem[i] += (19 - twos[i - 1]) * mem[i - 2]; } else if (i == 1) { mem[i] += 19 - twos[i - 1]; } } } } int main() { unsigned long long n; std::cin >> n; unsigned long long copy = n; int size = 0; while (copy != 0) { ++size; copy /= 10; } unsigned long long mem[size]; int ones[size], twos[size - 1], i = 0; while (n != 0) { ones[i] = static_cast<int>(n % 10); if (n > 9) { twos[i] = static_cast<int>(n % 100); } mem[i] = 0; n /= 10; ++i; } mem[0] = ones[0] + 1; calc(size - 1, mem, ones, twos); std::cout << mem[size - 1]; return 0; } |