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
#include <iostream>
#include <string>

using namespace std;

int main() {
    cin.tie(nullptr);
    ios::sync_with_stdio(false);

    string A, B, C;
    if (!(cin >> A >> B >> C)) return 0;

    int n = A.length();

    long long dp0 = 0;
    long long dp1 = 0;
    long long result = 0;

    for (int k = n - 1; k >= 0; k--) {
        int sum_digits = (A[k] - '0') + (B[k] - '0');
        int target = C[k] - '0';

        long long dp0_in = dp0 + 1;
        long long dp1_in = dp1;

        long long next_dp0 = 0;
        long long next_dp1 = 0;

        if (sum_digits == target) {
            next_dp0 = dp0_in;
            next_dp1 = 0;
        } 
        else if (sum_digits == target + 10) {
            next_dp0 = 0;
            next_dp1 = dp0_in;
        } 
        else if (sum_digits == target - 1) {
            next_dp0 = dp1_in;
            next_dp1 = 0;
        } 
        else if (sum_digits == target + 9) {
            next_dp0 = 0;
            next_dp1 = dp1_in;
        } 
        else {
            next_dp0 = 0;
            next_dp1 = 0;
        }

        dp0 = next_dp0;
        dp1 = next_dp1;

        result += dp0;
    }

    cout << result << "\n";

    return 0;
}