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
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <cassert>

char input1[1000 * 1000 + 42];
char input2[1000 * 1000 + 42];
char result[1000 * 1000 + 42];

int main() {
    scanf("%s %s %s", input1, input2, result);
    long long int n = strlen(result);
    assert(strlen(input1) == size_t(n));
    assert(strlen(input2) == size_t(n));

    long long int total = 0;
    long long int good_end_positions_without_carry = 0;
    long long int good_end_positions_with_carry = 0;

    for (long long int i = n - 1; i >= 0; i--) {
        const bool digit_matches_without_carry = (result[i] - '0') == ((input1[i] - '0') + (input2[i] - '0')) % 10;
        const bool digit_matches_with_carry = (result[i] - '0') == (1 + (input1[i] - '0') + (input2[i] - '0')) % 10;

        const bool has_carry_assuming_no_carry = (input1[i] - '0') + (input2[i] - '0') >= 10;
        const bool has_carry_assuming_carry = 1 + (input1[i] - '0') + (input2[i] - '0') >= 10;

        // Initialize the variables according to the situations
        long long int new_good_end_positions_without_carry = 0;
        long long int new_good_end_positions_with_carry = 0;

        if (digit_matches_without_carry && !has_carry_assuming_no_carry) {
            new_good_end_positions_without_carry += good_end_positions_without_carry + 1;
        }
        if (digit_matches_without_carry && has_carry_assuming_no_carry) {
            new_good_end_positions_with_carry += good_end_positions_without_carry + 1;
        }
        if (digit_matches_with_carry && !has_carry_assuming_carry) {
            new_good_end_positions_without_carry += good_end_positions_with_carry;
        }
        if (digit_matches_with_carry && has_carry_assuming_carry) {
            new_good_end_positions_with_carry += good_end_positions_with_carry;
        }

        good_end_positions_without_carry = new_good_end_positions_without_carry;
        good_end_positions_with_carry = new_good_end_positions_with_carry;

        total += good_end_positions_without_carry;
    }

    printf("%lld\n", total);

    return 0;
}