#include <iostream>
#include <string>
#include <vector>
int main() {
std::string s1, s2, s3;
std::cin >> s1 >> s2 >> s3;
int n = s1.size();
long long result = 0;
long long mod0 = 0;
long long mod1 = 0;
for (int i = n - 1; i >= 0; i--) {
int a = s1[i] - '0';
int b = s2[i] - '0';
int c = s3[i] - '0';
long long next_mod0 = 0;
long long next_mod1 = 0;
if (a + b == c) {
next_mod0 = mod0 + 1;
next_mod1 = 0;
}
else if (a + b == c + 10) {
next_mod0 = 0;
next_mod1 = mod0 + 1;
}
else if (a + b + 1 == c) {
next_mod0 = mod1;
next_mod1 = 0;
}
else if (a + b == c + 9) {
next_mod0 = 0;
next_mod1 = mod1;
}
else {
next_mod0 = 0;
next_mod1 = 0;
}
mod0 = next_mod0;
mod1 = next_mod1;
result += mod0;
}
std::cout << result;
}
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 | #include <iostream> #include <string> #include <vector> int main() { std::string s1, s2, s3; std::cin >> s1 >> s2 >> s3; int n = s1.size(); long long result = 0; long long mod0 = 0; long long mod1 = 0; for (int i = n - 1; i >= 0; i--) { int a = s1[i] - '0'; int b = s2[i] - '0'; int c = s3[i] - '0'; long long next_mod0 = 0; long long next_mod1 = 0; if (a + b == c) { next_mod0 = mod0 + 1; next_mod1 = 0; } else if (a + b == c + 10) { next_mod0 = 0; next_mod1 = mod0 + 1; } else if (a + b + 1 == c) { next_mod0 = mod1; next_mod1 = 0; } else if (a + b == c + 9) { next_mod0 = 0; next_mod1 = mod1; } else { next_mod0 = 0; next_mod1 = 0; } mod0 = next_mod0; mod1 = next_mod1; result += mod0; } std::cout << result; } |
English