#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using vi = vector<int>;
using pii = pair<int,int>;
#define mp make_pair
#define pb push_back
#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())
bool case_0_0(int a, int b, int c){ return a+b == c; }
bool case_0_1(int a, int b, int c){ return a+b+1 == c; }
bool case_1_0(int a, int b, int c){ return a+b-10 == c; }
bool case_1_1(int a, int b, int c){ return a+b-9 == c; }
bool case_none(int a, int b, int c){
return !case_0_0(a,b,c) && !case_0_1(a,b,c) && !case_1_0(a,b,c) && !case_1_1(a,b,c);
}
bool case_requires(int a, int b, int c){ // wymaga przeniesienia
return a+b+1 >= 10 ? a+b+1-10 == c : a+b+1 == c;
}
bool case_gives(int a, int b, int c){ // daje przeniesienie
return case_1_0(a,b,c) || case_1_1(a,b,c);
}
int main(){
cin.tie(0)->sync_with_stdio(0);
string a,b,c;
cin >> a >> b >> c;
const int n = sz(a);
each(x,a) x-='0';
each(x,b) x-='0';
each(x,c) x-='0';
bool broken[n] = {};
rep(i,0,n) if(case_none(a[i],b[i],c[i])) broken[i] = true;
rep(i,0,n){
if((i-1<0 || !case_requires(a[i-1],b[i-1],c[i-1])) && case_gives(a[i],b[i],c[i]))
broken[i] = true;
}
rep(i,0,n){
if(case_requires(a[i],b[i],c[i]) && (i+1>=n || !case_gives(a[i+1],b[i+1],c[i+1])))
broken[i] = true;
}
rep(i,1,n){
if(broken[i-1] && case_gives(a[i],b[i],c[i]))
broken[i] = true;
}
for(int i=n-2; i>=0; --i){
if(case_requires(a[i],b[i],c[i]) && broken[i+1])
broken[i] = true;
}
ll ans = 0;
int L=0,R,cnt;
while(L<n){
if(broken[L] || case_gives(a[L],b[L],c[L])){
++L;
continue;
}
R = L;
cnt = 1;
while(R+1<n && !broken[R+1]){
cnt += !case_requires(a[R],b[R],c[R]);
++R;
}
ans += ll(cnt) * (cnt+1) / 2;
L = R+1;
}
cout << ans;
}
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 72 73 74 75 76 | #include <bits/stdc++.h> using namespace std; using ll = long long; using vi = vector<int>; using pii = pair<int,int>; #define mp make_pair #define pb push_back #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()) bool case_0_0(int a, int b, int c){ return a+b == c; } bool case_0_1(int a, int b, int c){ return a+b+1 == c; } bool case_1_0(int a, int b, int c){ return a+b-10 == c; } bool case_1_1(int a, int b, int c){ return a+b-9 == c; } bool case_none(int a, int b, int c){ return !case_0_0(a,b,c) && !case_0_1(a,b,c) && !case_1_0(a,b,c) && !case_1_1(a,b,c); } bool case_requires(int a, int b, int c){ // wymaga przeniesienia return a+b+1 >= 10 ? a+b+1-10 == c : a+b+1 == c; } bool case_gives(int a, int b, int c){ // daje przeniesienie return case_1_0(a,b,c) || case_1_1(a,b,c); } int main(){ cin.tie(0)->sync_with_stdio(0); string a,b,c; cin >> a >> b >> c; const int n = sz(a); each(x,a) x-='0'; each(x,b) x-='0'; each(x,c) x-='0'; bool broken[n] = {}; rep(i,0,n) if(case_none(a[i],b[i],c[i])) broken[i] = true; rep(i,0,n){ if((i-1<0 || !case_requires(a[i-1],b[i-1],c[i-1])) && case_gives(a[i],b[i],c[i])) broken[i] = true; } rep(i,0,n){ if(case_requires(a[i],b[i],c[i]) && (i+1>=n || !case_gives(a[i+1],b[i+1],c[i+1]))) broken[i] = true; } rep(i,1,n){ if(broken[i-1] && case_gives(a[i],b[i],c[i])) broken[i] = true; } for(int i=n-2; i>=0; --i){ if(case_requires(a[i],b[i],c[i]) && broken[i+1]) broken[i] = true; } ll ans = 0; int L=0,R,cnt; while(L<n){ if(broken[L] || case_gives(a[L],b[L],c[L])){ ++L; continue; } R = L; cnt = 1; while(R+1<n && !broken[R+1]){ cnt += !case_requires(a[R],b[R],c[R]); ++R; } ans += ll(cnt) * (cnt+1) / 2; L = R+1; } cout << ans; } |
English