#include <bits/stdc++.h>
using namespace std;
#define all(a) (a).begin(), (a).end()
typedef long long ll;
typedef vector<int> vi;
typedef vector<long long> vll;
typedef pair<int,int> pii;
int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cout.tie(NULL);
// freopen("./a.txt", "r", stdin);
string as,bs,cs;
cin>>as>>bs>>cs;
vi a(as.size()), b(bs.size()), c(cs.size());
for (int i=0; i<as.size(); i++) {
a[i] = as[i]-'0';
b[i] = bs[i]-'0';
c[i] = cs[i]-'0';
}
ll res = 0;
int st=a.size()-1, en=a.size()-1;
vector<bool> carries(a.size(),false);
while (en>=0 || st>=0) {
bool last_carry = false;
int no_carries = 0;
while (en>=0 && (a[en]+b[en]+last_carry)%10==c[en]) {
last_carry = a[en]+b[en]+last_carry>=10;
if (last_carry) {
no_carries++;
carries[en] = true;
}
en--;
}
res += st-en-no_carries;
while (st > en) {
if (!carries[st]) {
st--;
res += st-en-no_carries;
} else {
no_carries--;
for (int i=st-1; i>en; i--) {
if (a[i]+b[i]>=1) break;
if (!carries[i]) break;
carries[i] = false;
no_carries--;
}
st--;
}
}
if (st>=0 && (a[st]+b[st])%10 != c[st]) {
st--;
en--;
}
}
cout<<res<<'\n';
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 | #include <bits/stdc++.h> using namespace std; #define all(a) (a).begin(), (a).end() typedef long long ll; typedef vector<int> vi; typedef vector<long long> vll; typedef pair<int,int> pii; int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL); // freopen("./a.txt", "r", stdin); string as,bs,cs; cin>>as>>bs>>cs; vi a(as.size()), b(bs.size()), c(cs.size()); for (int i=0; i<as.size(); i++) { a[i] = as[i]-'0'; b[i] = bs[i]-'0'; c[i] = cs[i]-'0'; } ll res = 0; int st=a.size()-1, en=a.size()-1; vector<bool> carries(a.size(),false); while (en>=0 || st>=0) { bool last_carry = false; int no_carries = 0; while (en>=0 && (a[en]+b[en]+last_carry)%10==c[en]) { last_carry = a[en]+b[en]+last_carry>=10; if (last_carry) { no_carries++; carries[en] = true; } en--; } res += st-en-no_carries; while (st > en) { if (!carries[st]) { st--; res += st-en-no_carries; } else { no_carries--; for (int i=st-1; i>en; i--) { if (a[i]+b[i]>=1) break; if (!carries[i]) break; carries[i] = false; no_carries--; } st--; } } if (st>=0 && (a[st]+b[st])%10 != c[st]) { st--; en--; } } cout<<res<<'\n'; return 0; } |
English