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

int tab[1000000];
bool ktore[1000000];
bool wskazuje[1000000];

int main(){
    ios_base::sync_with_stdio(0);
    cin.tie(0);
    string s1,s2,s3;
    cin>>s1>>s2>>s3;
    int N=(int)s1.size();
    for (int i=N-1;i>=0;i--){
        tab[i]+=(s1[i]-'0')+(s2[i]-'0');
        if (tab[i]>=10){
            int dodac=tab[i]/10;
            ktore[i]=1;
            if (i>0){
                tab[i-1]+=dodac;
                wskazuje[i-1]=1;
            }
            tab[i]%=10;
        }
    }
    int maks=-1;
    long long ans=0;
    for (int i=0;i<N;i++){
        if (ktore[i]) continue;
        if (i<=maks){
            ans+=maks-i+1;
       //    cout<<i<<" "<<maks<<"\n";
        } else{
            if (tab[i]!=(s3[i]-'0')){
                maks=-1;
            } else{
                for (int j=i;j<N;j++){
                    if (tab[j]==(s3[j]-'0') and wskazuje[j]==0){
                        maks=j;
                        break;
                    }
                }
                ans+=maks-i+1;
             //   cout<<i<<" "<<maks<<"\n";
            }
        }
    }
    cout<<ans<<"\n";
}