#include <bits/stdc++.h>
using namespace std;
string A, B, C;
int n;
int status[1000006];
// 0 - Incorrect (1 + 2 == 8)
// 1 - Valid (1 + 2 == 3)
// 2 - Valid if +1 (6 + 6 == 2) -> 6 + 6 = 12
// 3 - Valid if before +1 (4 + 3 == 8) -> 45 + 35 = 80
// 4 - Valid if +1 & b +1 (7 + 7 == 5) -> 74 + 75 = 150
// (1)(1)(32)(1)(1)
// (1)(1)(3442)(1)(1)
int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cin >> A >> B >> C;
n = A.size();
for (int i = 0; i < n; ++i) {
int a = (int)(A[i] - '0'), b = (int)(B[i] - '0'), c = (int)(C[i] - '0');
if (a + b == c) status[i] = 1;
else if (a + b == c + 10) status[i] = 2;
else if (a + b == c - 1) status[i] = 3;
else if (a + b == c + 9) status[i] = 4;
}
long long output = 0;
long long cnt = 0;
int i = 0;
while (i < n) {
if (status[i] == 1) ++cnt;
else if (status[i] == 3) {
++i;
while (status[i] == 4 && i < n - 1) ++i;
if (status[i] == 2) ++cnt;
else {
// cout << i << ": " << cnt * (cnt + 1) / 2 << '\n';
output += cnt * (cnt + 1) / 2;
cnt = 0;
continue;
}
}
else {
// cout << i << ": " << cnt * (cnt + 1) / 2 << '\n';
output += cnt * (cnt + 1) / 2;
cnt = 0;
}
++i;
}
// cout << i << ": " << cnt * (cnt + 1) / 2 << '\n';
output += cnt * (cnt + 1) / 2;
// for (int i = 0; i < n; ++i) std::cout << status[i];
// cout << "\n";
cout << output;
return 0;
}
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 <bits/stdc++.h> using namespace std; string A, B, C; int n; int status[1000006]; // 0 - Incorrect (1 + 2 == 8) // 1 - Valid (1 + 2 == 3) // 2 - Valid if +1 (6 + 6 == 2) -> 6 + 6 = 12 // 3 - Valid if before +1 (4 + 3 == 8) -> 45 + 35 = 80 // 4 - Valid if +1 & b +1 (7 + 7 == 5) -> 74 + 75 = 150 // (1)(1)(32)(1)(1) // (1)(1)(3442)(1)(1) int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); cin >> A >> B >> C; n = A.size(); for (int i = 0; i < n; ++i) { int a = (int)(A[i] - '0'), b = (int)(B[i] - '0'), c = (int)(C[i] - '0'); if (a + b == c) status[i] = 1; else if (a + b == c + 10) status[i] = 2; else if (a + b == c - 1) status[i] = 3; else if (a + b == c + 9) status[i] = 4; } long long output = 0; long long cnt = 0; int i = 0; while (i < n) { if (status[i] == 1) ++cnt; else if (status[i] == 3) { ++i; while (status[i] == 4 && i < n - 1) ++i; if (status[i] == 2) ++cnt; else { // cout << i << ": " << cnt * (cnt + 1) / 2 << '\n'; output += cnt * (cnt + 1) / 2; cnt = 0; continue; } } else { // cout << i << ": " << cnt * (cnt + 1) / 2 << '\n'; output += cnt * (cnt + 1) / 2; cnt = 0; } ++i; } // cout << i << ": " << cnt * (cnt + 1) / 2 << '\n'; output += cnt * (cnt + 1) / 2; // for (int i = 0; i < n; ++i) std::cout << status[i]; // cout << "\n"; cout << output; return 0; } |
English