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
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
#include <iostream>
#include <vector>
#include <utility>

using namespace std;

void vecvecp(vector<std::vector<int>> spot) {
	for (int i=0; i<spot.size(); i++) {
		cout << i << ": ";
		for (int j=0; j<spot[i].size(); j++)
			cout << spot[i][j] << " ";
		cout << endl;
	}
}

void vecp(vector<int> spot) {
	for (int j=0; j<spot.size(); j++)
		cout << spot[j] << " ";
	cout << endl;
}

int main()
{
    ios_base::sync_with_stdio(0);
    cout.tie(0);
    cin.tie(0);

	std::vector<int> licz1, licz2, licz3;
	std::string linia;
    cin >> linia;
	for (char c : linia) {
		licz1.push_back(c - '0');
    }
    cin >> linia;
	for (char c : linia) {
		licz2.push_back(c - '0');
    }
    cin >> linia;
	for (char c : linia) {
		licz3.push_back(c - '0');
    }

	std::vector<pair<int, int>> kompres;
	bool inrow = false;
	bool insum = false;
	bool inx = false;
	int a,b,c,rodz;
	int serow, serx;

	int suma = 0;
	int dl_serii = 0;
	int r, w;


	for (int i=0; i<licz1.size(); i++) {
		a = licz1[i];
		b = licz2[i];
		c = licz3[i];
		rodz = a+b-c;
		if (inrow and rodz != 0) {
			kompres.push_back({0, serow});
			inrow = false;
			serow = 0;
		}
		if (inx and (rodz==0 or rodz == 10 or rodz == -1 or rodz== 9)) {
			kompres.push_back({8, serx});
			inx = false;
			serx = 0;
		}

		if (rodz==0) {
			if (inrow) {
				serow += 1;
			} else {
				inrow = true;
				serow = 1;
			}
		}

		if (rodz == 10 or rodz == -1 or rodz== 9)
			kompres.push_back({rodz, 1});
		
		if (not (rodz==0 or rodz == 10 or rodz == -1 or rodz== 9)) {
			if (inx) {
				serx += 1;
			} else {
				inx = true;
				serx = 1;
			}
		}
	}

	if (inrow)
		kompres.push_back({0, serow});
	if (inx)
		kompres.push_back({8, serx});
			
	
	for (int i = kompres.size()-1; i>=0; i--) {
		
		r = kompres[i].first;
		w = kompres[i].second;
		if (r == 0) {
			if (insum) 
				dl_serii = 0;
			
			insum = false;
			suma+= w*(w+1)/2 + w * dl_serii;
			dl_serii+=w;
			continue;
		}
		if (r == 10) {
			insum = true;
			continue;
		}
			
		if (insum and (r == 9))
			continue;
		
		if (insum and r == -1) {
			suma+= 1 + dl_serii;
			insum = false;
			dl_serii+=1;
			continue;
		}
		
		insum = false;
		dl_serii = 0;
		continue;
	}

	cout << suma << endl;


	return(0);
}