#ifdef LOC
#include "debuglib.hpp"
#else
#define _USE_MATH_DEFINES
#include <bits/stdc++.h>
#define deb(...)
#define DBP(...)
#endif
using namespace std;
using ll = long long;
using vi = vector<int>;
using pii = pair<int, int>;
#define pb push_back
#define mp make_pair
#define x first
#define y second
#define rep(i, b, e) for (int i = (b); i < (e); i++)
#define each(a, x) for (auto& a : (x))
#define all(x) (x).begin(), (x).end()
#define sz(x) int((x).size())
int main() {
cin.sync_with_stdio(0); cin.tie(0);
string a, b, c;
cin >> a >> b >> c;
int n = sz(a);
vector<bool> ok(n+2);
vector<bool> needOverflow(n+2);
vector<bool> hasOverflow(n+2);
rep(i, 0, n) {
int x = a[i] - '0';
int y = b[i] - '0';
int z = c[i] - '0';
if ((x+y+1)%10 == z) {
ok[i+1] = 1;
needOverflow[i+1] = 1;
hasOverflow[i+1] = (x+y+1) / 10;
} else if ((x+y)%10 == z) {
ok[i+1] = 1;
hasOverflow[i+1] = (x+y) / 10;
}
}
for (int i = 1; i <= n; i++) {
ok[i] = ok[i] && (!hasOverflow[i] || (ok[i-1] && needOverflow[i-1]));
}
for (int i = n; i >= 1; i--) {
ok[i] = ok[i] && (!needOverflow[i] || (ok[i+1] && hasOverflow[i+1]));
}
ll ans = 0;
for (int i = 1; i <= n; i++) {
if (!ok[i] || ok[i-1]) continue;
ll segs = 0;
for (int j = i; j <= n && ok[j]; j++) {
segs += !hasOverflow[j];
}
ans += segs * (segs+1) / 2;
}
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 55 56 57 58 59 60 61 62 63 64 65 66 | #ifdef LOC #include "debuglib.hpp" #else #define _USE_MATH_DEFINES #include <bits/stdc++.h> #define deb(...) #define DBP(...) #endif using namespace std; using ll = long long; using vi = vector<int>; using pii = pair<int, int>; #define pb push_back #define mp make_pair #define x first #define y second #define rep(i, b, e) for (int i = (b); i < (e); i++) #define each(a, x) for (auto& a : (x)) #define all(x) (x).begin(), (x).end() #define sz(x) int((x).size()) int main() { cin.sync_with_stdio(0); cin.tie(0); string a, b, c; cin >> a >> b >> c; int n = sz(a); vector<bool> ok(n+2); vector<bool> needOverflow(n+2); vector<bool> hasOverflow(n+2); rep(i, 0, n) { int x = a[i] - '0'; int y = b[i] - '0'; int z = c[i] - '0'; if ((x+y+1)%10 == z) { ok[i+1] = 1; needOverflow[i+1] = 1; hasOverflow[i+1] = (x+y+1) / 10; } else if ((x+y)%10 == z) { ok[i+1] = 1; hasOverflow[i+1] = (x+y) / 10; } } for (int i = 1; i <= n; i++) { ok[i] = ok[i] && (!hasOverflow[i] || (ok[i-1] && needOverflow[i-1])); } for (int i = n; i >= 1; i--) { ok[i] = ok[i] && (!needOverflow[i] || (ok[i+1] && hasOverflow[i+1])); } ll ans = 0; for (int i = 1; i <= n; i++) { if (!ok[i] || ok[i-1]) continue; ll segs = 0; for (int j = i; j <= n && ok[j]; j++) { segs += !hasOverflow[j]; } ans += segs * (segs+1) / 2; } cout << ans << '\n'; } |
English