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
#include <cstdio>
#include <cstring>

using namespace std;

const int N = 1000005;

inline int v(char c) {
  return c - '0';
}

int main() {
  char a[N], b[N], c[N];
  scanf(" %s", a);
  scanf(" %s", b);
  scanf(" %s", c);
  int n = strlen(a);

  long long res = 0;
  int g = 0;
  int s = 0;

  for (int i = n-1; i >= 0; --i) {
    int top = v(a[i]) + v(b[i]);
    int bot = v(c[i]);

    if (s == 1) {
      if (top + 1 == bot) {
        ++g;
        s = 0;
        continue;
      } else if (top + 1 == bot + 10) {
        s = 1;
        continue;
      } else {
        res += (long long)g * (g+1) / 2;
        g = 0;
        s = 0;
      }
    }

    if (top == bot) {
      ++g;
      s = 0;
      continue;
    }

    if (top == bot + 10) {
      s = 1;
      continue;
    }

    res += (long long)g * (g+1) / 2;
    g = 0;
    s = 0;
  }
    
  res += (long long)g * (g+1) / 2;

  printf("%lld\n", res);

  return 0;
};