#include <cstdio>
#include <cstring>
using namespace std;
char a1[1000100];
char a2[1000100];
char s[1000100];
int n;
long long result;
int chain;
bool surplus;
void consume_and_reset() {
result += ((long long)chain + 1LL) * (long long)chain / 2LL;
chain = 0;
}
int main() {
scanf("%s", a1);
scanf("%s", a2);
scanf("%s", s);
n = strlen(s);
result = 0LL;
chain = 0;
surplus = false;
for (int i = n - 1; i >= 0; i--) {
int temp_s = s[i] - '0';
if (surplus) {
int temp_a1_a2 = a1[i] - '0' + a2[i] - '0' + 1;
if (temp_a1_a2 >= 10) {
if (temp_a1_a2 % 10 == temp_s) { // continue surplus, don't increase chain
// continue; // continue not needed in fact?
} else {
// meh, consume chain (if there's anything) and start from the beginning
consume_and_reset();
surplus = false;
temp_a1_a2 = a1[i] - '0' + a2[i] - '0';
if (temp_a1_a2 >= 10) {
if (temp_a1_a2 % 10 == temp_s) { // start surplus "mode" from the beginning
surplus = true;
}
} else {
if (temp_a1_a2 == temp_s) { // ok, increase chain
chain++;
}
}
}
} else {
surplus = false;
if (temp_a1_a2 == temp_s) { // treate whole block with surplus(es) as one
chain++;
} else { // meh, consume chain (if there's anything) and start from the beginning
consume_and_reset();
temp_a1_a2 = a1[i] - '0' + a2[i] - '0';
if (temp_a1_a2 == temp_s) { // ok, increase chain
chain++;
}
}
}
} else {
int temp_a1_a2 = a1[i] - '0' + a2[i] - '0';
if (temp_a1_a2 >= 10) {
if (temp_a1_a2 % 10 == temp_s) { // start surplus "mode"
surplus = true;
} else { // meh, consume chain (if there's anything) and start from the beginning
consume_and_reset();
}
} else {
if (temp_a1_a2 == temp_s) { // ok, increase chain
chain++;
} else { // meh, consume chain (if there's anything) and start from the beginning
consume_and_reset();
}
}
}
}
consume_and_reset(); // one more at the end
printf("%lld\n", result);
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 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 | #include <cstdio> #include <cstring> using namespace std; char a1[1000100]; char a2[1000100]; char s[1000100]; int n; long long result; int chain; bool surplus; void consume_and_reset() { result += ((long long)chain + 1LL) * (long long)chain / 2LL; chain = 0; } int main() { scanf("%s", a1); scanf("%s", a2); scanf("%s", s); n = strlen(s); result = 0LL; chain = 0; surplus = false; for (int i = n - 1; i >= 0; i--) { int temp_s = s[i] - '0'; if (surplus) { int temp_a1_a2 = a1[i] - '0' + a2[i] - '0' + 1; if (temp_a1_a2 >= 10) { if (temp_a1_a2 % 10 == temp_s) { // continue surplus, don't increase chain // continue; // continue not needed in fact? } else { // meh, consume chain (if there's anything) and start from the beginning consume_and_reset(); surplus = false; temp_a1_a2 = a1[i] - '0' + a2[i] - '0'; if (temp_a1_a2 >= 10) { if (temp_a1_a2 % 10 == temp_s) { // start surplus "mode" from the beginning surplus = true; } } else { if (temp_a1_a2 == temp_s) { // ok, increase chain chain++; } } } } else { surplus = false; if (temp_a1_a2 == temp_s) { // treate whole block with surplus(es) as one chain++; } else { // meh, consume chain (if there's anything) and start from the beginning consume_and_reset(); temp_a1_a2 = a1[i] - '0' + a2[i] - '0'; if (temp_a1_a2 == temp_s) { // ok, increase chain chain++; } } } } else { int temp_a1_a2 = a1[i] - '0' + a2[i] - '0'; if (temp_a1_a2 >= 10) { if (temp_a1_a2 % 10 == temp_s) { // start surplus "mode" surplus = true; } else { // meh, consume chain (if there's anything) and start from the beginning consume_and_reset(); } } else { if (temp_a1_a2 == temp_s) { // ok, increase chain chain++; } else { // meh, consume chain (if there's anything) and start from the beginning consume_and_reset(); } } } } consume_and_reset(); // one more at the end printf("%lld\n", result); return 0; } |
English