Problem:
G: Yet Another Expression Mining
You are given an integer $A$ and a string $S$ which consists of the following 10 characters: an addition operator '+', a digit '1', '2', ... ,
'9'.
Count the number of subsequences of $S$ which satisfy the following conditions.
The subsequence is not empty.
The first letter of the subsequence is not '+'.
The last letter of the subsequence is not '+'.
'+' does not appear consecutively in the subsequence.
Reading the subsequence as a mathematical expression, its evaluation result is $A$.
Note that a subsequence is a sequence that can be obtained from the given sequence by removing zero or more elements without
  changing the order of the remaining elements.
Two subsequences are considered different if the set of removed elements' indices are different.
Input
The input consists of a single test case in the format below.
$S$
$A$
The first line contains a single string $S$ ($1 \leq |S| \leq 36$). Each character of $S$ is either '+', or a digit between '1' and '9'. The second
line contains a single integer $A$ ($1 \leq A \leq 10^{18}$).
Output
Output the number of subsequences which satisfy the given conditions in a single line.
Examples
Input 1
1+2+3+4+5+6
18
Output 1
9
Input 2
+391+49++21+9934
43
Output 2
47
Input 3
1111111111111111
1111111111
Output 3
8008

Submitted solutions:

code_snippet: #include <bits/stdc++.h>
using namespace std;
using ll = long long;
#define rep(i, n) for (ll i = 0; i < n; ++i)
#define rrep(i, n) for (ll i = n-1; i >=0; i--)
#define loop(i,m,n)for(ll i=m;i<=n;i++)
#define vll vector<ll>
#define vvll vector<vll>

int main() {
    string s; cin >> s;
    ll a; cin >> a;

    ll l = (s.size() + 1) / 2;
    ll r = s.size() / 2;
    vector<map<ll, ll>> cnt(19);
    rep(bit, 1LL << l) {
        string i = "";
        ll res = 0;

        bool ok = true;
        rep(j, l) {
            if (bit & (1LL << j)) {
                if (s[j] == '+') {
                    if (i == "") {
                        ok = false;
                        break;
                    }

                    res += stoll(i);
                    i = "";
                }
                else {
                    i += s[j];
                }
            }
        }

        if (!ok) continue;

        // iが空で終わっていたら+で終わっている
        if (i == "") {
            loop(j, 1, cnt.size() - 1) {
                cnt[j][res]++;
            }
        }
        else {
            ll now = stoll(i);
            rep(j, cnt.size()) {
                cnt[j][res + now]++;
                if (LLONG_MAX / 10 <= now) break;
                now *= 10;
            }
        }
    }



    ll ans = 0;
    rep(bit, 1LL << r) {
        string i = "";
        ll res = 0;

        bool ok = true;
        for (ll j = r - 1; j >= 0; j--) {
            if (bit & (1LL << j)) {
                if (s[l + j] == '+') {
                    if (i == "") {
                        ok = false;
                        break;
                    }

                    reverse(i.begin(), i.end());
                    res += stoll(i);
                    i = "";
                }
                else {
                    i += s[l + j];
                }
            }
        }
        if (!ok) continue;

        // 最後iに残っていた桁数がずれる数
        ll mv = i.size();
        reverse(i.begin(), i.end());
        if (mv != 0) res += stoll(i);
        ans += cnt[mv][a - res];
    }

    cout << ans << endl;
    return 0;
}
accuracy: 1
time_ms: 390
memory_kb: 107648
final_rank: 9

code_snippet: #include <bits/stdc++.h>
using namespace std;
int main(){
    string S;
    cin >> S;
    long long A;
    cin >> A;
    int N = S.size();
    long long ans = 0;
    vector<map<long long, int>> mp(20);
    mp[0][0] = 1;
    for (int i = 1; i < (1 << (N - N / 2)); i++){
        string T;
        for (int j = 0; j < N - N / 2; j++){
            if (i >> j & 1) T += S[j + N / 2];
        }
        if (T.back() == '+') continue;
        bool ok = true;
        for (int j = 0; j < T.size() - 1; j++){
            if (T[j] == '+' and T[j + 1] == '+') ok = false;
        }
        if (!ok) continue;
        long long l = 0, r = 0, k = 1;
        reverse(T.begin(), T.end());
        for (char c : T){
            if (c == '+'){
                r += l;
                l = 0;
                k = 1;
            } else {
                l += k * (c - '0');
                k *= 10;
            }
        }
        int c = 0;
        r += l;
        while (l > 0){
            l /= 10;
            c++;
        }
        mp[c][r]++;
    }
    for (int i = 0; i < (1 << (N / 2)); i++){
        string T;
        for (int j = 0; j < N / 2; j++){
            if (i >> j & 1) T += S[j];
        }
        if (!T.empty() and T.front() == '+') continue;
        bool ok = true;
        for (int j = 0; j < (int) T.size() - 1; j++){
            if (T[j] == '+' and T[j + 1] == '+') ok = false;
        }
        if (!ok) continue;
        long long l = 0, r = 0, c = 0;
        for (char c : T){
            if (c == '+'){
                l += r;
                r = 0;
                c = 0;
            } else {
                r = r * 10 + (c - '0');
            }
        }
        for (int j = 0; j < 20; j++){
            if (j == 0 and r == 0) continue;
            unsigned long long sum = r;
            bool ok = true;
            for (int k = 0; k < j; k++){
                sum *= 10;
                if (sum + l > A){
                    ok = false;
                    break;
                }
            }
            if (!ok) continue;
            sum += l;
            ans += mp[j][A - sum];
        }
    }
    cout << ans << endl;
}
accuracy: 1
time_ms: 400
memory_kb: 98048
final_rank: 8

code_snippet: #line 1 "main.cpp"
#include <bits/stdc++.h>
using namespace std;
using ll = long long;

int main() {
    string s; cin >> s;
    ll a; cin >> a;
    if(ssize(s) == 1) {
        if(s[0] != '+' && s[0]-'0' == a) cout << "1\n";
        else cout << "0\n";
        return 0;
    }
    int l = ssize(s);
    int b = l/2;
    auto split = [&](const string& s) {
        int len = ssize(s);
        vector<int> plus;
        for(int i = 0; i < len; i++) {
            if(s[i] == '+') plus.push_back(i);
        }
        if(plus.empty()) return vector<string>{s};
        vector<string> ret;
        ret.push_back(s.substr(0, plus[0]));
        for(int i = 0; i < ssize(plus) - 1; i++) ret.push_back(s.substr(plus[i]+1, plus[i+1]-plus[i]-1));
        ret.push_back(s.substr(plus.back()+1));
        return ret;
    };
    map<pair<ll, ll>, ll> left_digit;
    map<ll, ll> left_plus;
    for(int i = 0; i < (1<<b); i++) {
        if(i == 0) continue;
        string buf;
        for(int j = 0; j < b; j++) {
            if(i >> j & 1) buf.push_back(s[j]);
        }
        vector<string> digit_v = split(buf);
        assert(!digit_v.empty());
        if(digit_v.back().empty()) {
            ll acc = 0;
            bool valid = true;
            for(int i = 0; i < ssize(digit_v)-1; i++) {
                if(digit_v[i].empty()) {
                    valid = false;
                    continue;
                }
                acc += stoll(digit_v[i]);
            }
            if(valid) left_plus[acc]++;
        } else {
            ll acc = 0;
            bool valid = true;
            for(int i = 0; i < ssize(digit_v)-1; i++) {
                if(digit_v[i].empty()) {
                    valid = false;
                    continue;
                }
                acc += stoll(digit_v[i]);
            }
            if(digit_v.back().empty()) valid = false;
            if(valid) left_digit[{acc, stoll(digit_v.back())}]++;
        }
    }
    map<pair<ll, int>, ll> right_digit;
    map<ll, ll> right_plus;
    for(int i = 0; i < (1<<(l-b)); i++) {
        if(i == 0) continue;
        string buf;
        for(int j = 0; j < l-b; j++) {
            if(i >> j & 1) buf.push_back(s[b+j]);
        }
        vector<string> digit_v = split(buf);
        assert(!digit_v.empty());
        if(digit_v.front().empty()) {
            ll acc = 0;
            bool valid = true;
            for(int i = 1; i < ssize(digit_v); i++) {
                if(digit_v[i].empty()) {
                    valid = false;
                    continue;
                }
                acc += stoll(digit_v[i]);
            }
            if(valid) right_plus[acc]++;
        } else {
            ll acc = 0;
            bool valid = true;
            for(int i = 0; i < ssize(digit_v); i++) {
                if(digit_v[i].empty()) {
                    valid = false;
                    continue;
                }
                acc += stoll(digit_v[i]);
            }
            if(valid) right_digit[{acc, ssize(digit_v.front())}]++;
        }
    }
    ll ans = 0;
    for(auto [k1, c1] : left_digit) {
        if(k1.first + k1.second == a) ans += c1; // right empty
        ans += c1 * right_plus[a - k1.first - k1.second];
        __int128_t dig = 1;
        for(int sl = 1;; sl++) {
            dig *= 10;
            __int128_t tar = (__int128_t)a - k1.first - k1.second*dig;
            if(tar < 0) break;
            ans += c1 * right_digit[{tar, sl}];
        }
    }
    for(auto [k1, c1] : left_plus) {
        for(int sl = 1; sl <= l-b; sl++) {
            ans += c1 * right_digit[{a-k1, sl}];
        }
    }
    // left empty
    for(int sl = 1; sl <= l-b; sl++) {
        ans += right_digit[{a, sl}];
    }
    cout << ans << '\n';
}
accuracy: 1
time_ms: 320
memory_kb: 107520
final_rank: 7

code_snippet: #line 1 "main.cpp"
#include <bits/stdc++.h>
using namespace std;
using ll = long long;

