#include <bits/stdc++.h>
using namespace std;
using ll = long long;
string nums[3];
const int nax = 1000 * 1000 + 10;
int carry[nax], need[nax], prefix_sum[nax], match[nax];
int char_to_int(char c) {
return c - '0';
}
int main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
int n;
for (int i = 0; i < 3; ++i) {
cin >> nums[i];
}
n = (int)nums[0].size();
for (int i = 1; i <= n; ++i) {
int sum = char_to_int(nums[0][i-1]) + char_to_int(nums[1][i-1]);
if (sum >= 10) {
carry[i] = 1;
} else {
carry[i] = 0;
}
if (char_to_int(nums[2][i-1]) == sum % 10) {
need[i] = 0;
} else if (char_to_int(nums[2][i-1]) == (sum + 1) % 10) {
need[i] = 1;
} else {
need[i] = -1;
}
}
prefix_sum[0] = 0;
for (int i = 1; i <= n; ++i) {
prefix_sum[i] = prefix_sum[i-1] + (carry[i] == 0);
}
ll answer = 0;
int ptr = 2;
for (int j = 1; j <= n; ++j) {
// fix carry bit
int consider = j;
if (carry[j] == 1) {
for (int i = j-1; i >= 1; --i) {
int sum = char_to_int(nums[0][i-1]) + char_to_int(nums[1][i-1]);
if (carry[i] == 0 && (sum + 1) >= 10) {
carry[i] = 1;
consider = i;
} else {
break;
}
}
}
for (int i = consider; i <= j; ++i) {
prefix_sum[i] = prefix_sum[i - 1] + (carry[i] == 0);
if (i > 1 && need[i-1] == carry[i]) {
match[i] = match[i-1] + 1;
} else {
match[i] = 0;
}
}
if (need[j] == 0) {
answer += prefix_sum[j] - prefix_sum[j - match[j] - 1];
}
}
cout << answer << "\n";
// for (int i = 1; i <= n; ++i) {
// cout << carry[i] << " ";
// }
// cout << "\n";
// for (int i = 1; i <= n; ++i) {
// cout << need[i] << " ";
// }
// cout << "\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 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 | #include <bits/stdc++.h> using namespace std; using ll = long long; string nums[3]; const int nax = 1000 * 1000 + 10; int carry[nax], need[nax], prefix_sum[nax], match[nax]; int char_to_int(char c) { return c - '0'; } int main() { ios_base::sync_with_stdio(0); cin.tie(0); int n; for (int i = 0; i < 3; ++i) { cin >> nums[i]; } n = (int)nums[0].size(); for (int i = 1; i <= n; ++i) { int sum = char_to_int(nums[0][i-1]) + char_to_int(nums[1][i-1]); if (sum >= 10) { carry[i] = 1; } else { carry[i] = 0; } if (char_to_int(nums[2][i-1]) == sum % 10) { need[i] = 0; } else if (char_to_int(nums[2][i-1]) == (sum + 1) % 10) { need[i] = 1; } else { need[i] = -1; } } prefix_sum[0] = 0; for (int i = 1; i <= n; ++i) { prefix_sum[i] = prefix_sum[i-1] + (carry[i] == 0); } ll answer = 0; int ptr = 2; for (int j = 1; j <= n; ++j) { // fix carry bit int consider = j; if (carry[j] == 1) { for (int i = j-1; i >= 1; --i) { int sum = char_to_int(nums[0][i-1]) + char_to_int(nums[1][i-1]); if (carry[i] == 0 && (sum + 1) >= 10) { carry[i] = 1; consider = i; } else { break; } } } for (int i = consider; i <= j; ++i) { prefix_sum[i] = prefix_sum[i - 1] + (carry[i] == 0); if (i > 1 && need[i-1] == carry[i]) { match[i] = match[i-1] + 1; } else { match[i] = 0; } } if (need[j] == 0) { answer += prefix_sum[j] - prefix_sum[j - match[j] - 1]; } } cout << answer << "\n"; // for (int i = 1; i <= n; ++i) { // cout << carry[i] << " "; // } // cout << "\n"; // for (int i = 1; i <= n; ++i) { // cout << need[i] << " "; // } // cout << "\n"; } |
English