#include <bits/stdc++.h>
#include <iomanip>
using namespace std;
using ll = long long ;
using pii = pair <int,int> ;
using pll = pair<ll,ll> ;
using vi = vector <int> ;
using vll = vector < ll > ;
using tiii = tuple<int,int,int> ;
using ld = long double ;
#define cYes {cout<<"YES\n";return;}
#define cNo {cout<<"NO\n";return;}
#define bra(x) "[" << (x) << "] "
#define ndl '\n' ;
#define all(x) (x).begin() , (x).end()
#define sz(x) (int)(x).size()
#define nd second
#define st first
#define vvi vector<vector<int>>
//#define DEBUG
#ifdef DEBUG
#define dbg(x) cerr << #x << " = " << x << endl
#else
#define dbg(x)
#endif
const int maxn = 1'000'007 ;
string S1 , S2 , SW ;
int N ;
int t1[maxn] , t2[maxn] , tres[maxn] , needed[maxn] ;
int dp[maxn][2] ; // ile 0 , ile 1
int main(){
ios_base::sync_with_stdio(false) ; cin.tie(0) ;
cin >> S1 >> S2 >> SW ;
N = sz(S2) ;
for(int i=1;i<=N;i++){
t1[i] = S1[i-1] - '0' ;
t2[i] = S2[i-1] - '0' ;
tres[i] = SW[i-1] - '0' ;
int flaga ;
if( ( t1[i] + t2[i] ) % 10 == tres[i] ) flaga = 0 ;
else if( ( t1[i] + t2[i] +1 + 10 ) % 10 == tres[i] ) flaga = 1 ;
else flaga = -1 ;
needed[i] = flaga ;
}
for(int i=N;i>=1;i--){
dbg(i) ;
if(needed[i] == -1 ) continue ;
if( needed[i] == 0 ){
int flag = ( t1[i] + t2[i] >= 10 ) ;
dp[i][flag] = dp[i+1][0] + 1 ;
}
else if( needed[i] == 1 ){
dbg("jestem") ;
int flag = ( t1[i] + t2[i] >= 9 ) ;
dp[i][flag] = dp[i+1][1] ;
}
else { assert(false) ; }
}
for(int i=1;i<=N;i++){
dbg(i) ;
dbg(dp[i][0]) ;
dbg(dp[i][1]) ;
}
ll sum = 0 ;
for(int i=1;i<=N;i++){
sum += dp[i][0] ;
// if( tres[i-1] == 1 ){
// sum += dp[i][1] ;
// }
}
cout << sum << ndl ;
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 72 73 74 75 76 77 78 79 80 | #include <bits/stdc++.h> #include <iomanip> using namespace std; using ll = long long ; using pii = pair <int,int> ; using pll = pair<ll,ll> ; using vi = vector <int> ; using vll = vector < ll > ; using tiii = tuple<int,int,int> ; using ld = long double ; #define cYes {cout<<"YES\n";return;} #define cNo {cout<<"NO\n";return;} #define bra(x) "[" << (x) << "] " #define ndl '\n' ; #define all(x) (x).begin() , (x).end() #define sz(x) (int)(x).size() #define nd second #define st first #define vvi vector<vector<int>> //#define DEBUG #ifdef DEBUG #define dbg(x) cerr << #x << " = " << x << endl #else #define dbg(x) #endif const int maxn = 1'000'007 ; string S1 , S2 , SW ; int N ; int t1[maxn] , t2[maxn] , tres[maxn] , needed[maxn] ; int dp[maxn][2] ; // ile 0 , ile 1 int main(){ ios_base::sync_with_stdio(false) ; cin.tie(0) ; cin >> S1 >> S2 >> SW ; N = sz(S2) ; for(int i=1;i<=N;i++){ t1[i] = S1[i-1] - '0' ; t2[i] = S2[i-1] - '0' ; tres[i] = SW[i-1] - '0' ; int flaga ; if( ( t1[i] + t2[i] ) % 10 == tres[i] ) flaga = 0 ; else if( ( t1[i] + t2[i] +1 + 10 ) % 10 == tres[i] ) flaga = 1 ; else flaga = -1 ; needed[i] = flaga ; } for(int i=N;i>=1;i--){ dbg(i) ; if(needed[i] == -1 ) continue ; if( needed[i] == 0 ){ int flag = ( t1[i] + t2[i] >= 10 ) ; dp[i][flag] = dp[i+1][0] + 1 ; } else if( needed[i] == 1 ){ dbg("jestem") ; int flag = ( t1[i] + t2[i] >= 9 ) ; dp[i][flag] = dp[i+1][1] ; } else { assert(false) ; } } for(int i=1;i<=N;i++){ dbg(i) ; dbg(dp[i][0]) ; dbg(dp[i][1]) ; } ll sum = 0 ; for(int i=1;i<=N;i++){ sum += dp[i][0] ; // if( tres[i-1] == 1 ){ // sum += dp[i][1] ; // } } cout << sum << ndl ; return 0 ; } |
English