#include <bits/stdc++.h>
using namespace std;
using ll = long long;
constexpr int N = 1e6 + 5;
int a[N], b[N], c[N], n, mul, rem;
string s1, s2, s3;
bool state;
ll ans = 0;
int main() {
ios::sync_with_stdio(0); cin.tie(0);
cin >> s1 >> s2 >> s3;
n = s1.size();
for(int i = 0; i < n; ++i) a[i] = s1[i] - '0', b[i] = s2[i] - '0', c[i] = s3[i] - '0';
mul = 0, ans = 0, state = 0, rem = 0;
for(int i = n - 1; i >= 0; --i) {
if(state) {
if(a[i] + b[i] == c[i]) {
mul = 1;
rem = 0;
state = 0;
ans += mul;
} else if ((a[i] + b[i]) % 10 == c[i]){
mul = 0;
rem = (a[i] + b[i]) / 10;
} else if(a[i] + b[i] + rem == c[i]) {
++mul;
state = 0;
rem = 0;
ans += mul;
} else if((a[i] + b[i] + rem) % 10 == c[i]) {
rem = (a[i] + b[i] + rem) / 10;
} else {
mul = 0;
rem = 0;
state = 0;
}
} else {
if(a[i] + b[i] == c[i]) {
mul++;
ans += mul;
} else if((a[i] + b[i]) % 10 == c[i]) {
state = 1;
rem = (a[i] + b[i]) / 10;
} else {
mul = 0;
}
}
}
cout << ans << '\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 | #include <bits/stdc++.h> using namespace std; using ll = long long; constexpr int N = 1e6 + 5; int a[N], b[N], c[N], n, mul, rem; string s1, s2, s3; bool state; ll ans = 0; int main() { ios::sync_with_stdio(0); cin.tie(0); cin >> s1 >> s2 >> s3; n = s1.size(); for(int i = 0; i < n; ++i) a[i] = s1[i] - '0', b[i] = s2[i] - '0', c[i] = s3[i] - '0'; mul = 0, ans = 0, state = 0, rem = 0; for(int i = n - 1; i >= 0; --i) { if(state) { if(a[i] + b[i] == c[i]) { mul = 1; rem = 0; state = 0; ans += mul; } else if ((a[i] + b[i]) % 10 == c[i]){ mul = 0; rem = (a[i] + b[i]) / 10; } else if(a[i] + b[i] + rem == c[i]) { ++mul; state = 0; rem = 0; ans += mul; } else if((a[i] + b[i] + rem) % 10 == c[i]) { rem = (a[i] + b[i] + rem) / 10; } else { mul = 0; rem = 0; state = 0; } } else { if(a[i] + b[i] == c[i]) { mul++; ans += mul; } else if((a[i] + b[i]) % 10 == c[i]) { state = 1; rem = (a[i] + b[i]) / 10; } else { mul = 0; } } } cout << ans << '\n'; } |
English