int main() {
    string s; cin >> s;
    ll a; cin >> a;
    if(ssize(s) == 1) {
        if(s[0] != '+' && s[0]-'0' == a) cout << "1\n";
        else cout << "0\n";
        return 0;
    }
    int l = ssize(s);
    int b = l/2;
    auto split = [&](const string& s) {
        int len = ssize(s);
        vector<int> plus;
        for(int i = 0; i < len; i++) {
            if(s[i] == '+') plus.push_back(i);
        }
        if(plus.empty()) return vector<string>{s};
        vector<string> ret;
        ret.push_back(s.substr(0, plus[0]));
        for(int i = 0; i < ssize(plus) - 1; i++) ret.push_back(s.substr(plus[i]+1, plus[i+1]-plus[i]-1));
        ret.push_back(s.substr(plus.back()+1));
        return ret;
    };
    map<pair<ll, int>, ll> left_digit;
    map<ll, ll> left_plus;
    for(int i = 0; i < (1<<b); i++) {
        if(i == 0) continue;
        string buf;
        for(int j = 0; j < b; j++) {
            if(i >> j & 1) buf.push_back(s[j]);
        }
        vector<string> digit_v = split(buf);
        assert(!digit_v.empty());
        if(digit_v.back().empty()) {
            ll acc = 0;
            bool valid = true;
            for(int i = 0; i < ssize(digit_v)-1; i++) {
                if(digit_v[i].empty()) {
                    valid = false;
                    continue;
                }
                acc += stoll(digit_v[i]);
            }
            if(valid) left_plus[acc]++;
        } else {
            ll acc = 0;
            bool valid = true;
            for(int i = 0; i < ssize(digit_v)-1; i++) {
                if(digit_v[i].empty()) {
                    valid = false;
                    continue;
                }
                acc += stoll(digit_v[i]);
            }
            if(digit_v.back().empty()) valid = false;
            if(valid) left_digit[{acc, stoll(digit_v.back())}]++;
        }
    }
    map<pair<ll, int>, ll> right_digit;
    map<ll, ll> right_plus;
    for(int i = 0; i < (1<<(l-b)); i++) {
        if(i == 0) continue;
        string buf;
        for(int j = 0; j < l-b; j++) {
            if(i >> j & 1) buf.push_back(s[b+j]);
        }
        vector<string> digit_v = split(buf);
        assert(!digit_v.empty());
        if(digit_v.front().empty()) {
            ll acc = 0;
            bool valid = true;
            for(int i = 1; i < ssize(digit_v); i++) {
                if(digit_v[i].empty()) {
                    valid = false;
                    continue;
                }
                acc += stoll(digit_v[i]);
            }
            if(valid) right_plus[acc]++;
        } else {
            ll acc = 0;
            bool valid = true;
            for(int i = 0; i < ssize(digit_v); i++) {
                if(digit_v[i].empty()) {
                    valid = false;
                    continue;
                }
                acc += stoll(digit_v[i]);
            }
            if(valid) right_digit[{acc, ssize(digit_v.front())}]++;
        }
    }
    ll ans = 0;
    for(auto [k1, c1] : left_digit) {
        if(k1.first + k1.second == a) ans += c1; // right empty
        ans += c1 * right_plus[a - k1.first - k1.second];
        __int128_t dig = 1;
        for(int sl = 1;; sl++) {
            dig *= 10;
            __int128_t tar = (__int128_t)a - k1.first - k1.second*dig;
            if(tar < 0) break;
            ans += c1 * right_digit[{tar, sl}];
        }
    }
    for(auto [k1, c1] : left_plus) {
        for(int sl = 1; sl <= l-b; sl++) {
            ans += c1 * right_digit[{a-k1, sl}];
        }
    }
    // left empty
    for(int sl = 1; sl <= l-b; sl++) {
        ans += right_digit[{a, sl}];
    }
    cout << ans << '\n';
}
accuracy: 0.6
time_ms: 380
memory_kb: 94848
final_rank: 17

code_snippet: #include <bits/stdc++.h>
using namespace std;
#define all(v) (v).begin(),(v).end()
#define pb(a) push_back(a)
#define rep(i, n) for(int i=0;i<n;i++)
#define foa(e, v) for(auto&& e : v)
using ll = long long;
const ll MOD7 = 1000000007, MOD998 = 998244353, INF = (1LL << 60);
#define dout(a) cout<<fixed<<setprecision(10)<<a<<endl;

int main() {
    cin.tie(0);
    ios::sync_with_stdio(false);
    string s;
    cin >> s;
    ll x; cin >> x;
    int n = s.size();
    int m = max(n / 2, min(n, 18));
    int mask = 1 << (n - m);
    vector<map<ll, ll>> mp(20);
    
    ll ans = 0;
    rep(bit, mask) {
        if(bit == 0) continue;
        string t = "";
        for(int i = m; i < n; i ++) if(bit >> (i - m) & 1) t += s[i];
        int len = t.size();
        
        bool ng = 0;
        rep(i, len - 1) if(t[i] == '+' and t[i + 1] == '+') ng = 1;
        if(ng) continue;
        if(t.back() == '+') continue;
        ll sum = 0;
        bool f = (t[0] == '+');
        string u = "";
        if(f) {
            for(int i = 1; i < len; i ++) {
                if(t[i] == '+') {
                    sum += stoll(u);
                    u = "";
                } else {
                    u += t[i];
                }
            }
            sum += stoll(u);
            u = "";
        } else {
            int id = n;
            rep(i, len) if(t[i] == '+') {
                id = i;
                break;
            }
            if(id != n) {
                for(int i = id + 1; i < len; i ++) {
                    if(t[i] == '+') {
                        sum += stoll(u);
                        u = "";
                    } else {
                        u += t[i];
                    }
                }
                sum += stoll(u);
                u = "";
                rep(i, id) u += t[i];
                sum += stoll(u);
            } else {
                rep(i, len) u += t[i];
                sum += stoll(u);
            }
            
            if(sum == x) {
                ans ++;
            }
        }
        
        mp[(ll)u.size()][sum] ++;
    }
    mask = 1LL << m;

    ll inf = (INF / 10) * 8;
    rep(bit, mask) {
        if(bit == 0) continue;
        string t = "";
        rep(i, m) if(bit >> i & 1) t += s[i];
        int len = t.size();
        
        bool ng = 0;
        rep(i, len - 1) if(t[i] == '+' and t[i + 1] == '+') ng = 1;
        if(ng) continue;
        if(t[0] == '+') continue;
        ll sum = 0;
        string u = "";
        rep(i, len) {
            if(t[i] == '+') {
                sum += stoll(u);
                u = "";
            } else {
                u += t[i];
            }
        }
        if(u == "") {
            for(int i = 1; i < 19; i ++) ans += mp[i][x - sum];
            continue;
        }
        ll d = 1;
        ll num = stoll(u);
        if(sum + num == x) {
            ans ++;
        }
        
        for(ll i = 0; i < 19; i ++) {
            ans += mp[i][x - (num * d + sum)];
            if(num * d > inf) break;
            d *= 10;
        }
    }
    cout << ans << endl;
    return 0;
}
accuracy: 1
time_ms: 500
memory_kb: 107540
final_rank: 11

code_snippet: #include <bits/stdc++.h>
using namespace std;
#define all(v) (v).begin(),(v).end()
#define pb(a) push_back(a)
#define rep(i, n) for(int i=0;i<n;i++)
#define foa(e, v) for(auto&& e : v)
using ll = long long;
const ll MOD7 = 1000000007, MOD998 = 998244353, INF = (1LL << 60);
#define dout(a) cout<<fixed<<setprecision(10)<<a<<endl;

int main() {
    cin.tie(0);
    ios::sync_with_stdio(false);
    string s;
    cin >> s;
    ll x; cin >> x;
    int n = s.size();
    int m = max(n / 2, min(n, 18));
    int mask = 1 << (n - m);
    vector<map<ll, ll>> mp(20);
    
    ll ans = 0;
    rep(bit, mask) {
        if(bit == 0) continue;
        string t = "";
        for(int i = m; i < n; i ++) if(bit >> (i - m) & 1) t += s[i];
        int len = t.size();
        
        bool ng = 0;
        rep(i, len - 1) if(t[i] == '+' and t[i + 1] == '+') ng = 1;
        if(ng) continue;
        if(t.back() == '+') continue;
        ll sum = 0;
        bool f = (t[0] == '+');
        string u = "";
        if(f) {
            for(int i = 1; i < len; i ++) {
                if(t[i] == '+') {
                    sum += stoll(u);
                    u = "";
                } else {
                    u += t[i];
                }
            }
            sum += stoll(u);
            u = "";
        } else {
            int id = n;
            rep(i, len) if(t[i] == '+') {
                id = i;
                break;
            }
            if(id != n) {
                for(int i = id + 1; i < len; i ++) {
                    if(t[i] == '+') {
                        sum += stoll(u);
                        u = "";
                    } else {
                        u += t[i];
                    }
                }
                sum += stoll(u);
                u = "";
                rep(i, id) u += t[i];
                sum += stoll(u);
            } else {
                rep(i, len) u += t[i];
                sum += stoll(u);
            }
            
            if(sum == x) {
                ans ++;
            }
        }
        
        mp[(ll)u.size()][sum] ++;
    }
    mask = 1LL << m;
    rep(bit, mask) {
        if(bit == 0) continue;
        string t = "";
        rep(i, m) if(bit >> i & 1) t += s[i];
        int len = t.size();
        
        bool ng = 0;
        rep(i, len - 1) if(t[i] == '+' and t[i + 1] == '+') ng = 1;
        if(ng) continue;
        if(t[0] == '+') continue;
        ll sum = 0;
        string u = "";
        rep(i, len) {
            if(t[i] == '+') {
                sum += stoll(u);
                u = "";
            } else {
                u += t[i];
            }
        }
        if(u == "") {
            for(int i = 1; i < 20; i ++) ans += mp[i][x - sum];
            continue;
        }
        ll d = 1;
        ll num = stoll(u);
        if(sum + num == x) {
            ans ++;
        }
        
        for(ll i = 0; i < 19; i ++) {
            ans += mp[i][x - (num * d + sum)];
            if(i != 18) d *= 10;
        }
    }
    cout << ans << endl;
    return 0;
}
accuracy: 0.825
time_ms: 1420
memory_kb: 188212
final_rank: 15

code_snippet: #include <bits/stdc++.h>
using namespace std;
#define all(v) (v).begin(),(v).end()
#define pb(a) push_back(a)
#define rep(i, n) for(int i=0;i<n;i++)
#define foa(e, v) for(auto&& e : v)
using ll = long long;
const ll MOD7 = 1000000007, MOD998 = 998244353, INF = (1LL << 60);
#define dout(a) cout<<fixed<<setprecision(10)<<a<<endl;

int main() {
    cin.tie(0);
    ios::sync_with_stdio(false);
    string s;
    cin >> s;
    ll x; cin >> x;
    int n = s.size();
    int m = n / 2;
    int mask = 1 << (n - m);
    vector<map<ll, ll>> mp(20);
    
    ll ans = 0;
    rep(bit, mask) {
        if(bit == 0) continue;
        string t = "";
        for(int i = m; i < n; i ++) if(bit >> (i - m) & 1) t += s[i];
        int len = t.size();
        
        bool ng = 0;
        rep(i, len - 1) if(t[i] == '+' and t[i + 1] == '+') ng = 1;
        if(ng) continue;
        if(t.back() == '+') continue;
        ll sum = 0;
        bool f = (t[0] == '+');
        string u = "";
        if(f) {
            for(int i = 1; i < len; i ++) {
                if(t[i] == '+') {
                    sum += stoll(u);
                    u = "";
                } else {
                    u += t[i];
                }
            }
            sum += stoll(u);
            u = "";
        } else {
            int id = n;
            rep(i, len) if(t[i] == '+') {
                id = i;
                break;
            }
            if(id != n) {
                for(int i = id + 1; i < len; i ++) {
                    if(t[i] == '+') {
                        sum += stoll(u);
                        u = "";
                    } else {
                        u += t[i];
                    }
                }
                sum += stoll(u);
                u = "";
                rep(i, id) u += t[i];
                sum += stoll(u);
            } else {
                rep(i, len) u += t[i];
                sum += stoll(u);
            }
            
            if(sum == x) {
                ans ++;
            }
        }
        
        mp[(ll)u.size()][sum] ++;
    }
    mask = 1LL << m;
    rep(bit, mask) {
        if(bit == 0) continue;
        string t = "";
        rep(i, m) if(bit >> i & 1) t += s[i];\
        int len = t.size();
        
        bool ng = 0;
        rep(i, len - 1) if(t[i] == '+' and t[i + 1] == '+') ng = 1;
        if(ng) continue;
        if(t[0] == '+') continue;
        ll sum = 0;
        string u = "";
        rep(i, len) {
            if(t[i] == '+') {
                sum += stoll(u);
                u = "";
            } else {
                u += t[i];
            }
        }
        if(u == "") {
            for(int i = 1; i < 20; i ++) ans += mp[i][x - sum];
            continue;
        }
        ll d = 1;
        ll num = stoll(u);
        if(sum + num == x) {
            ans ++;
        }
        
        for(ll i = 0; i < 19; i ++) {
            ans += mp[i][x - (num * d + sum)];
            if(i != 18) d *= 10;
        }
    }
    cout << ans << endl;
    return 0;
}
accuracy: 0.825
time_ms: 1480
memory_kb: 188212
final_rank: 16

