#include <bits/stdc++.h>
#define FOR(i, a, b) for(int i = (int)a; i <= (int)b; ++i)
#define RFOR(i, a, b) for(int i = (int)a; i >= (int)b; --i)
#define in insert
#define ll long long
#define pb push_back
#define fi first
#define se second
using namespace std;
const int MAX = 1e6 + 7;
ll odl[MAX], pr[MAX], sums[MAX];
ll f9[MAX], sp[MAX];
void solve() {
string n1, n2, n3; cin >> n1 >> n2 >> n3;
n1 = '0' + n1, n2 = '0' + n2, n3 = '0' + n3;
int n = n1.size() - 1, pop = 0;
RFOR(i, n, 1) {
ll sum = (n1[i] - '0') + (n2[i] - '0');
ll l = n3[i] - '0';
if((sum % 10) > l) l += 10;
int roz = l - (sum % 10);
odl[i] = roz;
sums[i] = sum;
pr[i - 1] = (sum + pop) / 10;
pop = pr[i - 1];
}
//prep
sp[0] = pr[0];
odl[0] = 1e9;
FOR(i, 1, n) {
if(sums[i] == 9 && sums[i - 1] != 9) f9[i] = i;
else if(sums[i] == 9) f9[i] = f9[i - 1];
sp[i] = sp[i - 1] + pr[i];
}
ll res = 0, ind = n;
RFOR(i, n, 1) {
ll ile = 0; bool tf = false;
if(odl[i] != 0) continue;
if(pr[i] == 1 && sums[i] == 9) {
tf = true;
FOR(j, f9[i] - 1, i - 1) {
pr[j] = 0;
if(j > 0) sp[j] = sp[j - 1];
else sp[j] = 0;
}
}
if(tf || i <= ind) {
ind = i;
RFOR(j, i - 1, 0) {
if(pr[j] != odl[j]) {ind = j; break;}
}
}
/*RFOR(j, i - 1, 1) {
if(odl[j] != pr[j]) {ind = j; break;}
if(pr[j] == 1) ++ile;
}*/
ll zeg = sp[i - 1];
if(ind != 0) zeg -= sp[ind - 1];
ll dl = i - ind - zeg;
res += dl;
}
cout << res;
}
int main() {
ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
solve();
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 | #include <bits/stdc++.h> #define FOR(i, a, b) for(int i = (int)a; i <= (int)b; ++i) #define RFOR(i, a, b) for(int i = (int)a; i >= (int)b; --i) #define in insert #define ll long long #define pb push_back #define fi first #define se second using namespace std; const int MAX = 1e6 + 7; ll odl[MAX], pr[MAX], sums[MAX]; ll f9[MAX], sp[MAX]; void solve() { string n1, n2, n3; cin >> n1 >> n2 >> n3; n1 = '0' + n1, n2 = '0' + n2, n3 = '0' + n3; int n = n1.size() - 1, pop = 0; RFOR(i, n, 1) { ll sum = (n1[i] - '0') + (n2[i] - '0'); ll l = n3[i] - '0'; if((sum % 10) > l) l += 10; int roz = l - (sum % 10); odl[i] = roz; sums[i] = sum; pr[i - 1] = (sum + pop) / 10; pop = pr[i - 1]; } //prep sp[0] = pr[0]; odl[0] = 1e9; FOR(i, 1, n) { if(sums[i] == 9 && sums[i - 1] != 9) f9[i] = i; else if(sums[i] == 9) f9[i] = f9[i - 1]; sp[i] = sp[i - 1] + pr[i]; } ll res = 0, ind = n; RFOR(i, n, 1) { ll ile = 0; bool tf = false; if(odl[i] != 0) continue; if(pr[i] == 1 && sums[i] == 9) { tf = true; FOR(j, f9[i] - 1, i - 1) { pr[j] = 0; if(j > 0) sp[j] = sp[j - 1]; else sp[j] = 0; } } if(tf || i <= ind) { ind = i; RFOR(j, i - 1, 0) { if(pr[j] != odl[j]) {ind = j; break;} } } /*RFOR(j, i - 1, 1) { if(odl[j] != pr[j]) {ind = j; break;} if(pr[j] == 1) ++ile; }*/ ll zeg = sp[i - 1]; if(ind != 0) zeg -= sp[ind - 1]; ll dl = i - ind - zeg; res += dl; } cout << res; } int main() { ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); solve(); return 0; } |
English