#include <bits/stdc++.h>
using namespace std;
string a, b, c;
long long atoi (char x) {
return (long long)x - 48;
}
void wypisz (vector<pair<long long, long long>> x) {
for (auto t : x) {
cout << t.first << ' ' << t.second << endl;
}
}
long long merge (vector<pair<long long, long long>> ans) {
long long score = 0, dummy = 0;
for (long long i = 1; i < ans.size(); i++) {
if (ans[i - 1].first - 1 == ans[i].second) {
dummy++;
continue;
}
score += (dummy + 1) * (dummy + 2) / 2 - dummy - 1;
dummy = 0;
}
if (dummy) {
score += (dummy + 1) * (dummy + 2) / 2 - dummy - 1;
}
//cout << score << endl;
return score;
}
long long solve () {
cin >> a >> b >> c;
vector<pair<long long, long long>> ans;
long long right = a.length() - 1, dodaj = 0;
for (long long left = a.length() - 1; left >= 0; left--) {
if ((atoi(a[left]) + atoi(b[left]) + dodaj) % 10 != atoi(c[left])) {
// Może bez przeniesienia jest ok
if (dodaj == 1) {
dodaj = 0;
right = left;
left++;
}
// Jeśli nawet bez przeniesienia nie wchodzi to pomijam
else {
right = left - 1;
}
}
else {
if (atoi(a[left]) + atoi(b[left]) + dodaj < 10) {
ans.push_back(make_pair(left, right));
right = left - 1;
dodaj = 0;
}
else {
dodaj = 1;
}
}
}
//wypisz(ans);
return ans.size() + merge(ans);
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cout << solve() << endl;
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 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 | #include <bits/stdc++.h> using namespace std; string a, b, c; long long atoi (char x) { return (long long)x - 48; } void wypisz (vector<pair<long long, long long>> x) { for (auto t : x) { cout << t.first << ' ' << t.second << endl; } } long long merge (vector<pair<long long, long long>> ans) { long long score = 0, dummy = 0; for (long long i = 1; i < ans.size(); i++) { if (ans[i - 1].first - 1 == ans[i].second) { dummy++; continue; } score += (dummy + 1) * (dummy + 2) / 2 - dummy - 1; dummy = 0; } if (dummy) { score += (dummy + 1) * (dummy + 2) / 2 - dummy - 1; } //cout << score << endl; return score; } long long solve () { cin >> a >> b >> c; vector<pair<long long, long long>> ans; long long right = a.length() - 1, dodaj = 0; for (long long left = a.length() - 1; left >= 0; left--) { if ((atoi(a[left]) + atoi(b[left]) + dodaj) % 10 != atoi(c[left])) { // Może bez przeniesienia jest ok if (dodaj == 1) { dodaj = 0; right = left; left++; } // Jeśli nawet bez przeniesienia nie wchodzi to pomijam else { right = left - 1; } } else { if (atoi(a[left]) + atoi(b[left]) + dodaj < 10) { ans.push_back(make_pair(left, right)); right = left - 1; dodaj = 0; } else { dodaj = 1; } } } //wypisz(ans); return ans.size() + merge(ans); } int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); cout << solve() << endl; return 0; } |
English