code_snippet: #include <bits/stdc++.h>

using namespace std;

#define rep(i, a, b) for (int i = (int)(a); i < (int)(b); i++)
#define rrep(i, a, b) for (int i = (int)(b)-1; i >= (int)(a); i--)
#define ALL(v) (v).begin(), (v).end()
#define UNIQUE(v) sort(ALL(v)), (v).erase(unique(ALL(v)), (v).end())
#define SZ(v) (int)v.size()
#define MIN(v) *min_element(ALL(v))
#define MAX(v) *max_element(ALL(v))
#define LB(v, x) int(lower_bound(ALL(v), (x)) - (v).begin())
#define UB(v, x) int(upper_bound(ALL(v), (x)) - (v).begin())

using uint = unsigned int;
using ll = long long int;
using ull = unsigned long long;
using i128 = __int128_t;
using u128 = __uint128_t;
const int inf = 0x3fffffff;
const ll INF = 0x1fffffffffffffff;

template <typename T> inline bool chmax(T &a, T b) {
    if (a < b) {
        a = b;
        return 1;
    }
    return 0;
}
template <typename T> inline bool chmin(T &a, T b) {
    if (a > b) {
        a = b;
        return 1;
    }
    return 0;
}
template <typename T, typename U> T ceil(T x, U y) {
    assert(y != 0);
    if (y < 0)
        x = -x, y = -y;
    return (x > 0 ? (x + y - 1) / y : x / y);
}
template <typename T, typename U> T floor(T x, U y) {
    assert(y != 0);
    if (y < 0)
        x = -x, y = -y;
    return (x > 0 ? x / y : (x - y + 1) / y);
}
template <typename T> int popcnt(T x) {
    return __builtin_popcountll(x);
}
template <typename T> int topbit(T x) {
    return (x == 0 ? -1 : 63 - __builtin_clzll(x));
}
template <typename T> int lowbit(T x) {
    return (x == 0 ? -1 : __builtin_ctzll(x));
}

template <class T, class U>
ostream &operator<<(ostream &os, const pair<T, U> &p) {
    os << "P(" << p.first << ", " << p.second << ")";
    return os;
}
template <typename T> ostream &operator<<(ostream &os, const vector<T> &vec) {
    os << "{";
    for (int i = 0; i < vec.size(); i++) {
        os << vec[i] << (i + 1 == vec.size() ? "" : ", ");
    }
    os << "}";
    return os;
}
template <typename T, typename U>
ostream &operator<<(ostream &os, const map<T, U> &map_var) {
    os << "{";
    for (auto itr = map_var.begin(); itr != map_var.end(); itr++) {
        os << "(" << itr->first << ", " << itr->second << ")";
        itr++;
        if (itr != map_var.end())
            os << ", ";
        itr--;
    }
    os << "}";
    return os;
}
template <typename T> ostream &operator<<(ostream &os, const set<T> &set_var) {
    os << "{";
    for (auto itr = set_var.begin(); itr != set_var.end(); itr++) {
        os << *itr;
        ++itr;
        if (itr != set_var.end())
            os << ", ";
        itr--;
    }
    os << "}";
    return os;
}
#ifdef LOCAL
#define show(...) _show(0, #__VA_ARGS__, __VA_ARGS__)
#else
#define show(...) true
#endif
template <typename T> void _show(int i, T name) {
    cerr << '\n';
}
template <typename T1, typename T2, typename... T3>
void _show(int i, const T1 &a, const T2 &b, const T3 &...c) {
    for (; a[i] != ',' && a[i] != '\0'; i++)
        cerr << a[i];
    cerr << ":" << b << " ";
    _show(i + 1, a, c...);
}

/**
 * @brief template
 */

int main() {
    cin.tie(0);
    ios_base::sync_with_stdio(false);
    string S;
    ll A;
    cin >> S >> A;
    int L = S.size();
    ll ANS = 0;
    if (L <= 18) {
        rep(i,1,1<<L) {
            string T = "";
            rep(j,0,L) {
                if (i&(1<<j)) T += S[j];
            }
            int K = T.size();
            if (T[0] == '+') continue;
            if (T.back() == '+') continue;
            bool check = true;
            rep(j,0,K-1) if (T[j] == '+' && T[j+1] == '+') check = false;
            if (!check) continue;
            ll X = 0, Y = 0;
            rep(j,0,K) {
                if (isdigit(T[j])) Y *= 10, Y += (T[j] - '0');
                else X += Y, Y = 0;
            }
            if (X + Y == A) ANS++;
        }
        cout << ANS << endl;
        return 0;
    }
    int L1 = L/2;
    int L2 = L-L1;
    if (L1 <= 18) {
        rep(i,1,1<<L1) {
            string T = "";
            rep(j,0,L1) {
                if (i&(1<<j)) T += S[j];
            }
            int K = T.size();
            if (T[0] == '+') continue;
            if (T.back() == '+') continue;
            bool check = true;
            rep(j,0,K-1) if (T[j] == '+' && T[j+1] == '+') check = false;
            if (!check) continue;
            ll X = 0, Y = 0;
            rep(j,0,K) {
                if (isdigit(T[j])) Y *= 10, Y += (T[j] - '0');
                else X += Y, Y = 0;
            }
            if (X + Y == A) ANS++;
        }
    }
    if (L2 <= 18) {
        rep(i,1,1<<L2) {
            string T = "";
            rep(j,0,L2) {
                if (i&(1<<j)) T += S[L1+j];
            }
            int K = T.size();
            if (T[0] == '+') continue;
            if (T.back() == '+') continue;
            bool check = true;
            rep(j,0,K-1) if (T[j] == '+' && T[j+1] == '+') check = false;
            if (!check) continue;
            ll X = 0, Y = 0;
            rep(j,0,K) {
                if (isdigit(T[j])) Y *= 10, Y += (T[j] - '0');
                else X += Y, Y = 0;
            }
            if (X + Y == A) ANS++;
        }
    }
    vector<map<ll,ll>> mp(L2+1);
    rep(i,1,1<<L2) {
        string T = "";
        rep(j,0,L2) {
            if (i&(1<<j)) T += S[L1+j];
        }
        int K = T.size();
        if (T.back() == '+') continue;
        bool check = true;
        rep(j,0,K-1) if (T[j] == '+' && T[j+1] == '+') check = false;
        if (!check) continue;
        ll X = 0, Y = 0;
        rep(j,0,K) {
            if (isdigit(T[j])) Y *= 10, Y += (T[j] - '0');
            else X += Y, Y = 0;
        }
        int Z = 0;
        rep(j,0,K) {
            if (T[j] == '+') break;
            Z++;
        }
        mp[Z][X+Y]++;
    }
    rep(i,1,1<<L1) {
        string T = "";
        rep(j,0,L1) {
            if (i&(1<<j)) T += S[j];
        }
        int K = T.size();
        if (T[0] == '+') continue;
        bool check = true;
        rep(j,0,K-1) if (T[j] == '+' && T[j+1] == '+') check = false;
        if (!check) continue;
        ll X = 0, Y = 0;
        rep(j,0,K) {
            if (isdigit(T[j])) Y *= 10, Y += (T[j] - '0');
            else X += Y, Y = 0;
        }
        if (A-X < 0) continue;
        if (Y == 0) {
            rep(j,1,L2+1) {
                if (mp[j].count(A-X)) ANS += mp[j][A-X];
            }
        }
        else {
            rep(j,0,L2+1) {
                if (X+Y > A) break;
                if (mp[j].count(A-X-Y)) ANS += mp[j][A-X-Y];
                if (Y > 1e17+10) break;
                Y *= 10;
            }
        }
    }
    cout << ANS << endl;
}
accuracy: 1
time_ms: 100
memory_kb: 12488
final_rank: 2

code_snippet: #include <bits/stdc++.h>
using namespace std;

using ll = long long;

const ll INF = 1.1e18;
array<ll, 4> ERROR{-1, -1, -1, -1};
array<ll, 4> ZERO{0, 0, 0, 0};

array<ll, 4> get1(string s) {
  if (s.length() == 0) {
    return {0, 0, 0, 0};
  }
  
  ll len = s.length();
  bool bp = false, ep = false;
  if (s[0] == '+') bp = true;
  if (s.back() == '+') ep = true;
  if (bp) return ERROR;
  
  ll sum = 0, last = 0;
  for (int i = 0; i < len; ++i) {
    if (i == 0 && bp) continue;
    if (sum + last > INF || sum + last < 0) return ERROR;
    
    if (s[i] == '+') {
      if (last == 0) return ERROR;
      else {sum += last, last = 0;}
    }
    else {
      ll n = s[i] - '0';
      last = last * 10 + n;
    }
  }
  
  return {bp, ep, sum, last};
}

array<ll, 4> get2(string s) {
  if (s.length() == 0) {
    return {0, 0, 0, 0};
  }
  
  bool bp = false, ep = false;
  if (s[0] == '+') bp = true;
  if (s.back() == '+') ep = true;
  if (ep) return ERROR;
  
  s += '+';
  ll len = s.length();
  
  ll sum = 0, last = 0, first = 0;
  for (int i = 0; i < len; ++i) {
    if (i == 0 && bp) continue;
    if (sum + last > INF || sum + last < 0) return ERROR;
    
    if (s[i] == '+') {
      if (last == 0) return ERROR;
      else {
        if (first == 0) first = last;
        sum += last, last = 0;
      }
    }
    else {
      ll n = s[i] - '0';
      last = last * 10 + n;
    }
  }
  if (bp) first = 0;
  
  ll up = 1;
  ll upc = 0;
  while (up <= first) {
    up *= 10;
    upc++;
  }
  
  return {bp, ep, upc, sum + last};
}

ll p10[19];

void init() {
  p10[0] = 1;
  for (int i = 1; i < 19; ++i) {
    p10[i] = p10[i-1] * 10;
  }
  return;
}

