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
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
#include <bits/stdc++.h>
using namespace std;

const char Z = '0'; // zero
const char T = 'T'; // true
const char F = 'F'; // false
const char B = 'B'; // begin
const char E = 'E'; // end
const char C = 'C'; // continue


void fillState(const string &s1, const string &s2, const string &s3,
		string &out) {
	const int L = s1.length();
	int cb = 0, res, currentOut;
	for (int i = L - 1; i >= 0; i--) {
		res = s1[i] - Z + s2[i] - Z;
		currentOut = s3[i] - Z;
		if (res == currentOut) {
			out[i] = T;
			cb = 0;
		} else if (res % 10 == currentOut) {
			out[i] = B;
			cb = 1;
		} else {
			res += cb; // from cb
			if (res == currentOut) {
				out[i] = E;
				cb = 0;
			} else if (res % 10 == currentOut) {
				out[i] = C;
				cb = 1;
			} else {
				out[i] = F;
				cb = 0;
			}

		}

	}
}
long long int count(long long int gl) {
	return gl * (gl + 1) / 2;
}

int main() {

	cin.tie(NULL);
	cout.tie(NULL);
	ios_base::sync_with_stdio(false);
	string lineA;
	string lineB;
	string lineC;

	cin >> lineA;
	cin >> lineB;
	cin >> lineC;

	const int L = lineA.length();
	string state(L, ' ');

	fillState(lineA, lineB, lineC, state);


	long long int groupLength = 0;
	long long int result = 0;

	bool inside = false;
	for (int i = L - 1; i >= 0; i--) {
		const char ch = state[i];
		if (inside) {
			if (ch == B) {
				result += count(groupLength);
				groupLength = 0;
				inside = true;
			} else if (ch == C) {
			} else if (ch == E) {
				groupLength++;
				inside = false;
			} else if (ch == F) {
				result += count(groupLength);
				groupLength = 0;
				inside = false;
			} else if (ch == T) {
				result += count(groupLength);
				groupLength = 0;
				groupLength++;
				inside = false;
			}
		} else {
			if (ch == B) {
				inside = true;
			} else if (ch == C) {
				cout << "exception C" << endl;
			} else if (ch == E) {
				cout << "exception E" << endl;
			} else if (ch == F) {
				result += count(groupLength);
				groupLength = 0;
			} else if (ch == T) {
				groupLength++;
			}
		}

	}
	result += count(groupLength);
	cout << result << endl;

}