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
#include <bits/stdc++.h>
using namespace std;

string a, b, c;
vector<int> pos;
bool carry;
bool carry_last;
int last;
long long w;

bool match(int i) {
    return (a[i] - '0' + b[i] - '0') < 10 && a[i] - '0' + b[i] - '0' == c[i] - '0';
}

bool plus_1_match(int i) {
    return (a[i] - '0' + b[i] - '0' + 1) < 10 && a[i] - '0' + b[i] - '0' + 1 == c[i] - '0';
}

bool overflow_match(int i) {
    return (a[i] - '0' + b[i] - '0') > 9 && (a[i] - '0' + b[i] - '0') % 10 == c[i] - '0';
}

bool overflow_plus_1_match(int i) {
    return (a[i] - '0' + b[i] - '0' + 1) > 9 && (a[i] - '0' + b[i] - '0' + 1) % 10 == c[i] - '0';
}

int main() {
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    cout.tie(NULL);

    cin >> a >> b >> c;

    for (int i = a.length() - 1; i >= 0; i--) {
        if (!carry && match(i)) { // nowa pozycja
            pos.push_back(1);
        } else if (carry && match(i)) { // nowa pozycja, kończymy nieudany blok carry
            carry = false;
            pos.push_back(-1);
            pos.push_back(1);
        } else if (!carry && overflow_match(i)) { // rozpoczynamy blok carry
            carry = true;
            carry_last = false;
        } else if (carry && plus_1_match(i)) { // kończymy udany blok carry
            carry = false;
            pos.push_back(1);
        } else if (carry && overflow_match(i)) { // rozpoczynamy blok carry, kończymy nieudany poprzedni
            carry_last = false;
            pos.push_back(-1);
        } else if (carry && overflow_plus_1_match(i)) { // carry trwa
            carry_last = true;
        } else { // brak carry i brak matcha
            carry = false;
            pos.push_back(-1);
        }
    }
    reverse(pos.begin(), pos.end());
    //for (int i = 0; i < pos.size(); i++) {
    //    cout << pos[i] << " ";
    //}
    //cout<<"\n";

    w = 0;
    last = -1;
    for (int i = pos.size() - 1; i >= 0; i--) {
        if (pos[i] == 1) {
            if (last == -1) {
                last = i;
            }
            w += (last - i + 1);
        } else {
            last = -1;
        }
    }
    cout << w << "\n";

    return 0;
}