int main() {
  init();
  string S; cin >> S;
  ll A; cin >> A;
  
  ll N = S.length();
  ll L = N / 2, R = N - L;
  string SL = S.substr(0, L);
  string SR = S.substr(L);
  
  // cout << SL << " " << SR << endl;
  
  map< array<ll, 4>, ll> M1;
  vector< map<array<ll, 4>, ll> > M2(20);
  {
    for (int bit = 0; bit < (1<<L); ++bit) {
      string tmp = "";
      for (int i = 0; i < L; ++i) {
        if (bit & (1<<i)) tmp += SL[i];
      }
      array<ll, 4> ret = get1(tmp);
      if (ret != ERROR) M1[ret] += 1;
      
      // {
      //   auto [bp, ep, sum, last] = ret;
      //   cout << tmp << " " << bp << " " << ep << " " << sum << " " << last << endl;
      // }
    }
    
    for (int bit = 0; bit < (1<<R); ++bit) {
      string tmp = "";
      for (int i = 0; i < R; ++i) {
        if (bit & (1<<i)) tmp += SR[i];
      }
      array<ll, 4> ret = get2(tmp);
      if (ret != ERROR) {
        auto [bp, ep, up, sum] = ret;
        M2[up][ret] += 1;
      }
      
      // {
      //   auto [bp, ep, sum, last] = ret;
      //   cout << tmp << " " << bp << " " << ep << " " << sum << " " << last << endl;
      // }
    }
  }
  
  // M1
  // cout << "M1" << endl;
  // for (auto [key, v1] : M1) {
  //   auto [bp, ep, sum, last] = key;
  //   cout << bp << " " << ep << " " << sum << " " << last << endl;
  // }
  // cout << endl << "M2" << endl;
  // for (int up = 0; up < 20; ++up) {
  //   cout << up << endl;
  //   for (auto [key, v1] : M2[up]) {
  //     auto [bp, ep, sum, last] = key;
  //     cout << bp << " " << ep << " " << sum << " " << last << endl;
  //   }
  // }

  
  ll res = 0;
  for (auto [key, v1] : M1) {
    auto [bp, ep, sum, last] = key;
    ll v2 = 0;
    
    if (!ep) {
      for (int up = 0; up < 19; ++up) {
        ll tot = sum + last * p10[up];
        if (tot > A || tot < 0) break;
        ll diff = A - tot;
        v2 += M2[up][{0, 0, up, diff}];
      }
      for (int up = 0; up < 1; ++up) {
        if (key != ZERO) {
          ll tot = sum + last;
          if (tot > A || tot < 0) break;
          ll diff = A - tot;
          v2 += M2[0][{1, 0, 0, diff}];
        }
      }
      
    }
    else {
      for (int up = 0; up < 19; ++up) {
        ll tot = sum + last;
        if (tot > A || tot < 0) break;
        ll diff = A - tot;
        if (diff == 0 && up == 0) continue;
        v2 += M2[up][{0, 0, up, diff}];
      }
    }
    
    res += v1 * v2;
  }
  cout << res << endl;
  
  
  
  return 0;
}
accuracy: 1
time_ms: 520
memory_kb: 144896
final_rank: 13

code_snippet: #include<bits/stdc++.h>
using namespace std;
using ll = long long;
using vll = vector<ll>;
using vvll = vector<vll>;
#define rep(i,n) for(ll i=0;i<ll(n);i++)

const ll mod = 1e9 + 7;

ll logz(ll n) {
    ll res = 0;
    if (n == 0)n = 1;
    while (n > 0) {
        res++;
        n /= 10;
    }
    return res;
}

int main() {
    string S;
    cin >> S;
    ll N = S.size();
    ll A;
    cin >> A;
    if (N == 1) {
        if (S == "+") {
            cout << 0 << endl;
        }
        else if (S[0] - '0' == A) {
            cout << 1 << endl;
        }
        else cout << 0 << endl;
        return 0;
    }
    ll H = N / 2;
    ll B = N - H;
    map<pair<ll, ll>, ll> numend, plusend;
    map<pair<ll, ll>, ll> numbegin, plusbegin;
    ll an = 0;
    rep(bit, 1 << H) {
        ll res = 0;
        ll plus = 0;
        bool lastplus = 0;
        if (bit == 0)continue;
        bool beg = 1, OK = 1;
        rep(h, H) {
            if (bit & (1 << h)) {
                if (S[h] == '+') {
                    if (lastplus) {
                        OK = 0;
                    }
                    if (beg) {
                        OK = 0;
                        break;
                    }
                    res += plus;
                    if (res > 1e18)OK = 0;
                    plus = 0;
                    lastplus = 1;
                }
                else {
                    if (plus > 2e17)OK = 0;
                    plus = 10 * plus + S[h] - '0';
                    lastplus = 0;
                }
                beg = 0;
            }
        }
        if (OK) {
            if (lastplus) {
                plusend[{res, plus}]++;
            }
            else {
                if (res + plus == A) {
                    an++;
                }
                numend[{res, plus}]++;
            }
        }
    }
    rep(bit, 1 << B) {
        ll res = 0;
        ll plus = 0;
        bool lastplus = 0;
        bool begplus = 0;
        ll begnum = -1;
        bool OK = 1;
        if (bit == 0)continue;
        rep(b, B) {
            if (bit & (1 << (b))) {
                if (S[b + H] == '+') {
                    if (lastplus) {
                        OK = 0;
                    }
                    if (begnum == -1) {
                        begnum = plus;
                    }
                    if (res == 0 && plus == 0) {
                        begplus = 1;
                    }
                    res += plus;
                    if (res > 1e18)OK = 0;
                    plus = 0;
                    lastplus = 1;
                }
                else {
                    if (plus > 2e17)OK = 0;
                    plus = 10 * plus + S[b + H] - '0';
                    lastplus = 0;
                }
            }
        }
        if(begnum==-1)begnum=plus;
        res += plus;
        if (!lastplus && OK) {
            if (begplus) {
                plusbegin[{res, logz(plus)}]++;
            }
            else {
                if (res == A)an++;
                numbegin[{res, logz(begnum)}]++;
            }
        }
    }

    for (auto n : plusend) {
        ll a = n.first.first;
        if (A - a < 0)continue;
        rep(i, 18) {
            if (numbegin.count({ A - a,i + 1 })) {
                an += n.second * numbegin[{A - a, i + 1}];
            }
        }
    }
    vll T(19, 1);
    rep(t, 18) {
        T[t + 1] = T[t] * 10;
    }
    for (auto n : numend) {
        ll a = n.first.first;
        ll b = n.first.second;
        if (A - a < 0)continue;
        rep(i, 18) {
            if (plusbegin.count({ A - (a + b),i + 1 })) {
                an += n.second * plusbegin[{A - (a + b), i + 1}];
            }
            if (T[i + 1] > (A - a + b - 1) / b)continue;
            ll num = a + b * T[i + 1];
            if(num>A)continue;
            if (numbegin.count({ A - num,i + 1 })) {
                // cout<<a<<" "<<b<<" "<<num<<" "<<n.second<<" "<<numbegin[{A - num, i + 1}]<<endl;
                an += n.second * numbegin[{A - num, i + 1}];
            }
        }

    }


    cout << an << endl;


}
accuracy: 1
time_ms: 110
memory_kb: 20256
final_rank: 4

code_snippet: #include<bits/stdc++.h>
using namespace std;
using ll = long long;
using vll = vector<ll>;
using vvll = vector<vll>;
#define rep(i,n) for(ll i=0;i<ll(n);i++)

const ll mod = 1e9 + 7;

ll logz(ll n) {
    ll res = 0;
    if (n == 0)n = 1;
    while (n > 0) {
        res++;
        n /= 10;
    }
    return res;
}

int main() {
    string S;
    cin >> S;
    ll N = S.size();
    ll A;
    cin >> A;
    if (N == 1) {
        if (S == "+") {
            cout << 0 << endl;
        }
        else if (S[0] - '0' == A) {
            cout << 1 << endl;
        }
        else cout << 0 << endl;
        return 0;
    }
    ll H = N / 2;
    ll B = N - H;
    map<pair<ll, ll>, ll> numend, plusend;
    map<pair<ll, ll>, ll> numbegin, plusbegin;
    ll an = 0;
    rep(bit, 1 << H) {
        ll res = 0;
        ll plus = 0;
        bool lastplus = 0;
        if (bit == 0)continue;
        bool beg = 1, OK = 1;
        rep(h, H) {
            if (bit & (1 << h)) {
                if (S[h] == '+') {
                    if (lastplus) {
                        OK = 0;
                    }
                    if (beg) {
                        OK = 0;
                        break;
                    }
                    res += plus;
                    if (res > 1e18)OK = 0;
                    plus = 0;
                    lastplus = 1;
                }
                else {
                    if (plus > 2e17)OK = 0;
                    plus = 10 * plus + S[h] - '0';
                    lastplus = 0;
                }
                beg = 0;
            }
        }
        if (OK) {
            if (lastplus) {
                plusend[{res, plus}]++;
            }
            else {
                if (res + plus == A) {
                    an++;
                }
                numend[{res, plus}]++;
            }
        }
    }
    rep(bit, 1 << B) {
        ll res = 0;
        ll plus = 0;
        bool lastplus = 0;
        bool begplus = 0;
        ll begnum = -1;
        bool OK = 1;
        if (bit == 0)continue;
        rep(b, B) {
            if (bit & (1 << (b))) {
                if (S[b + H] == '+') {
                    if (lastplus) {
                        OK = 0;
                    }
                    if (begnum == -1) {
                        begnum = plus;
                    }
                    if (res == 0 && plus == 0) {
                        begplus = 1;
                    }
                    res += plus;
                    if (res > 1e18)OK = 0;
                    plus = 0;
                    lastplus = 1;
                }
                else {
                    if (plus > 2e17)OK = 0;
                    plus = 10 * plus + S[b + H] - '0';
                    lastplus = 0;
                }
            }
        }
        res += plus;
        if (!lastplus && OK) {
            if (begplus) {

                plusbegin[{res, logz(plus)}]++;
            }
            else {
                
                if (res == A)an++;
                numbegin[{res, logz(plus)}]++;
            }
        }
    }

    for (auto n : plusend) {
        ll a = n.first.first;
        if (A - a < 0)continue;
        rep(i, 18) {
            if (numbegin.count({ A - a,i + 1 })) {
                an += n.second * numbegin[{A - a, i + 1}];
            }
        }
    }
    vll T(19, 1);
    rep(t, 18) {
        T[t + 1] = T[t] * 10;
    }
    for (auto n : numend) {
        ll a = n.first.first;
        ll b = n.first.second;
        if (A - a < 0)continue;
        rep(i, 18) {
            if (plusbegin.count({ A - (a + b),i + 1 })) {
                an += n.second * plusbegin[{A - (a + b), i + 1}];
            }
            if (T[i + 1] > (A - a + b - 1) / b)continue;
            ll num = a + b * T[i + 1];
            if (numbegin.count({ A - num,i + 1 })) {
                an += n.second * numbegin[{A - num, i + 1}];
            }
        }

    }


    cout << an << endl;


}
accuracy: 0.325
time_ms: 20
memory_kb: 4104
final_rank: 18

