#include <bits/stdc++.h>
#define rep(a,b,c) for(auto a = (b); a != (c); a++)
#define repD(a,b,c) for(auto a = (b); a != (c); a--)
#define repIn(a, b) for(auto& a : (b))
#define repIn2(a, b, c) for(auto& [a, b] : (c))
constexpr bool dbg = 0;
#define DEBUG if constexpr(dbg)
#define DC DEBUG std::cerr
#define eol std::endl
#define int long long
#define ld long double
#define pb push_back
using namespace std;
#define rg ranges
int32_t main() {
ios_base::sync_with_stdio(false); cin.tie(nullptr); cout.tie(nullptr);
string a, b, c;
cin >> a >> b >> c;
int n = (int) a.size();
vector<bool> needL(n), needR(n), notR(n), never(n);
bool carry = 0;
repD(i, n - 1, -1) {
auto x = a[i] - '0', y = b[i] - '0', z = c[i] - '0';
if((x + y) % 10 == z) {
notR[i] = carry;
carry = (x + y) / 10;
needL[i] = carry;
}
else if((x + y + carry) % 10 == z) {
needR[i] = 1;
carry = (x + y + carry) / 10;
needL[i] = carry;
}
else never[i] = 1;
}
int j = -1, ends = 0, ans = 0;
rep(i, 0, n) {
if(i && j >= i) ends -= !needR[i - 1];
if(!never[i] && !needL[i]) {
if(j < i) j = i, ends = 0;
while(j < n && !never[j] && (j == i || !notR[j - 1])) {
ends += !needR[j];
j++;
}
ans += ends;
DC << i << ' ' << ends << eol;
}
else DC << i << " 0" << eol;
}
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 | #include <bits/stdc++.h> #define rep(a,b,c) for(auto a = (b); a != (c); a++) #define repD(a,b,c) for(auto a = (b); a != (c); a--) #define repIn(a, b) for(auto& a : (b)) #define repIn2(a, b, c) for(auto& [a, b] : (c)) constexpr bool dbg = 0; #define DEBUG if constexpr(dbg) #define DC DEBUG std::cerr #define eol std::endl #define int long long #define ld long double #define pb push_back using namespace std; #define rg ranges int32_t main() { ios_base::sync_with_stdio(false); cin.tie(nullptr); cout.tie(nullptr); string a, b, c; cin >> a >> b >> c; int n = (int) a.size(); vector<bool> needL(n), needR(n), notR(n), never(n); bool carry = 0; repD(i, n - 1, -1) { auto x = a[i] - '0', y = b[i] - '0', z = c[i] - '0'; if((x + y) % 10 == z) { notR[i] = carry; carry = (x + y) / 10; needL[i] = carry; } else if((x + y + carry) % 10 == z) { needR[i] = 1; carry = (x + y + carry) / 10; needL[i] = carry; } else never[i] = 1; } int j = -1, ends = 0, ans = 0; rep(i, 0, n) { if(i && j >= i) ends -= !needR[i - 1]; if(!never[i] && !needL[i]) { if(j < i) j = i, ends = 0; while(j < n && !never[j] && (j == i || !notR[j - 1])) { ends += !needR[j]; j++; } ans += ends; DC << i << ' ' << ends << eol; } else DC << i << " 0" << eol; } cout << ans << '\n'; } |
English