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
#include <bits/stdc++.h>
#define int long long

using namespace std;

signed main(){
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  string num1; cin >> num1;
  string num2; cin >> num2;
  string num3; cin >> num3;
  const int n = num1.size();
  vector<int> A(n);
  for(int i=0; i<n; i++)
    A[i] = num1[i] + num2[i] - num3[i] - '0';

  int res = 0;
  int l=0;
  while(l<n){
    if(A[l] != 0 && A[l] != -1) {
      l++;
      continue;
    }

    int atoms=0;
    int r=l;
    while(r<n){
      if(A[r] == 0){
        r++;
        atoms++;
        continue;
      }

      if(A[r] == -1){
        r++;
        while(r<n && A[r] == 9){
          r++;
        }
        if(r == n) continue;
        if(A[r] != 10) break;
        //A[r] is 10
        atoms++;
        r++;
        continue;
      }
      break;
    }

    res += atoms*(atoms+1)/2;
    l=r;
  }
  cout << res << '\n';
}