#include<iostream>
#include<vector>
int main() {
std::ios::sync_with_stdio(0);
std::cin.tie(0);
std::string a, b, c;
std::cin >> a >> b >> c;
const int n = (int)a.size();
std::vector<std::vector<int>> dp(n + 1, {0, 0});
dp[0][0] = 1;
auto get = [](std::string_view s, int pos) {
return s[pos] - '0';
};
long long res = 0;
for (int i = 0; i < n; ++i) {
if (get(a, i) + get(b, i) == get(c, i)) {
dp[i + 1][0] = dp[i][0];
}
if (get(a, i) + get(b, i) + 1 == get(c, i)) {
dp[i + 1][1] = dp[i][0];
}
if (get(a, i) + get(b, i) - 10 == get(c, i)) {
dp[i + 1][0] = dp[i][1];
}
if (get(a, i) + get(b, i) + 1 - 10 == get(c, i)) {
dp[i + 1][1] = dp[i][1];
}
res += dp[i + 1][0];
dp[i + 1][0]++;
}
std::cout << res << '\n';
}
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 | #include<iostream> #include<vector> int main() { std::ios::sync_with_stdio(0); std::cin.tie(0); std::string a, b, c; std::cin >> a >> b >> c; const int n = (int)a.size(); std::vector<std::vector<int>> dp(n + 1, {0, 0}); dp[0][0] = 1; auto get = [](std::string_view s, int pos) { return s[pos] - '0'; }; long long res = 0; for (int i = 0; i < n; ++i) { if (get(a, i) + get(b, i) == get(c, i)) { dp[i + 1][0] = dp[i][0]; } if (get(a, i) + get(b, i) + 1 == get(c, i)) { dp[i + 1][1] = dp[i][0]; } if (get(a, i) + get(b, i) - 10 == get(c, i)) { dp[i + 1][0] = dp[i][1]; } if (get(a, i) + get(b, i) + 1 - 10 == get(c, i)) { dp[i + 1][1] = dp[i][1]; } res += dp[i + 1][0]; dp[i + 1][0]++; } std::cout << res << '\n'; } |
English