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
90
91
#include <iostream>
using namespace std;
const int stala=1e6+10;
int tab[3][stala];
int dostaje[stala];
int wysyla[stala];
bool nieprawidlowe[stala];

int main()
{
    ios_base::sync_with_stdio(0);
    cin.tie(0);
    cout.tie(0);
    string s,s2,s3;
    cin>>s;
    cin>>s2;
    cin>>s3;
    int ile=(int)s.length();
    for(int i=0;i<ile;i++){
        tab[0][i+1]=s[i]-'0';
    }
    for(int i=0;i<ile;i++){
        tab[1][i+1]=s2[i]-'0';
    }
    for(int i=0;i<ile;i++){
        tab[2][i+1]=s3[i]-'0';
    }
    for(int i=1;i<=ile;i++){
        int reszta=(tab[0][i]+tab[1][i])%10;
        if((reszta+1)%10==tab[2][i]){
            dostaje[i]=1;
        }
        else if(reszta!=tab[2][i]){
            nieprawidlowe[i]=1;
        }
        if(tab[0][i]+tab[1][i]+dostaje[i]>=10){
            wysyla[i]=1;
        }
    }
    for(int i=1;i<=ile;i++){
        if(nieprawidlowe[i]==0){
            if(dostaje[i]==1&&wysyla[i]==0){
                if(i+1>ile||nieprawidlowe[i+1]==1||wysyla[i+1]==0){
                    nieprawidlowe[i]=1;
                }
            }
            else if(dostaje[i]==1&&wysyla[i]==1){
                if(i-1<1||dostaje[i-1]==0||nieprawidlowe[i-1]==1){
                    nieprawidlowe[i]=1;
                }
                if(i+1>ile||nieprawidlowe[i+1]==1||wysyla[i+1]==0){
                    nieprawidlowe[i]=1;
                    for(int j=i-1;j>=1;j--){
                        if(nieprawidlowe[j]==1||dostaje[j]==0){
                            break;
                        }
                        nieprawidlowe[j]=1;
                    }
                }
            }
            else if(dostaje[i]==0&&wysyla[i]==1){
                if(i-1<1||dostaje[i-1]==0||nieprawidlowe[i-1]==1){
                    nieprawidlowe[i]=1;
                }
            }
        }
    }
    long long wyn=0;
    long long cnt=0;
    for(int i=1;i<=ile;i++){
        if(nieprawidlowe[i]==0){
            if(dostaje[i]==0&&wysyla[i]==0){
                cnt++;
                wyn+=cnt;
            }
            else if(dostaje[i]==0&&wysyla[i]==1){
                wyn+=cnt;
            }
            else if(dostaje[i]==1&&wysyla[i]==0){
                cnt++;
            }
        }
        else{
            cnt=0;
        }
    }
    cout<<wyn;


    return 0;
}