#include <bits/stdc++.h>
using namespace std;
string a, b, c;
pair<bool, bool> can_extend(int pos, bool need_carry) {
bool needed_carry = need_carry;
if (!needed_carry) {
if ((a[pos] - '0') + (b[pos] - '0') == c[pos] - '0') {
need_carry = false;
return {need_carry, true};
}
if ((a[pos] - '0') + b[pos] - '0' + 1 == c[pos] - '0') {
need_carry = true;
return {need_carry, true};
}
return {false, false};
}
if (needed_carry) {
if ((a[pos] - '0') + (b[pos] - '0') == c[pos] - '0' + 10) {
need_carry = false;
return {need_carry, true};
}
if ((a[pos] - '0') + b[pos] - '0' + 1 == c[pos] - '0' + 10) {
need_carry = true;
return {need_carry, true};
}
return {false, false};
}
}
int main() {
std::ios_base::sync_with_stdio(false);
cin >> a >> b >> c;
int length = a.length();
long long result = 0;
for(int p = 0; p < length;) {
int k = p - 1;
bool need_carry = false;
bool can_go = true;
long long counted_good_fragments = 0;
while (k < length - 1 && can_go) {
auto aux = can_extend(k+1, need_carry);
need_carry = aux.first;
can_go = aux.second;
if (can_go && !need_carry) {
counted_good_fragments++;
}
k++;
}
result += counted_good_fragments * (counted_good_fragments + 1) / 2;
//cout << "Starting at " << p << " we had " << counted_good_fragments << " fragments\n";
if (can_go && !need_carry) {
k++;
}
p = max(p + 1, k);
}
cout << result << "\n";
}
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 | #include <bits/stdc++.h> using namespace std; string a, b, c; pair<bool, bool> can_extend(int pos, bool need_carry) { bool needed_carry = need_carry; if (!needed_carry) { if ((a[pos] - '0') + (b[pos] - '0') == c[pos] - '0') { need_carry = false; return {need_carry, true}; } if ((a[pos] - '0') + b[pos] - '0' + 1 == c[pos] - '0') { need_carry = true; return {need_carry, true}; } return {false, false}; } if (needed_carry) { if ((a[pos] - '0') + (b[pos] - '0') == c[pos] - '0' + 10) { need_carry = false; return {need_carry, true}; } if ((a[pos] - '0') + b[pos] - '0' + 1 == c[pos] - '0' + 10) { need_carry = true; return {need_carry, true}; } return {false, false}; } } int main() { std::ios_base::sync_with_stdio(false); cin >> a >> b >> c; int length = a.length(); long long result = 0; for(int p = 0; p < length;) { int k = p - 1; bool need_carry = false; bool can_go = true; long long counted_good_fragments = 0; while (k < length - 1 && can_go) { auto aux = can_extend(k+1, need_carry); need_carry = aux.first; can_go = aux.second; if (can_go && !need_carry) { counted_good_fragments++; } k++; } result += counted_good_fragments * (counted_good_fragments + 1) / 2; //cout << "Starting at " << p << " we had " << counted_good_fragments << " fragments\n"; if (can_go && !need_carry) { k++; } p = max(p + 1, k); } cout << result << "\n"; } |
English