code_snippet: #include <bits/stdc++.h>

using namespace std;

#define rep(i, l, r) for (int i = (l); i < r; ++i)
#define rrep(i, l, r) for (int i = (r - 1); i >= l; --i)
#define all(c) begin(c), end(c)

#ifdef LOCAL
#define debug(...) debug_impl(#__VA_ARGS__, __VA_ARGS__)
template <class H, class... Ts> void debug_impl(string s, H&& h, Ts &&... ts) {
  cerr << '(' << s << "): (" << forward<H>(h);
  ((cerr << ", " << forward<Ts>(ts)), ..., (cerr << ")\n"));
}
#else
#define debug(...) void(0)
#endif

template <class T> bool chmax(T& a, const T& b) {
  return b > a ? (a = b, true) : false;
}
template <class T> bool chmin(T& a, const T& b) {
  return b < a ? (a = b, true) : false;
}

template <class T> istream& operator>>(istream& in, vector<T>& v) {
  for (auto& e : v) in >> e;
  return in;
}
template <class ... Args> void read(Args &... args) {
  (cin >> ... >> args);
}
template <class T> ostream& operator<<(ostream& out, const vector<T>& v) {
  int n = v.size();
  rep(i, 0, n) {
    out << v[i];
    if (i + 1 != n) out << ' ';
  }
  return out;
}
template <class H, class ... Ts> void print(H&& h, Ts &&...ts) {
  cout << h, ((cout << " " << forward<Ts>(ts)), ..., (cout << "\n"));
}

struct io_setup_ {
  io_setup_() {
    ios::sync_with_stdio(false), cin.tie(nullptr);
    cout << fixed << setprecision(10);
    cerr << fixed << setprecision(10);
  }
} io_setup{};

int naive(string s, long long a) {
  int ans = 0;

  int n = s.size();

  rep(st, 1, 1 << n) {
    string ss;

    bool cons_pos = false;
    long long val = 0;
    vector<long long> vs;
    rep(i, 0, n) if ((st >> i) & 1) {
      ss += s[i];
      if (s[i] == '+') {
        if (val == 0) {
          cons_pos = true;
          break;
        }
        vs.push_back(val);
        val = 0;
      } else {
        val = val * 10 + (s[i] - '0');
      }
    }
    if (cons_pos or val == 0) {
      continue;
    }
    vs.push_back(val);

    long long sum = accumulate(all(vs), 0LL);

    if (sum == a) {
      ++ans;
    }
  }
  return ans;
}

long long solve(string s, __int128_t a) {
  long long ans = 0;

  const int n = s.size();
  const int l = n / 2, r = n - l;

  string sl = s.substr(0, l), sr = s.substr(l);

  vector<map<__int128_t, int>> resr(20);

  rep(st, 1, 1 << r) {
    string ss;

    bool cons_pos = false;
    __int128_t val = 0;
    vector<__int128_t> vs;
    rep(i, 0, r) if ((st >> i) & 1) {
      ss += sr[i];
      if (sr[i] == '+') {
        if (val == 0 and vs.size()) {
          assert(ss.end()[-2] == '+');
          cons_pos = true;
          break;
        }
        vs.push_back(val);
        val = 0;
      } else {
        val = val * 10 + (sr[i] - '0');
      }
    }
    if (cons_pos or val == 0) {
      continue;
    }
    vs.push_back(val);

    __int128_t sum = accumulate(all(vs), 0LL);

    int d = 0;
    if (vs.front()) {
      for (__int128_t x = vs.front(); x; x /= 10) {
        ++d;
      }
    }
    ++resr[d][sum];

    if (sum == a and d) {
      ++ans;
    }
  }

  rep(st, 1, 1 << l) {
    string ss;

    bool cons_pos = false;
    __int128_t val = 0;
    vector<__int128_t> vs;
    rep(i, 0, l) if ((st >> i) & 1) {
      if (sl[i] == '+') {
        if (val == 0) {
          cons_pos = true;
          break;
        }
        vs.push_back(val);
        val = 0;
      } else {
        val = val * 10 + (sl[i] - '0');
      }
      ss.push_back(sl[i]);
    }
    if (cons_pos) {
      continue;
    }
    __int128_t sum = accumulate(all(vs), __int128_t(0));
    if (val == 0) {
      assert(ss.back() == '+');
      rep(d, 1, 20) {
        auto it = resr[d].find(a - sum);
        ans += it == resr[d].end() ? 0 : it->second;
      }
    } else {
      assert(ss.back() != '+');
      ans += sum + val == a;
      rep(d, 0, 20) {
        auto it = resr[d].find(a - sum - val);
        ans += it == resr[d].end() ? 0 : it->second;
        val *= 10;
      }
    }
  }
  return ans;
}

int main() {
  string s;
  long long a;
  read(s, a);
  print(solve(s, a));
  // mt19937 rng{ random_device{}() };

  // while (true) {
  //   int n = 1 + rng() % 15;
  //   string s;
  //   rep(i, 0, n) {
  //     if (rng() % 2) {
  //       s += '+';
  //     } else {
  //       s += '1' + (rng() % 9);
  //     }
  //   }
  //   int v = rng() % 1000 + 1;

  //   if (naive(s, v) != solve(s, v)) {
  //     print(s, v);
  //     cout.flush();
  //     break;
  //   }
  // }
}
accuracy: 1
time_ms: 150
memory_kb: 14880
final_rank: 3

code_snippet: #include <bits/stdc++.h>

using namespace std;

#define rep(i, l, r) for (int i = (l); i < r; ++i)
#define rrep(i, l, r) for (int i = (r - 1); i >= l; --i)
#define all(c) begin(c), end(c)

#ifdef LOCAL
#define debug(...) debug_impl(#__VA_ARGS__, __VA_ARGS__)
template <class H, class... Ts> void debug_impl(string s, H&& h, Ts &&... ts) {
  cerr << '(' << s << "): (" << forward<H>(h);
  ((cerr << ", " << forward<Ts>(ts)), ..., (cerr << ")\n"));
}
#else
#define debug(...) void(0)
#endif

template <class T> bool chmax(T& a, const T& b) {
  return b > a ? (a = b, true) : false;
}
template <class T> bool chmin(T& a, const T& b) {
  return b < a ? (a = b, true) : false;
}

template <class T> istream& operator>>(istream& in, vector<T>& v) {
  for (auto& e : v) in >> e;
  return in;
}
template <class ... Args> void read(Args &... args) {
  (cin >> ... >> args);
}
template <class T> ostream& operator<<(ostream& out, const vector<T>& v) {
  int n = v.size();
  rep(i, 0, n) {
    out << v[i];
    if (i + 1 != n) out << ' ';
  }
  return out;
}
template <class H, class ... Ts> void print(H&& h, Ts &&...ts) {
  cout << h, ((cout << " " << forward<Ts>(ts)), ..., (cout << "\n"));
}

struct io_setup_ {
  io_setup_() {
    ios::sync_with_stdio(false), cin.tie(nullptr);
    cout << fixed << setprecision(10);
    cerr << fixed << setprecision(10);
  }
} io_setup{};

int naive(string s, long long a) {
  int ans = 0;

  int n = s.size();

  rep(st, 1, 1 << n) {
    string ss;

    bool cons_pos = false;
    long long val = 0;
    vector<long long> vs;
    rep(i, 0, n) if ((st >> i) & 1) {
      ss += s[i];
      if (s[i] == '+') {
        if (val == 0) {
          cons_pos = true;
          break;
        }
        vs.push_back(val);
        val = 0;
      } else {
        val = val * 10 + (s[i] - '0');
      }
    }
    if (cons_pos or val == 0) {
      continue;
    }
    vs.push_back(val);

    long long sum = accumulate(all(vs), 0LL);

    if (sum == a) {
      ++ans;
    }
  }
  return ans;
}

long long solve(string s, __int128_t a) {
  long long ans = 0;

  const int n = s.size();
  const int l = n / 2, r = n - l;

  string sl = s.substr(0, l), sr = s.substr(l);

  vector<map<__int128_t, int>> resr(20);

  rep(st, 1, 1 << r) {
    string ss;

    bool cons_pos = false;
    __int128_t val = 0;
    vector<__int128_t> vs;
    rep(i, 0, r) if ((st >> i) & 1) {
      ss += sr[i];
      if (sr[i] == '+') {
        if (val == 0 and vs.size()) {
          assert(ss.end()[-2] == '+');
          cons_pos = true;
          break;
        }
        vs.push_back(val);
        val = 0;
      } else {
        val = val * 10 + (sr[i] - '0');
      }
    }
    if (cons_pos or val == 0) {
      continue;
    }
    vs.push_back(val);

    __int128_t sum = accumulate(all(vs), 0LL);

    int d = 0;
    if (vs.front()) {
      for (__int128_t x = val; x; x /= 10) {
        ++d;
      }
    }
    ++resr[d][sum];

    if (sum == a and d) {
      ++ans;
    }
  }

  rep(st, 1, 1 << l) {
    string ss;

    bool cons_pos = false;
    __int128_t val = 0;
    vector<__int128_t> vs;
    rep(i, 0, l) if ((st >> i) & 1) {
      if (sl[i] == '+') {
        if (val == 0) {
          cons_pos = true;
          break;
        }
        vs.push_back(val);
        val = 0;
      } else {
        val = val * 10 + (sl[i] - '0');
      }
      ss.push_back(sl[i]);
    }
    if (cons_pos) {
      continue;
    }
    __int128_t sum = accumulate(all(vs), __int128_t(0));
    if (val == 0) {
      assert(ss.back() == '+');
      rep(d, 1, 20) {
        auto it = resr[d].find(a - sum);
        ans += it == resr[d].end() ? 0 : it->second;
      }
    } else {
      assert(ss.back() != '+');
      ans += sum + val == a;
      rep(d, 0, 20) {
        auto it = resr[d].find(a - sum - val);
        ans += it == resr[d].end() ? 0 : it->second;
        val *= 10;
      }
    }
  }
  return ans;
}

int main() {
  string s;
  long long a;
  read(s, a);
  print(solve(s, a));
  // mt19937 rng{ random_device{}() };

  // while (true) {
  //   int n = 1 + rng() % 15;
  //   string s;
  //   rep(i, 0, n) {
  //     if (rng() % 2) {
  //       s += '+';
  //     } else {
  //       s += '1' + (rng() % 9);
  //     }
  //   }
  //   int v = rng() % 1000 + 1;

  //   if (naive(s, v) != solve(s, v)) {
  //     print(s, v);
  //     cout.flush();
  //     break;
  //   }
  // }
}
accuracy: 0.325
time_ms: 50
memory_kb: 3716
final_rank: 19

code_snippet: #include <bits/stdc++.h>

using namespace std;

