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 <bits/stdc++.h>
using namespace std;
using i64 = int64_t;
using u64 = uint64_t;

int main() {
    ios_base::sync_with_stdio(false);
    cin.tie(nullptr);
 
    string a, b, res;
    cin >> a >> b >> res;

    int carryOn = -1;
    i64 count = 0;
    i64 ans = 0;

    for (int i = a.size() - 1; i >= 0; i--) {
        int currA = a[i] - '0';
        int currB = b[i] - '0';
        int currRes = res[i] - '0';
        int sum = currA + currB;

        int in = -1;
        int out = -1;

        if (sum % 10 == currRes) {
            in = 0;
            out = sum / 10;
        } else if ((sum + 1) % 10 == currRes) {
            in = 1;
            out = (sum + 1) / 10;
        }

        if (in == -1 || (carryOn != -1 && in != carryOn)) {
            count = 0;
        }

        if (in != -1) {
            if (in == 0) count++;
            if (out == 0) ans += count;

            carryOn = out;
        } else {
            carryOn = -1;
        }

    }

    cout << ans << "\n";
    
}