#include<bits/stdc++.h>
using namespace std;
const int DEF = -1;
int main() {
std::ios_base::sync_with_stdio(false);
std::cin.tie(NULL);
string a, b, c;
cin >> a >> b >> c;
int n = a.size();
vector<int> yc(n, DEF);
vector<int> yn(n, DEF);
for (int i = n - 1; i >= 0; i--) {
int sum = (int)(a[i] - '0') + (int)(b[i] - '0');
int exp_sum = (int) (c[i] - '0');
if (sum == exp_sum) {
yn[i] = 0;
} else if (sum == exp_sum + 10) {
yn[i] = 1;
} else if (sum + 1 == exp_sum) {
yc[i] = 0;
} else if (sum + 1 == exp_sum + 10) {
yc[i] = 1;
}
}
long long pos_ends = 0LL;
bool carry = false;
long long total = 0LL;
for (int i = n-1; i >= 0; i--) {
if (carry) {
if (yc[i] == DEF) {
pos_ends = 0LL;
carry = false;
if (yn[i] != DEF) {
pos_ends++;
carry = (yn[i] == 1);
if (!carry) {
total += pos_ends;
}
}
} else {
carry = (yc[i] == 1);
if (!carry) {
total += pos_ends;
}
}
} else {
if (yn[i] == DEF) {
pos_ends = 0LL;
carry = false;
continue;
} else {
pos_ends++;
carry = (yn[i] == 1);
if (!carry) {
total += pos_ends;
}
}
}
}
cout << total;
}
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 | #include<bits/stdc++.h> using namespace std; const int DEF = -1; int main() { std::ios_base::sync_with_stdio(false); std::cin.tie(NULL); string a, b, c; cin >> a >> b >> c; int n = a.size(); vector<int> yc(n, DEF); vector<int> yn(n, DEF); for (int i = n - 1; i >= 0; i--) { int sum = (int)(a[i] - '0') + (int)(b[i] - '0'); int exp_sum = (int) (c[i] - '0'); if (sum == exp_sum) { yn[i] = 0; } else if (sum == exp_sum + 10) { yn[i] = 1; } else if (sum + 1 == exp_sum) { yc[i] = 0; } else if (sum + 1 == exp_sum + 10) { yc[i] = 1; } } long long pos_ends = 0LL; bool carry = false; long long total = 0LL; for (int i = n-1; i >= 0; i--) { if (carry) { if (yc[i] == DEF) { pos_ends = 0LL; carry = false; if (yn[i] != DEF) { pos_ends++; carry = (yn[i] == 1); if (!carry) { total += pos_ends; } } } else { carry = (yc[i] == 1); if (!carry) { total += pos_ends; } } } else { if (yn[i] == DEF) { pos_ends = 0LL; carry = false; continue; } else { pos_ends++; carry = (yn[i] == 1); if (!carry) { total += pos_ends; } } } } cout << total; } |
English