#define rep(i, l, r) for (int i = (l); i < r; ++i)
#define rrep(i, l, r) for (int i = (r - 1); i >= l; --i)
#define all(c) begin(c), end(c)

#ifdef LOCAL
#define debug(...) debug_impl(#__VA_ARGS__, __VA_ARGS__)
template <class H, class... Ts> void debug_impl(string s, H&& h, Ts &&... ts) {
  cerr << '(' << s << "): (" << forward<H>(h);
  ((cerr << ", " << forward<Ts>(ts)), ..., (cerr << ")\n"));
}
#else
#define debug(...) void(0)
#endif

template <class T> bool chmax(T& a, const T& b) {
  return b > a ? (a = b, true) : false;
}
template <class T> bool chmin(T& a, const T& b) {
  return b < a ? (a = b, true) : false;
}

template <class T> istream& operator>>(istream& in, vector<T>& v) {
  for (auto& e : v) in >> e;
  return in;
}
template <class ... Args> void read(Args &... args) {
  (cin >> ... >> args);
}
template <class T> ostream& operator<<(ostream& out, const vector<T>& v) {
  int n = v.size();
  rep(i, 0, n) {
    out << v[i];
    if (i + 1 != n) out << ' ';
  }
  return out;
}
template <class H, class ... Ts> void print(H&& h, Ts &&...ts) {
  cout << h, ((cout << " " << forward<Ts>(ts)), ..., (cout << "\n"));
}

struct io_setup_ {
  io_setup_() {
    ios::sync_with_stdio(false), cin.tie(nullptr);
    cout << fixed << setprecision(10);
    cerr << fixed << setprecision(10);
  }
} io_setup{};

vector<string> naive(string s, long long a) {
  vector<string> expected;
  int n = s.size();

  rep(st, 1, 1 << n) {
    string ss;

    bool cons_pos = false;
    long long val = 0;
    vector<long long> vs;
    rep(i, 0, n) if ((st >> i) & 1) {
      ss += s[i];
      if (s[i] == '+') {
        if (val == 0) {
          cons_pos = true;
          break;
        }
        vs.push_back(val);
        val = 0;
      } else {
        val = val * 10 + (s[i] - '0');
      }
    }
    if (cons_pos or val == 0) {
      continue;
    }
    vs.push_back(val);

    long long sum = accumulate(all(vs), 0LL);

    if (sum == a) {
      expected.push_back(ss);
    }
  }
  sort(all(expected));
  return expected;
}

int main() {
  string s;
  long long a;
  read(s, a);

  long long ans = 0;

  const int n = s.size();
  const int l = n / 2, r = n - l;

  string sl = s.substr(0, l), sr = s.substr(l);

  vector<map<long long, int>> resr(20);

  rep(st, 1, 1 << r) {
    string ss;

    bool cons_pos = false;
    long long val = 0;
    vector<long long> vs;
    rep(i, 0, r) if ((st >> i) & 1) {
      ss += sr[i];
      if (sr[i] == '+') {
        if (val == 0 and vs.size()) {
          assert(ss.end()[-2] == '+');
          cons_pos = true;
          break;
        }
        vs.push_back(val);
        val = 0;
      } else {
        val = val * 10 + (sr[i] - '0');
      }
    }
    if (cons_pos or val == 0) {
      continue;
    }
    vs.push_back(val);

    long long sum = accumulate(all(vs), 0LL);

    int d = 0;
    if (vs.front()) {
      d = to_string(vs.front()).size();
    }
    // resr[d][sum].push_back(ss);
    ++resr[d][sum];

    if (sum == a and d) {
      ++ans;
    }
  }

  rep(st, 1, 1 << l) {
    string ss;

    bool cons_pos = false;
    __int128_t val = 0;
    vector<long long> vs;
    rep(i, 0, l) if ((st >> i) & 1) {
      if (sl[i] == '+') {
        if (val == 0) {
          cons_pos = true;
          break;
        }
        vs.push_back(val);
        val = 0;
      } else {
        val = val * 10 + (sl[i] - '0');
      }
      ss.push_back(sl[i]);
    }
    if (cons_pos) {
      continue;
    }
    long long sum = accumulate(all(vs), 0LL);
    if (val == 0) {
      assert(ss.back() == '+');
      // +
      rep(d, 1, 20) {
        auto it = resr[d].find(a - sum);
        ans += it == resr[d].end() ? 0 : it->second;
      }
    } else {
      assert(ss.back() != '+');
      ans += sum + val == a;
      // + ...
      rep(d, 0, 20) {

        auto it = resr[d].find(a - sum - val);
        ans += it == resr[d].end() ? 0 : it->second;
        val *= 10;
      }
    }
  }

  print(ans);

  // sort(all(actual));

  // print(actual);
  // print("///");
  // print(naive(s, a));
}
accuracy: 0.825
time_ms: 140
memory_kb: 12484
final_rank: 14

code_snippet: #include <bits/stdc++.h>
using namespace std;

long long a;
string s;

long long solve();
void calc(string &s, bool isleft, array<map<long long, long long>, 20> &mp);

int main() {
  cin >> s >> a;
  cout << solve() << endl;
  return 0;
}

long long solve() {
  int half = s.size() / 2;
  string sl, sr;
  sl = s.substr(0, half), sr = s.substr(half, s.size() - half);
  auto bit_enumerate = [](string &s, bool isleft) {
    array<map<long long, long long>, 20> res;
    int len = s.size();
    for (int i = 0; i < (1 << len); ++i) {
      string ns;
      for (int j = 0; j < len; ++j)
        if (i >> j & 1) ns += s[j];
      calc(ns, isleft, res);
    }
    return res;
  };
  auto mpl = bit_enumerate(sl, 1);
  auto mpr = bit_enumerate(sr, 0);
  long long res = mpl[0][a];
  for (int i = 0; i <= 18; ++i) res += mpr[i][a];
  for (auto [x, cnt] : mpr[19])
    if (mpl[0].count(a - x)) res += cnt * mpl[0][a - x];
  for (auto [x, cnt] : mpl[19])
    for (int i = 0; i <= 18; ++i)
      if (mpr[i].count(a - x)) res += cnt * mpr[i][a - x];
  for (int i = 0; i <= 18; ++i)
    for (auto [x, cnt] : mpr[i])
      if (mpl[i].count(a - x)) res += cnt * mpl[i][a - x];
  return res;
}

void calc(string &s, bool isleft, array<map<long long, long long>, 20> &mp) {
  int len = s.size();
  if (!len) return;
  for (int i = 1; i < len; ++i)
    if (s[i] == s[i - 1] && s[i] == '+') return;
  if (len == 1 && s[0] == '+') return;
  int is19 = 0;
  if (s.back() == '+') {
    if (!isleft) return;
    is19 = 1;
    s.pop_back();
    --len;
  }
  if (s[0] == '+') {
    if (isleft) return;
    is19 = 1;
    s = s.substr(1, len - 1);
  }
  long long res = 0;
  vector<long long> v;
  for (int i = 0; i < len; ++i) {
    long long now = 0;
    while (isdigit(s[i])) {
      if (now > a / 10) return;
      now *= 10;
      now += s[i++] - '0';
      if (now > a || res + now > a) return;
    }
    v.push_back(now);
    res += now;
  }
  if (is19)
    ++mp[19][res];
  else if (!isleft)
    ++mp[to_string(v[0]).size()][res];
  else {
    long long now = v.back();
    res -= now;
    for (int i = 0; i <= 18; ++i) {
      ++mp[i][res + now];
      if (now > a / 10) break;
      now *= 10;
      if (now + res > a) break;
    }
  }
}
accuracy: 1
time_ms: 480
memory_kb: 98044
final_rank: 10

code_snippet: #include<bits/stdc++.h>
using namespace std;
typedef long long int ll;
typedef unsigned long long int ull;
#define BIG_NUM 2000000000
#define HUGE_NUM 4000000000000000000 //オーバーフローに注意
#define MOD 1000000007
#define EPS 0.000000001



#define SIZE 40
#define MAX 22


enum Type{
	L_plus,
	L_num,
};


int len;
ll POW[MAX+1],A;
map<ll,ll> L_PLUS,L_NUM_ADD; //L_PLUS[加算値] = 場合の数
map<ll,ll> L_NUM[20];  //L_NUM[左端の桁数][加算値] = 場合の数
char buf[SIZE];

bool isNUM(char ch){

	return ch >= '0' && ch <= '9';
}

void calc_small(){

	int ans = 0;

	for(ll state = 1; state < POW[len]; state++){

		vector<char> vec;
		for(int loop = 0; loop < len; loop++){
			if(state&POW[loop]){

				vec.push_back(buf[loop]);
			}
		}

		if(vec[0] == '+' || vec.back() == '+')continue;

		bool FLG = true;
		for(int i = 0; i < vec.size()-1; i++){
			if(vec[i] == '+' && vec[i+1] == '+'){

				FLG = false;
				break;
			}
		}
		if(!FLG)continue;

		int ind = 0;
		ll tmp = 0;

		while(ind < vec.size()){

			ll add = 0;
			while(ind < vec.size() && isNUM(vec[ind])){

				if(add > A/10){

					FLG = false;
					break;
				}
				add = 10*add + (vec[ind]-'0');
				ind++;
			}
			if(!FLG)break;

			tmp += add;
			ind++;
		}
		if(!FLG)continue;

		if(tmp == A){

			ans++;
		}
	}


	printf("%d\n",ans);
}


