#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
typedef long long ll;
using namespace __gnu_pbds;
using namespace std;
template <typename T>
using ordered_set = tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>;
vector<int> to_vec(string s) {
int n = s.size();
vector<int> v(n);
for(int i=0; i<n; ++i) v[i] = (s[i] - '0');
return v;
}
int main() {
ios_base::sync_with_stdio(0);
cin.tie(0); cout.tie(0);
string a, b, c;
cin >> a >> b >> c;
// reverse(a.begin(), a.end());
// reverse(b.begin(), b.end());
// reverse(c.begin(), c.end());
int n = a.size();
vector<int> A = to_vec(a);
vector<int> B = to_vec(b);
vector<int> C = to_vec(c);
vector<vector<ll>> dp(n+1, vector<ll>(2));
for(int i=1; i<=n; ++i) {
for(int c=0; c<2; ++c) {
if((A[i-1] + B[i-1] + c)%10 == C[i-1]) {
dp[i][c] = ((A[i-1] + B[i-1] + c) < 10) + dp[i-1][(A[i-1] + B[i-1] + c)/10];
}
// cout << "(" << i << ", " << c << "): " << dp[i][c] << "\n";
}
}
ll res = 0;
for(int i=1; i<=n; ++i) res += dp[i][0];
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 | #include <bits/stdc++.h> #include <ext/pb_ds/assoc_container.hpp> #include <ext/pb_ds/tree_policy.hpp> typedef long long ll; using namespace __gnu_pbds; using namespace std; template <typename T> using ordered_set = tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>; vector<int> to_vec(string s) { int n = s.size(); vector<int> v(n); for(int i=0; i<n; ++i) v[i] = (s[i] - '0'); return v; } int main() { ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); string a, b, c; cin >> a >> b >> c; // reverse(a.begin(), a.end()); // reverse(b.begin(), b.end()); // reverse(c.begin(), c.end()); int n = a.size(); vector<int> A = to_vec(a); vector<int> B = to_vec(b); vector<int> C = to_vec(c); vector<vector<ll>> dp(n+1, vector<ll>(2)); for(int i=1; i<=n; ++i) { for(int c=0; c<2; ++c) { if((A[i-1] + B[i-1] + c)%10 == C[i-1]) { dp[i][c] = ((A[i-1] + B[i-1] + c) < 10) + dp[i-1][(A[i-1] + B[i-1] + c)/10]; } // cout << "(" << i << ", " << c << "): " << dp[i][c] << "\n"; } } ll res = 0; for(int i=1; i<=n; ++i) res += dp[i][0]; cout << res << "\n"; return 0; } |
English