int main(){

	POW[0] = 1;
	for(int i = 1; i <= MAX; i++){

		POW[i] = POW[i-1]*2;
	}

	scanf("%s",buf);
	for(len = 0; buf[len] != '\0'; len++);

	scanf("%lld",&A);

	if(len <= 20){

		calc_small();
		return 0;
	}


	vector<char> L,R;
	for(int i = 0; i < len/2; i++){

		L.push_back(buf[i]);
	}
	for(int i = len/2; i < len; i++){

		R.push_back(buf[i]);
	}

	/*■後半len/2文字を前計算■*/
	ll ans = 0;

	for(int state = 1; state < POW[R.size()]; state++){

		vector<char> vec;
		for(int loop = 0; loop < R.size(); loop++){
			if(state&POW[loop]){

				vec.push_back(R[loop]);
			}
		}
		if(vec.back() == '+')continue;

		bool FLG = true;
		for(int i = 0; i < vec.size()-1; i++){

			if(vec[i] == '+' && vec[i+1] == '+'){

				FLG = false;
				break;
			}
		}
		if(!FLG)continue;

		bool is_add = false;
		int ind = 0;

		if(vec[0] == '+'){

			is_add = true;
			ind++;
		}

		bool is_first = true;
		ll value_L = 0;
		int num_digit = 0;
		ll tmp = 0;

		while(ind < vec.size()){

			ll add = 0;
			while(ind < vec.size() && isNUM(vec[ind])){

				if(add > A/10){

					FLG = false;
					break;
				}
				add = 10*add + (vec[ind]-'0');
				ind++;
			}
			if(!FLG)break;

			if(!is_add && is_first){ //左端が数字で、かつ初めての数字列

				num_digit = ind; //左端の数字が何桁か
				value_L = add;

			}else{ //その他の場合

				tmp += add;
			}
			is_first = false;
			ind++;
		}
		if(!FLG)continue;

		if(!is_add && (tmp+value_L) == A){ //左端が数字

			ans++;
		}

		if(is_add){ //左端が+

			L_PLUS[tmp] += 1;

		}else{ //左端が数字

			L_NUM_ADD[(value_L+tmp)] += 1; //[左の部分列] + で接続
			L_NUM[num_digit][value_L+tmp] += 1; //[左の部分列] + 数字 で接続
		}
	}

	//前半の計算
	for(int state = 1; state < POW[L.size()]; state++){

		vector<char> vec;
		for(int loop = 0; loop < L.size(); loop++){
			if(state&POW[loop]){

				vec.push_back(L[loop]);
			}
		}

		if(vec[0] == '+')continue;

		bool FLG = true;
		for(int i = 0; i < vec.size()-1; i++){
			if(vec[i] == '+' && vec[i+1] == '+'){

				FLG = false;
				break;
			}
		}
		if(!FLG)continue;

		if(vec.back() == '+'){ //右端がプラス

			vec.pop_back();

			int ind = 0;
			ll tmp = 0;

			while(ind < vec.size()){

				ll add = 0;
				while(ind < vec.size() && isNUM(vec[ind])){

					if(add > A/10){

						FLG = false;
						break;
					}
					add = 10*add + (vec[ind]-'0');
					ind++;
				}
				if(!FLG)break;

				tmp += add;
				ind++;
			}
			if(!FLG)continue;

			if(tmp >= A)continue;
			ll rest = A-tmp;

			auto at = L_NUM_ADD.find(rest);
			if(at == L_NUM_ADD.end())continue;

			ans += L_NUM_ADD[rest];

		}else{ //右端が数字


			int last = -1;
			for(int i = 0; i < vec.size(); i++){
				if(vec[i] == '+'){

					last = i;
				}
			}

			int ind = 0;
			ll base = 0;

			while(ind < last){

				ll add = 0;
				while(ind < last && isNUM(vec[ind])){

					if(add > A/10){

						FLG = false;
						break;
					}
					add = 10*add + (vec[ind]-'0');
					ind++;
				}
				if(!FLG)break;

				base += add;
				ind++;
			}
			if(!FLG)continue;

			//最後のプラスより右の数字
			ll num = 0;
			for(int i = last+1; i < vec.size(); i++){

				if(num > A/10){
					FLG = false;
					break;
				}
				num = 10*num+(vec[i]-'0');
			}
			if(!FLG)continue;
			if(base+num > A)continue;

			if(base+num == A){

				ans++;
				continue;
			}

			//■L_PLUSに接続■
			ll work = A-(base+num);
			auto bt = L_PLUS.find(work);

			if(bt != L_PLUS.end()){

				ans += L_PLUS[work];
			}

			//■左端が数字に接続■
			for(int i = 1; i <= 18; i++){ //左にシフトする桁数

				if(num > (A-base)/10){

					break;
				}

				num *= 10;

				ll rest = (A-(base+num));

				auto at = L_NUM[i].find(rest);
				if(at == L_NUM[i].end())continue;

				ans += L_NUM[i][rest];
			}
		}
	}

	printf("%lld\n",ans);

	return 0;
}
accuracy: 1
time_ms: 140
memory_kb: 21816
final_rank: 5

code_snippet: #include<bits/stdc++.h>
using namespace std;
typedef long long int ll;
typedef unsigned long long int ull;
#define BIG_NUM 2000000000
#define HUGE_NUM 4000000000000000000 //オーバーフローに注意
#define MOD 1000000007
#define EPS 0.000000001



#define SIZE 40
#define MAX 22


enum Type{
	L_plus,
	L_num,
};


int len;
ll POW[MAX+1],A;
map<ll,ll> L_PLUS,L_NUM_ADD; //L_PLUS[加算値] = 場合の数
map<ll,ll> L_NUM[20];  //L_NUM[左端の桁数][加算値] = 場合の数
char buf[SIZE];

bool isNUM(char ch){

	return ch >= '0' && ch <= '9';
}

void calc_small(){

	int ans = 0;

	for(ll state = 1; state < POW[len]; state++){

		vector<char> vec;
		for(int loop = 0; loop < len; loop++){
			if(state&POW[loop]){

				vec.push_back(buf[loop]);
			}
		}

		if(vec[0] == '+' || vec.back() == '+')continue;

		bool FLG = true;
		for(int i = 0; i < vec.size()-1; i++){
			if(vec[i] == '+' && vec[i+1] == '+'){

				FLG = false;
				break;
			}
		}
		if(!FLG)continue;

		int ind = 0;
		ll tmp = 0;

		while(ind < vec.size()){

			ll add = 0;
			while(ind < vec.size() && isNUM(vec[ind])){

				if(add > A/10){

					FLG = false;
					break;
				}
				add = 10*add + (vec[ind]-'0');
				ind++;
			}
			if(!FLG)break;

			tmp += add;
			ind++;
		}
		if(!FLG)continue;

		if(tmp == A){

			ans++;
		}
	}


	printf("%d\n",ans);
}


int main(){

	POW[0] = 1;
	for(int i = 1; i <= MAX; i++){

		POW[i] = POW[i-1]*2;
	}

	scanf("%s",buf);
	for(len = 0; buf[len] != '\0'; len++);

	scanf("%lld",&A);

	//printf("len:%d\n",len);

	if(len <= 20){

		calc_small();
		return 0;
	}


	vector<char> L,R;
	for(int i = 0; i < len/2; i++){

		L.push_back(buf[i]);
	}
	for(int i = len/2; i < len; i++){

		R.push_back(buf[i]);
	}

	//printf("L.size():%lld R.size():%lld\n",L.size(),R.size());


	/*■後半len/2文字を前計算■*/
	ll ans = 0;

	for(int state = 1; state < POW[R.size()]; state++){

		vector<char> vec;
		for(int loop = 0; loop < R.size(); loop++){
			if(state&POW[loop]){

				vec.push_back(R[loop]);
			}
		}
		if(vec.back() == '+')continue;

		bool FLG = true;
		for(int i = 0; i < vec.size()-1; i++){

			if(vec[i] == '+' && vec[i+1] == '+'){

				FLG = false;
				break;
			}
		}
		if(!FLG)continue;

		bool is_add = false;
		int ind = 0;

		if(vec[0] == '+'){

			is_add = true;
			ind++;
		}

		bool is_first = true;
		ll value_L = 0;
		int num_digit = 0;
		ll tmp = 0;

		while(ind < vec.size()){

			ll add = 0;
			while(ind < vec.size() && isNUM(vec[ind])){

				if(add > A/10){

					FLG = false;
					break;
				}
				add = 10*add + (vec[ind]-'0');
				ind++;
			}
			if(!FLG)break;

			if(!is_add && is_first){ //左端が数字で、かつ初めての数字列

				num_digit = ind; //左端の数字が何桁か
				value_L = add;

			}else{ //その他の場合

				tmp += add;
			}
			is_first = false;
			ind++;
		}
		if(!FLG)continue;

		if(!is_add && (tmp+value_L) == A){ //左端が数字

			ans++;
		}

		if(is_add){ //左端が+

			L_PLUS[tmp] += 1;

		}else{ //左端が数字

			L_NUM_ADD[(value_L+tmp)] += 1; //[左の部分列] + で接続
			L_NUM[num_digit][value_L+tmp] += 1; //[左の部分列] + 数字 で接続
		}
	}

	//前半の計算
	for(int state = 1; state < POW[L.size()]; state++){

		vector<char> vec;
		for(int loop = 0; loop < L.size(); loop++){
			if(state&POW[loop]){

				vec.push_back(L[loop]);
			}
		}

		if(vec[0] == '+')continue;

		bool FLG = true;
		for(int i = 0; i < vec.size()-1; i++){
			if(vec[i] == '+' && vec[i+1] == '+'){

				FLG = false;
				break;
			}
		}
		if(!FLG)continue;

		if(vec.back() == '+'){ //右端がプラス

			vec.pop_back();

			int ind = 0;
			ll tmp = 0;

			while(ind < vec.size()){

				ll add = 0;
				while(ind < vec.size() && isNUM(vec[ind])){

					if(add > A/10){

						FLG = false;
						break;
					}
					add = 10*add + (vec[ind]-'0');
					ind++;
				}
				if(!FLG)break;

				tmp += add;
				ind++;
			}
			if(!FLG)continue;

			if(tmp >= A)continue;
			ll rest = A-tmp;

			auto at = L_NUM_ADD.find(rest);
			if(at == L_NUM_ADD.end())continue;

			ans += L_NUM_ADD[rest];

		}else{ //右端が数字


			int last = -1;
			for(int i = 0; i < vec.size(); i++){
				if(vec[i] == '+'){

					last = i;
				}
			}

			int ind = 0;
			ll base = 0;

			while(ind < last){

				ll add = 0;
				while(ind < last && isNUM(vec[ind])){

					if(add > A/10){

						FLG = false;
						break;
					}
					add = 10*add + (vec[ind]-'0');
					ind++;
				}
				if(!FLG)break;

				base += add;
				ind++;
			}
			if(!FLG)continue;

			//最後のプラスより右の数字
			ll num = 0;
			for(int i = last+1; i < vec.size(); i++){

				if(num > A/10){
					FLG = false;
					break;
				}
				num = 10*num+(vec[i]-'0');
			}
			if(!FLG)continue;
			if(base+num > A)continue;

			if(base+num == A){

				ans++;
				continue;
			}

			//■L_PLUSに接続■
			ll work = A-(base+num);
			auto bt = L_PLUS.find(work);

			if(bt != L_PLUS.end()){

				ans += L_PLUS[work];
			}

			//■左端が数字に接続■
			for(int i = 1; i <= 18; i++){ //左にシフトする桁数

				if(num > (A-base)/10){

					break;
				}

				num *= 10;

				ll rest = (A-(base+num));

				auto at = L_NUM[i].find(rest);
				if(at == L_NUM[i].end())continue;

				ans += L_NUM[i][rest];
			}
		}
	}

	printf("%lld\n",ans);

	return 0;
}
accuracy: 1
time_ms: 140
memory_kb: 21928
final_rank: 6

code_snippet: #include<bits/stdc++.h>
using namespace std;
typedef long long int ll;
typedef unsigned long long int ull;
#define BIG_NUM 2000000000
#define HUGE_NUM 4000000000000000000 //オーバーフローに注意
#define MOD 1000000007
#define EPS 0.000000001



#define SIZE 40
#define MAX 22


enum Type{
	L_plus,
	L_num,
};


int len;
ll POW[MAX+1],A;
map<ll,ll> L_PLUS,L_NUM_ADD; //L_PLUS[加算値] = 場合の数
map<ll,ll> L_NUM[20];  //L_NUM[左端の桁数][加算値] = 場合の数
char buf[SIZE];

bool isNUM(char ch){

	return ch >= '0' && ch <= '9';
}

void calc_small(){

	int ans = 0;

	for(ll state = 1; state < POW[len]; state++){

		vector<char> vec;
		for(int loop = 0; loop < len; loop++){
			if(state&POW[loop]){

				vec.push_back(buf[loop]);
			}
		}

		if(vec[0] == '+' || vec.back() == '+')continue;

		bool FLG = true;
		for(int i = 0; i < vec.size()-1; i++){
			if(vec[i] == '+' && vec[i+1] == '+'){

				FLG = false;
				break;
			}
		}
		if(!FLG)continue;

		int ind = 0;
		ll tmp = 0;

		while(ind < vec.size()){

			ll add = 0;
			while(ind < vec.size() && isNUM(vec[ind])){

				if(add > A/10){

					FLG = false;
					break;
				}
				add = 10*add + (vec[ind]-'0');
				ind++;
			}
			if(!FLG)break;

			tmp += add;
			ind++;
		}
		if(!FLG)continue;

		if(tmp == A){

			ans++;
		}
	}


	printf("%d\n",ans);
}


int main(){

	POW[0] = 1;
	for(int i = 1; i <= MAX; i++){

		POW[i] = POW[i-1]*2;
	}

	scanf("%s",buf);
	for(len = 0; buf[len] != '\0'; len++);

	scanf("%lld",&A);

	//printf("len:%d\n",len);

	if(len <= 20){

		calc_small();
		return 0;
	}


	vector<char> L,R;
	for(int i = 0; i < len/2; i++){

		L.push_back(buf[i]);
	}
	for(int i = len/2; i < len; i++){

		R.push_back(buf[i]);
	}


	/*■後半len/2文字を前計算■*/
	ll ans = 0;

	for(int state = 1; state < POW[R.size()]; state++){

		vector<char> vec;
		for(int loop = 0; loop < R.size(); loop++){
			if(state&POW[loop]){

				vec.push_back(R[loop]);
			}
		}
		if(vec.back() == '+')continue;

		bool FLG = true;
		for(int i = 0; i < vec.size()-1; i++){

			if(vec[i] == '+' && vec[i+1] == '+'){

				FLG = false;
				break;
			}
		}
		if(!FLG)continue;

		bool is_add = false;
		int ind = 0;

		if(vec[0] == '+'){

			is_add = true;
			ind++;
		}

		bool is_first = true;
		ll value_L = -1;
		int num_digit = 0;
		ll tmp = 0;

		while(ind < vec.size()){

			ll add = 0;
			while(ind < vec.size() && isNUM(vec[ind])){

				if(add > A/10){

					FLG = false;
					break;
				}
				add = 10*add + (vec[ind]-'0');
				ind++;
			}
			if(!FLG)break;

			if(!is_add && is_first){ //左端が数字

				num_digit = ind; //左端の数字が何桁か
				value_L = add;

			}else{ //左端がプラス

				tmp += add;
			}
			is_first = false;
			ind++;
		}
		if(!FLG)continue;

		if(!is_add && tmp == A){ //左端が数字

			ans++;
		}

		if(is_add){ //左端が+

			L_PLUS[tmp] += 1;

		}else{ //左端が数字

			L_NUM_ADD[(value_L+tmp)] += 1; //[左の部分列] + で接続
			L_NUM[num_digit][value_L+tmp] += 1; //[左の部分列] + 数字 で接続
		}
	}

	//前半の計算
	for(int state = 1; state < POW[L.size()]; state++){

		vector<char> vec;
		for(int loop = 0; loop < L.size(); loop++){
			if(state&POW[loop]){

				vec.push_back(L[loop]);
			}
		}

		if(vec[0] == '+')continue;

		bool FLG = true;
		for(int i = 0; i < vec.size()-1; i++){
			if(vec[i] == '+' && vec[i+1] == '+'){

				FLG = false;
				break;
			}
		}
		if(!FLG)continue;

		if(vec.back() == '+'){ //右端がプラス

			vec.pop_back();

			int ind = 0;
			ll tmp = 0;

			while(ind < vec.size()){

				ll add = 0;
				while(ind < vec.size() && isNUM(vec[ind])){

					if(add > A/10){

						FLG = false;
						break;
					}
					add = 10*add + (vec[ind]-'0');
					ind++;
				}
				if(!FLG)break;

				tmp += add;
				ind++;
			}
			if(!FLG)continue;

			if(tmp >= A)continue;
			ll rest = A-tmp;

			auto at = L_NUM_ADD.find(rest);
			if(at == L_NUM_ADD.end())continue;

			ans += L_NUM_ADD[rest];

		}else{ //右端が数字


			int last = -1;
			for(int i = 0; i < vec.size(); i++){
				if(vec[i] == '+'){

					last = i;
				}
			}

			int ind = 0;
			ll base = 0;

			while(ind < last){

				ll add = 0;
				while(ind < last && isNUM(vec[ind])){

					if(add > A/10){

						FLG = false;
						break;
					}
					add = 10*add + (vec[ind]-'0');
					ind++;
				}
				if(!FLG)break;

				base += add;
				ind++;
			}
			if(!FLG)continue;

			//最後のプラスより右の数字
			ll num = 0;
			for(int i = last+1; i < vec.size(); i++){

				if(num > A/10){
					FLG = false;
					break;
				}
				num = 10*num+(vec[i]-'0');
			}
			if(!FLG)continue;
			if(base+num > A)continue;

			if(base+num == A){

				ans++;
				continue;
			}

			for(int i = 1; i <= 18; i++){ //左にシフトする桁数

				if(num > (A-base)/10){

					break;
				}

				num *= 10;

				ll rest = (A-(base+num));

				auto at = L_NUM[i].find(rest);
				if(at == L_NUM[i].end())continue;

				ans += L_NUM[i][rest];
			}
		}
	}

	printf("%lld\n",ans);

	return 0;
}
accuracy: 0.275
time_ms: 60
memory_kb: 3536
final_rank: 20

code_snippet: #include <iostream>
#include <numeric>
#include <vector>
#include <map>

using namespace std;
using lint = long long;

vector<lint> parse(const string& s) {
    vector<lint> ret{0};

    for (auto c : s) {
        if (c == '+') {
            if (ret.back() == 0) {
                // 先頭が+ or +が連続
                ret.clear();
                break;
            }
            ret.push_back(0);
        } else {
            ret.back() = ret.back() * 10 + (c - '0');
        }
    }

    // 末尾が+
    if (!ret.empty() && ret.back() == 0) ret.clear();

    return ret;
}

// 桁数
int digit(lint x) {
    int ret = 0;
    while (x > 0) {
        ++ret;
        x /= 10;
    }
    return ret;
}

void solve() {
    string s;
    lint a;
    cin >> s >> a;

    int n = s.length();
    int nf = n / 2, nb = n - nf;

    lint ans = 0;

    map<lint, lint> bs, bplus;
    // 数字で始まるやつ、+で始まるやつ
    vector<map<lint, lint>> bds(nb + 1);
    // 数字で始まるやつ 先頭項の長さ毎に管理

    for (int b = 1; b < (1 << nb); ++b) {
        string t;
        for (int i = 0; i < nb; ++i) {
            if ((b >> i) & 1) t.push_back(s[nf + i]);
        }

        if (!t.empty() && t.front() == '+') {
            t.erase(t.begin());

            auto v = parse(t);
            if (!v.empty()) {
                auto sum = accumulate(v.begin(), v.end(), 0LL);
                ++bplus[sum];
            }
        } else {
            auto v = parse(t);

            if (!v.empty()) {
                auto front = v.front();
                auto sum = accumulate(v.begin(), v.end(), 0LL);
                ++bs[sum];
                ++bds[digit(front)][sum];

                // 左が空の場合
                if (sum == a) ++ans;
            }
        }
    }

    for (int b = 1; b < (1 << nf); ++b) {
        string t;
        for (int i = 0; i < nf; ++i) {
            if ((b >> i) & 1) t.push_back(s[i]);
        }

        if (!t.empty() && t.back() == '+') {
            t.pop_back();
            auto v = parse(t);

            if (!v.empty()) {
                auto sum = accumulate(v.begin(), v.end(), 0LL);
                // +で終わる場合 右側は数字始まり
                ans += bs[a - sum];
            }
        } else {
            auto v = parse(t);

            if (!v.empty()) {
                // 数字で終わる場合
                auto sum = accumulate(v.begin(), v.end(), 0LL);

                // 右が+から始まる場合
                ans += bplus[a - sum];

                // 右が空の場合
                if (sum == a) ++ans;

                // 右が数字から始まる場合
                auto back = v.back();
                sum -= back;

                lint ten = 1;
                for (int d = 0; d <= nb; ++d, ten *= 10) {
                    auto r = a - sum - ten * back;
                    if (r < 0) break;
                    ans += bds[d][r];
                }
            }
        }
    }

    cout << ans << "\n";
}

int main() {
    cin.tie(nullptr);
    ios::sync_with_stdio(false);

    solve();

    return 0;
}
accuracy: 1
time_ms: 520
memory_kb: 114744
final_rank: 12

code_snippet: #include <iostream>
#include <string>
#include <vector>
#include <algorithm>
#include <cstdlib>
using namespace std;

typedef long long LL;

bool valid(const string &t){
	for(size_t i = 1; i < t.size(); ++i){
		if(t[i - 1] == '+' && t[i] == '+'){ return false; }
	}
	return true;
}

LL solve(const string &s, LL tgt){
	int n = s.size();
	int h = n / 2;
	string t;
	vector<LL> v[20];
	for(int b = 1 << (n - h); b--; ){
		t.clear();
		for(int i = h; i < n; ++i){
			if(b >> (i - h) & 1){
				t += s[i];
			}
		}
		t += "+0";
		if(!valid(t)){ continue; }
		int k = t.find('+');
		LL z = 0;
		const char *p = t.c_str();
		while(*p){
			char *endp;
			z += strtoll(p, &endp, 10);
			p = endp;
		}
		v[k].push_back(z);
	}
	for(auto &w : v){
		sort(w.begin(), w.end());
	}

	LL ans = 0;
	for(int b = 1 << h; b--; ){
		t = "0+";
		for(int i = 0; i < h; ++i){
			if(b >> i & 1){ t += s[i]; }
		}
		if(!valid(t)){ continue; }
		LL x = 0, y = 0;
		for(char c : t){
			if(c == '+'){
				x += y;
				y = 0;
			}
			else{
				y = y * 10 + c - '0';
			}
		}

		for(int k = 0; k < 20; ++k){
			if(y != 0 || k != 0){
				auto p = equal_range(v[k].begin(), v[k].end(), tgt - x - y);
				ans += p.second - p.first;
			}
			if(y > tgt / 10){ break; }
			y *= 10;
		}
	}
	return ans;
}

int main(){
	string s;
	LL a;
	while(cin >> s >> a){
		LL ans = solve(s, a);
		cout << ans << '\n';
	}
}
accuracy: 1
time_ms: 110
memory_kb: 5604
final_rank: 1