Problem:
パスワードをお忘れですか？
二郎君は数式を用いたセキュリティシステムを開発した。システムにはa、b、c、dを変数とする数式が設定されており、それぞれに０から９までの整数を入力すると計算が行われる。その計算結果が正の整数$N$と一致したとき、ロックが解除される。ロックを解除できる４つの整数をa,b,c,dの順に並べた４桁の数字の列がパスワードになる。
二郎君はこのシステムで彼が使っているパスワードを忘れてしまった。幸い、二郎君はシステムに設定されている数式と、$N$の値は覚えていたので、それを使ってパスワードを特定しようと考えた。
システムに設定されている数式と正の整数Nが与えられたとき、パスワードの候補の数を求めるプログラムを作成せよ。
入力
入力は以下の形式で与えられる。
$S$
$N$
１行目にシステムに設定されている数式を表す文字列$S$が与えられる。２行目に、整数$N$($1 \leq N \leq 10,000,000,000,000,000 = 10^{16}$)が与えられる。
$S$は算術式を表す文字列であり、以下の要素から構成される。
加減乗算（それぞれ +, -, *）。
カッコ「(」または「)」。
0から10,000までの整数。
変数a,b,c,d。
変数の連結。
変数が連続して与えられた場合は変数の連結を表し、それらの変数の値を並べてできる一つの数とみなす。たとえば、a=0、b=1、c=2なら、abcは一つの整数12を表す。
また、$S$は以下の条件を満たす。
符号を反転する記号として演算子-が使われることはない。
乗算を表すときに演算子*が省略されることはない。
Sの長さは200以下である。
乗算演算子*は２回までしか現れない。
変数が連続するのは４つまでである（たとえば、abcda, aaabb, aaaaaaのようなものは与えられない）。
空白文字は含まれない。
変数の連結を除いて、通常の数式である。
出力
パスワードの候補の数を１行目に出力する。パスワードがひとつに特定できる場合は、それを２行目に出力する。
入出力例
入力例１
a+b+c+d
36
出力例１
1
9999
入力例２
a+b+c+d
5
出力例２
56
式a+b+c+dが5になるのはa=b=c=0,d=5のときだが、他にもa=b=c=1,d=2などでも5になるので、合計で56通りの候補が存在する。
入力例３
b+c+d
27
出力例３
10
式b+c+dが27になるのはb=c=d=9のときだが、aの選び方は任意なので10通りの候補が存在する。
入力例４
abcd+abcd
1024
出力例４
1
0512
式abcd+abcdが1024になるのはa=0,b=5,c=1,d=2のときだけなので、候補は１つだけであり、0512が唯一のパスワードである。

Submitted solutions:

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

signed main(){
  string s,t="+-*()"; int m; cin >> s >> m; int n = s.size();
  sort(t.begin(),t.end());
  vector<string> S; stack<char> O;
  auto popop = [&](string u){
    sort(u.begin(),u.end());
    while(!O.empty()&&binary_search(u.begin(),u.end(),O.top())) S.emplace_back(1,O.top()),O.pop();
  };
  for (int i(0);i < n;){
    if (binary_search(t.begin(),t.end(),s[i])){
      char c = s[i++];
      if (c=='+'||c=='-') popop("+-*"),O.emplace(c);
      if (c=='*') popop("*"),O.emplace(c);
      if (c=='(') O.emplace(c);
      if (c==')') popop("+-*"),O.pop();
    } else {
      string r;
      for (;i < n&&!binary_search(t.begin(),t.end(),s[i]);++i) r += s[i];
      S.emplace_back(r);
    }
  }
  popop("+-*");
  stack<int> T; vector<string> R; int p;
  for (int a(0);a < 10;++a) for (int b(0);b < 10;++b) for (int c(0);c < 10;++c) for (int d(0);d < 10;++d){
    while(!T.empty()) T.pop();
    for (auto A:S){
      if (A=="+") p = T.top(),T.pop(),T.top() += p;
      else if (A=="-") p = T.top(),T.pop(),T.top() -= p;
      else if (A=="*") p = T.top(),T.pop(),T.top() *= p;
      else {
        while(find(A.begin(),A.end(),'a')!=A.end()) A[find(A.begin(),A.end(),'a')-A.begin()] = '0'+a;
        while(find(A.begin(),A.end(),'b')!=A.end()) A[find(A.begin(),A.end(),'b')-A.begin()] = '0'+b;
        while(find(A.begin(),A.end(),'c')!=A.end()) A[find(A.begin(),A.end(),'c')-A.begin()] = '0'+c;
        while(find(A.begin(),A.end(),'d')!=A.end()) A[find(A.begin(),A.end(),'d')-A.begin()] = '0'+d;
        T.emplace(stoi(A));
      }
    }
    if (T.top()==m) R.emplace_back(string({'0'+a,'0'+b,'0'+c,'0'+d}));
  }
  cout << R.size() << endl;
  if (R.size()==1) cout << R[0] << endl;
}
accuracy: 1
time_ms: 50
memory_kb: 3740
final_rank: 12

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

constexpr int BIG_INT = 1'000'000'100;
constexpr long BIG_LONG = 1'000'000'000'000'000'100;

struct calc {
    // num#+-*join
    string type;
    calc *left;
    calc *right;
    char val;
    int value = 0;
};

struct tmp {
    string s;
    int now;
};
calc *par(tmp &a);
calc *num(tmp &a) {
    if ('(' == a.s[a.now]) {
        a.now++;
        calc *ret = par(a);
        a.now++;
        return ret;
    } else if ('a' <= a.s[a.now] && a.s[a.now] <= 'd') {
        calc *ret = new calc{"#", nullptr, nullptr, a.s[a.now]};
        a.now++;
        return ret;
    } else {
        int num = 0;
        while ('0' <= a.s[a.now] && a.s[a.now] <= '9') {
            num *= 10;
            num += a.s[a.now] - '0';
            a.now++;
        }
        calc *ret = new calc{"num", nullptr, nullptr, '\n', num};
        return ret;
    }
}
calc *join(tmp &a) {
    calc *ret = num(a);
    while (a.now < a.s.size()) {
        if (a.s[a.now] >= 'a' && a.s[a.now] <= 'd') {
            ret = new calc{"join", ret, num(a), '\0'};
        } else {
            break;
        }
    }
    return ret;
}
calc *mult(tmp &a) {
    calc *ret = join(a);
    while (a.now < a.s.size()) {
        if (a.s[a.now] == '*') {
            a.now++;
            ret = new calc{"*", ret, join(a), '\0'};
        } else {
            break;
        }
    }
    return ret;
}
calc *par(tmp &a) {
    calc *ret = mult(a);
    while (a.now < a.s.size()) {
        if (a.s[a.now] == '+') {
            a.now++;
            ret = new calc{"+", ret, mult(a), '\0'};
        } else if (a.s[a.now] == '-') {
            a.now++;
            ret = new calc{"-", ret, mult(a), '\0'};
        } else {
            break;
        }
    }
    return ret;
}

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

    string s;
    cin >> s;
    long n;
    cin >> n;
    tmp t = tmp{s, 0};
    calc *expr = par(t);

    long a, b, c, d;
    auto dfs = [&](auto self, calc *e) -> long {
        if (e->type == "+") {
            return self(self, e->left) + self(self, e->right);
        } else if (e->type == "-") {
            return self(self, e->left) - self(self, e->right);
        } else if (e->type == "*") {
            return self(self, e->left) * self(self, e->right);
        } else if (e->type == "join") {
            return self(self, e->left) * 10 + self(self, e->right);
        } else if (e->type == "#") {
            if (e->val == 'a') {
                return a;
            }
            if (e->val == 'b') {
                return b;
            }
            if (e->val == 'c') {
                return c;
            }
            if (e->val == 'd') {
                return d;
            }
            cout << "ERR" << endl;
            return -1;
        } else if (e->type == "num") {
            return e->value;
        } else {
            cout << "ERR" << endl;
            return -1;
        }
    };

    int ans = 0;
    vector<long> pr;
    for (a = 0; a <= 9; a++) {
        for (b = 0; b <= 9; b++) {
            for (c = 0; c <= 9; c++) {
                for (d = 0; d <= 9; d++) {
                    if (dfs(dfs, expr) == n) {
                        ans++;
                        pr = {a, b, c, d};
                    }
                }
            }
        }
    }
    if (ans == 1) {
        cout << 1 << endl;
        for (auto i : pr) cout << i;
        cout << endl;
    } else {
        cout << ans << endl;
    }
}
accuracy: 1
time_ms: 70
memory_kb: 3444
final_rank: 10

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

long long power(int x, int n){
    long long res = 1;
    for(int i = 0; i < n; i++) res *= x;
    return res;
}

int pow_10[6] = {1, 10, 100, 1000, 10000, 100000};

int main(){
    string s;
    cin >> s;
    long long n;
    cin >> n;
    
    auto f = [&](auto self, string s)->pair<vector<array<long long, 5>>, vector<array<long long, 5>>> {
        int n = s.size();
        if(n == 0) return make_pair(vector<array<long long, 5>>(), vector<array<long long, 5>>());
        if(s[0] == '(') {
            //続く1項を探す
            int cnt = 1;
            for(int i = 1; i < n; i++){
                if(s[i] == '(') cnt++;
                if(s[i] == ')') cnt--;
                if(cnt == 0 && s[i] != ')'){
                    int las = i-1;
                    if(s[i] == '*'){
                        auto l = self(self, s.substr(1, las-1));
                        auto r = self(self, s.substr(las+2));
                        for(auto i : l.second) l.first.push_back(i);
                        
                        vector<array<long long, 5>> resl;
                        for(int i = 0; i < (int)l.first.size(); i++){
                            auto arr = l.first[i];
                            for(auto j : r.first){
                                resl.push_back({arr[0]+j[0], arr[1]+j[1], arr[2]+j[2], arr[3]+j[3], arr[4]*j[4]});
                            }
                        }
                        return make_pair(resl, r.second);
                    } else {
                        auto res1 = self(self, s.substr(1, las-1));
                        auto res2 = self(self, s.substr(las+1));
                        for(auto i : res1.second) res1.first.push_back(i);
                        if(s[i] == '-') for(auto &i : res2.first) for(auto &j : i) j = -j;
                        for(auto i : res2.second) res2.first.push_back(i);
                        return make_pair(res1.first, res2.first);
                    }
                }
            }
            auto res = self(self, s.substr(1, n-2));
            for(auto i : res.second) res.first.push_back(i);
            return make_pair(res.first, vector<array<long long, 5>>());
        } else if(s[0] == 'a' || s[0] == 'b' || s[0] == 'c' || s[0] == 'd') {
            int las = n-1;
            for(int i = 1; i < n; i++){
                if(s[i] == '+' || s[i] == '-' || s[i] == '*'){
                    las = i-1;
                    break;
                }
            }
            vector<array<long long, 5>> abcdn;
            for(int i = 0; i <= las; i++){
                if(s[i] == 'a') abcdn.push_back({1, 0, 0, 0, pow_10[las-i]});
                if(s[i] == 'b') abcdn.push_back({0, 1, 0, 0, pow_10[las-i]});
                if(s[i] == 'c') abcdn.push_back({0, 0, 1, 0, pow_10[las-i]});
                if(s[i] == 'd') abcdn.push_back({0, 0, 0, 1, pow_10[las-i]});
            }
            //cout << s << '\n';
            //for(auto i : abcdn) cout << i[0] << ' ' << i[1] << ' ' << i[2] << ' ' << i[3] << ' ' << i[4] << '\n';
            if(las == n-1) return make_pair(abcdn, vector<array<long long, 5>>());
            
            auto r = self(self, s.substr(las+2));
            if(s[las+1] == '*'){
                vector<array<long long, 5>> resl;
                for(auto j : abcdn) for(auto i : r.first){
                    resl.push_back({j[0]+i[0], j[1]+i[1], j[2]+i[2], j[3]+i[3], j[4]*i[4]});
                }
                return make_pair(resl, r.second);
            } else {
                if(s[las+1] == '-') for(auto &i : r.first) for(auto &j : i) j = -j;
                for(auto i : r.second) r.first.push_back(i);
                return make_pair(abcdn, r.first);
            }
        } else {
            int las = n-1;
            for(int i = 1; i < n; i++){
                if(s[i] < '0' || '9' < s[i]){
                    las = i-1;
                    break;
                }
            }
            
            array<long long, 5> abcdn = {0, 0, 0, 0, 0};
            for(int j = 0; j <= las; j++) abcdn[4] += pow(10, las-j) * (s[j] - '0');
            
            if(las == n-1) return make_pair(vector<array<long long, 5>>{abcdn}, vector<array<long long, 5>>());
            
            auto r = self(self, s.substr(las+2));
            if(s[las+1] == '*'){
                vector<array<long long, 5>> resl;
                for(auto i : r.first){
                    resl.push_back({abcdn[0]+i[0], abcdn[1]+i[1], abcdn[2]+i[2], abcdn[3]+i[3], abcdn[4]*i[4]});
                }
                return make_pair(resl, r.second);
            } else {
                if(s[las+1] == '-') for(auto &i : r.first) for(auto &j : i) j = -j;
                for(auto i : r.second) r.first.push_back(i);
                return make_pair(vector<array<long long, 5>>{abcdn}, r.first);
            }
        }
        assert(false);
    };
    
    auto res = f(f, s);
    for(auto i : res.second) res.first.push_back(i);
    
    auto &siki = res.first;
    
    //for(auto i : siki) cout << i[0] << ' ' << i[1] << ' ' << i[2] << ' ' << i[3] << ' ' << i[4] << '\n';
    
    int ans = 0;
    for(int a = 0; a <= 9; a++){
        for(int b = 0; b <= 9; b++){
            for(int c = 0; c <= 9; c++){
                for(int d = 0; d <= 9; d++){
                    long long acc = 0;
                    for(auto i : siki){
                        acc += i[4] * power(a, i[0]) * power(b, i[1]) * power(c, i[2]) * power(d, i[3]);
                    }
                    if(acc == n) ans++;
                }
            }
        }
    }
    
    
    auto st = chrono::steady_clock::now();
    
    cout << ans << '\n';
    if(ans == 1){
        while(chrono::duration_cast<chrono::milliseconds>(chrono::steady_clock::now() - st).count() < 100);
        for(int a = 0; a <= 9; a++){
            for(int b = 0; b <= 9; b++){
                for(int c = 0; c <= 9; c++){
                    for(int d = 0; d <= 9; d++){
                        long long acc = 0;
                        for(auto i : siki){
                            acc += i[4] * power(a, i[0]) * power(b, i[1]) * power(c, i[2]) * power(d, i[3]);
                        }
                        if(acc == n) cout << a << b << c << d << '\n';
                    }
                }
            }
        }
    }
}
accuracy: 0.3
time_ms: 100
memory_kb: 3744
final_rank: 20

code_snippet: //#define _GLIBCXX_DEBUG
#include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for (ll i = 0; i < (ll)(n); i++)
#define all(a) (a).begin(), (a).end()
using ll = long long;
const ll INF32 = 2e9;
const ll INF64 = 4e18;

void printYN(bool ok){
    if(ok)cout << "Yes" << endl;
    else cout << "No" << endl;
    return;
}

ll calc(string S, int a, int b, int c, int d){
    vector<ll> E;
    rep(i, S.size()){
        if(S[i]=='('){
            string T;
            i++;
            while(S[i]!=')'){
                T.push_back(S[i]);
                i++;
            }
            E.push_back(calc(T,a,b,c,d));
            i++;
        }
        string U = "";
        while('a'<=S[i]&&S[i]<='d'){
            char ch = S[i];
            U.push_back(ch);
            i++;
        }
        rep(j, U.size()){
            if(U[j]=='a')U[j] = a+'0';
            if(U[j]=='b')U[j] = b+'0';
            if(U[j]=='c')U[j] = c+'0';
            if(U[j]=='d')U[j] = d+'0';
        }
        while('0'<=S[i]&&S[i]<='9'){
            U.push_back(S[i]);
            i++;
        }
        if(U.size()==0);
        else{
            E.push_back(stoll(U));
        }
        if(i>=S.size())break;
        if(S[i]=='+')E.push_back(INF32+(ll)'+');
        else if(S[i]=='-')E.push_back(INF32+(ll)'-');
        else if(S[i]=='*')E.push_back(INF32+(ll)'*');
    }

        vector<ll> T(1, INF32+'+');
        for(int j = 0; j < E.size(); j+=2){
            ll now = 0;
            now += E[j];
            while(j+2<E.size()&&E[j+1]==INF32+'*'){
                now*=E[j+2];
                j+=2;
            }
            T.push_back(now);
            if(j+1<E.size()){
                assert(E[j+1]=='+'+INF32||E[j+1]=='-'+INF32);
                T.push_back(E[j+1]);
            }
        }
        ll res = 0;
        for(int j = 0; j+1 < T.size(); j+=2){
            if(T[j]=='+'+INF32)res+=T[j+1];
            else if(T[j]=='-'+INF32)res-=T[j+1];
        }
    return res;
}

int main() {
    string S;
    ll N;
    cin >> S >> N;
    vector<ll> ans;
    rep(a, 10)rep(b, 10)rep(c, 10)rep(d, 10){
        if(N==calc(S,a,b,c,d))ans.push_back(1000*a+100*b+10*c+d);
    }
    if(ans.size()==1)printf("1\n%04lld\n",ans[0]);
    else cout << ans.size() << endl;
    return 0;
}
accuracy: 0.7666666666666667
time_ms: 10
memory_kb: 3624
final_rank: 16

code_snippet: #include<algorithm>
#include<iostream>
#include<cmath>
#include<cstdio>
#include<iomanip>
#include<utility>
#include<string>
#include<vector>
#include<bit>
#include<bitset>
#include<tuple>
#include<map>
#include<set>
#include<queue>
#include<deque>
#include<stack>
using namespace std;

void fix(int x){
  cout << fixed << setprecision(x);
}

int main(){
  long int N, M, ans, a1, a2, a3, a4;
  string S;
  cin >> S >> N;
  ans = 0;
  for (long int i1 = 0; i1 < 10; i1++){
  for (long int i2 = 0; i2 < 10; i2++){
  for (long int i3 = 0; i3 < 10; i3++){
  for (long int i4 = 0; i4 < 10; i4++){
    long int A = 0;
    stack<long int> ST1;
    stack<char> ST2;
    stack<long int> ST3;
    stack<char> ST4;
    ST1.push(0);
    for (long int i = 0; i < S.size(); i++){
      if (S.at(i) == 'a'){
        long int B = ST1.top() * 10 + i1;
        ST1.pop();
        ST1.push(B);
      }
      if (S.at(i) == 'b'){
        long int B = ST1.top() * 10 + i2;
        ST1.pop();
        ST1.push(B);
      }
      if (S.at(i) == 'c'){
        long int B = ST1.top() * 10 + i3;
        ST1.pop();
        ST1.push(B);
      }
      if (S.at(i) == 'd'){
        long int B = ST1.top() * 10 + i4;
        ST1.pop();
        ST1.push(B);
      }
      if (S.at(i) == '0'){
        long int B = ST1.top() * 10 + 0;
        ST1.pop();
        ST1.push(B);
      }
      if (S.at(i) == '1'){
        long int B = ST1.top() * 10 + 1;
        ST1.pop();
        ST1.push(B);
      }
      if (S.at(i) == '2'){
        long int B = ST1.top() * 10 + 2;
        ST1.pop();
        ST1.push(B);
      }
      if (S.at(i) == '3'){
        long int B = ST1.top() * 10 + 3;
        ST1.pop();
        ST1.push(B);
      }
      if (S.at(i) == '4'){
        long int B = ST1.top() * 10 + 4;
        ST1.pop();
        ST1.push(B);
      }
      if (S.at(i) == '5'){
        long int B = ST1.top() * 10 + 5;
        ST1.pop();
        ST1.push(B);
      }
      if (S.at(i) == '6'){
        long int B = ST1.top() * 10 + 6;
        ST1.pop();
        ST1.push(B);
      }
      if (S.at(i) == '7'){
        long int B = ST1.top() * 10 + 7;
        ST1.pop();
        ST1.push(B);
      }
      if (S.at(i) == '8'){
        long int B = ST1.top() * 10 + 8;
        ST1.pop();
        ST1.push(B);
      }
      if (S.at(i) == '9'){
        long int B = ST1.top() * 10 + 9;
        ST1.pop();
        ST1.push(B);
      }
      if (S.at(i) == '+'){
        ST2.push('+');
        ST1.push(0);
      }
      if (S.at(i) == '-'){
        ST2.push('-');
        ST1.push(0);
      }
      if (S.at(i) == '*'){
        ST2.push('*');
        ST1.push(0);
      }
      if (S.at(i) == '('){
        ST2.push('(');
      }
      if (S.at(i) == ')'){	
        while (ST2.top() != '('){
          if (ST2.top() == '+'){
            long int B = ST1.top();
            ST1.pop();
            ST3.push(B);
            ST4.push('+');
          }
          if (ST2.top() == '-'){
            long int B = ST1.top();
            ST1.pop();
            ST3.push(B);
            ST4.push('-');
          }
          if (ST2.top() == '*'){
            long int B = ST1.top();
            ST1.pop();
            long int C = ST1.top();
            ST1.pop();
            ST1.push(C * B);
          }
          ST2.pop();
        }
        ST2.pop();
        ST3.push(ST1.top());
        ST1.pop();
        while (!ST4.empty()){
          if (ST4.top() == '+'){
            long int B = ST3.top();
            ST3.pop();
            long int C = ST3.top();
            ST3.pop();
            ST3.push(C + B);
          }
          if (ST4.top() == '-'){
            long int B = ST3.top();
            ST3.pop();
            long int C = ST3.top();
            ST3.pop();
            ST3.push(B - C);
          }
          ST4.pop();
        }
        ST1.push(ST3.top());
      }
    }
        while (!ST2.empty()){
          if (ST2.top() == '+'){
            long int B = ST1.top();
            ST1.pop();
            ST3.push(B);
            ST4.push('+');
          }
          if (ST2.top() == '-'){
            long int B = ST1.top();
            ST1.pop();
            ST3.push(B);
            ST4.push('-');
          }
          if (ST2.top() == '*'){
            long int B = ST1.top();
            ST1.pop();
            long int C = ST1.top();
            ST1.pop();
            ST1.push(C * B);
          }
          ST2.pop();
        }
        ST3.push(ST1.top());
        ST1.pop();
        while (!ST4.empty()){
          if (ST4.top() == '+'){
            long int B = ST3.top();
            ST3.pop();
            long int C = ST3.top();
            ST3.pop();
            ST3.push(C + B);
          }
          if (ST4.top() == '-'){
            long int B = ST3.top();
            ST3.pop();
            long int C = ST3.top();
            ST3.pop();
            ST3.push(B - C);
          }
          ST4.pop();
        }
        ST1.push(ST3.top());
//cout << i1 << i2 << i3 << i4 << " " << ST1.top() << endl;
    if (ST1.top() == N){
      ans++;
      a1 = i1;
      a2 = i2;
      a3 = i3;
      a4 = i4;
    }
  }
  }
  }
  }
  cout << ans << endl;
  if (ans == 1){
    cout << a1 << a2 << a3 << a4 << endl;
  }
}
accuracy: 1
time_ms: 10
memory_kb: 3584
final_rank: 6

code_snippet: #include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for (ll i = 0; i < (ll)(n); i++)
#define ll long long
const long long mod=998244353;
const long long hmod=46216567629137;
int main(){
    cin.tie(0)->sync_with_stdio(0);
    cout.tie(0);
    string S;
    cin>>S;
    ll N;
    cin>>N;
    int ans=0;
    string ans_str="";
    for(int i=0;i<10000;i++){
        string s=to_string(i);
        int ss=s.size();
        rep(j,4-ss) s='0'+s;
        int d=s[3]-'0';
        int c=s[2]-'0';
        int b=s[1]-'0';
        int a=s[0]-'0';
        string now=S;
        for(int j=0;j<now.size();j++){//代入
            if(now[j]=='a') now[j]=a+'0';
            if(now[j]=='b') now[j]=b+'0';
            if(now[j]=='c') now[j]=c+'0';
            if(now[j]=='d') now[j]=d+'0';
        }
        vector<string>dat;
        int num=-1;
        for(int j=0;j<now.size();j++){//分解
            if('0'<=now[j] && now[j]<='9'){
                if(num==-1) num=now[j]-'0';
                else num=num*10+now[j]-'0';
            }
            else{
                if(num!=-1) dat.push_back(to_string(num));
                string a="";
                a+=now[j];
                dat.push_back(a);
                num=-1;
            }
        }
        if(num!=-1) dat.push_back(to_string(num));
        vector<string>nex;
        while(dat.size()>=2){
            rep(j,dat.size()){
                if(dat[j]=="+"){
                    if(j<dat.size()-2 && dat[j+2]=="*"){
                        nex.push_back(dat[j]);
                        continue;
                    }
                    if(nex.size()>=2 && nex[nex.size()-2]=="*"){
                        nex.push_back(dat[j]);
                        continue;
                    }
                    if(nex[nex.size()-1]!=")" && dat[j+1]!="("){
                        ll a=stoll(nex[nex.size()-1])+stoll(dat[j+1]);
                        nex.pop_back();
                        nex.push_back(to_string(a));
                        j++;
                    }
                    else nex.push_back(dat[j]);
                }
                else if(dat[j]=="-"){
                    if(j<dat.size()-2 && dat[j+2]=="*"){
                        nex.push_back(dat[j]);
                        continue;
                    }
                    if(nex.size()>=2 && nex[nex.size()-2]=="*"){
                        nex.push_back(dat[j]);
                        continue;
                    }
                    if(nex[nex.size()-1]!=")" && dat[j+1]!="("){
                        ll a=stoll(nex[nex.size()-1])-stoll(dat[j+1]);
                        nex.pop_back();
                        nex.push_back(to_string(a));
                        j++;
                    }
                    else nex.push_back(dat[j]);
                }
                else if(dat[j]=="*"){
                    if(nex[nex.size()-1]!=")" && dat[j+1]!="("){
                        ll a=stoll(nex[nex.size()-1])*stoll(dat[j+1]);
                        nex.pop_back();
                        nex.push_back(to_string(a));
                        j++;
                    }
                    else nex.push_back(dat[j]);
                }
                else{
                    nex.push_back(dat[j]);
                }
            }
            dat.clear();
            rep(j,nex.size()) dat.push_back(nex[j]);
            nex.clear();
            rep(j,dat.size()){
                if(nex.size()==0 ||j==dat.size()-1){
                    nex.push_back(dat[j]);
                    continue;
                }
                if(nex[nex.size()-1]=="(" && dat[j+1]==")"){
                    nex.pop_back();
                    nex.push_back(dat[j]);
                    j++;
                }
                else nex.push_back(dat[j]);
            }
            dat.clear();
            rep(j,nex.size()) dat.push_back(nex[j]);
            nex.clear();
        }
        ll cnt=stoll(dat[0]);
        if(cnt==N){
            ans++;
            ans_str=s;
        }
    }
    cout<<ans<<"\n";
    if(ans==1) cout<<ans_str<<"\n";
}
accuracy: 0.5333333333333333
time_ms: 20
memory_kb: 3540
final_rank: 17

code_snippet: #include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for (ll i = 0; i < (ll)(n); i++)
#define ll long long
const long long mod=998244353;
const long long hmod=46216567629137;
int main(){
    cin.tie(0)->sync_with_stdio(0);
    cout.tie(0);
    string S;
    cin>>S;
    ll N;
    cin>>N;
    int ans=0;
    string ans_str="";
    for(int i=0;i<10000;i++){
        string s=to_string(i);
        int ss=s.size();
        rep(j,4-ss) s='0'+s;
        int d=s[3]-'0';
        int c=s[2]-'0';
        int b=s[1]-'0';
        int a=s[0]-'0';
        string now=S;
        for(int j=0;j<now.size();j++){
            if(now[j]=='a') now[j]=a+'0';
            if(now[j]=='b') now[j]=b+'0';
            if(now[j]=='c') now[j]=c+'0';
            if(now[j]=='d') now[j]=d+'0';
        }
        vector<string>dat;
        int num=-1;
        for(int j=0;j<now.size();j++){
            if('0'<=now[j] && now[j]<='9'){
                if(num==-1) num=now[j]-'0';
                else num=num*10+now[j]-'0';
            }
            else{
                if(num!=-1) dat.push_back(to_string(num));
                string a="";
                a+=now[j];
                dat.push_back(a);
                num=-1;
            }
        }
        if(num!=-1) dat.push_back(to_string(num));
        vector<string>nex;
        rep(j,dat.size()){
            if(dat[j]=="*"){
                if(dat[j-1]!=")" && dat[j+1]!="("){
                    nex.pop_back();
                    nex.push_back(to_string(stoll(dat[j-1])*stoll(dat[j+1])));
                    j++;
                }
                else{
                    nex.push_back(dat[j]);
                }
            }
            else{
                nex.push_back(dat[j]);
            }
        }
        dat.clear();
        rep(j,nex.size()) dat.push_back(nex[j]);
        nex.clear();
        rep(j,dat.size()){
            if(dat[j]=="+"){
                if(dat[j-1]!=")" && dat[j+1]!="("){
                    ll n=stoll(nex[nex.size()-1])+stoll(dat[j+1]);
                    nex.pop_back();
                    nex.push_back(to_string(n));
                    j++;
                }
                else nex.push_back(dat[j]);
            }
            else if(dat[j]=="-"){
                if(dat[j-1]!=")" && dat[j+1]!="("){
                    ll n=stoll(nex[nex.size()-1])-stoll(dat[j+1]);
                    nex.pop_back();
                    nex.push_back(to_string(n));
                    j++;
                }
                else nex.push_back(dat[j]);
            }
            else nex.push_back(dat[j]);
            
        }
        dat.clear();
        rep(j,nex.size()) dat.push_back(nex[j]);
        nex.clear();
        rep(j,dat.size()){
            if(dat[j]!="(" && dat[j]!=")") nex.push_back(dat[j]);
        }
        dat.clear();
        rep(j,nex.size()) dat.push_back(nex[j]);
        nex.clear();
        rep(j,dat.size()){
            if(dat[j]=="*"){
                ll a=stoll(nex[nex.size()-1])*stoll(dat[j+1]);
                nex.pop_back();
                nex.push_back(to_string(a));
                j++;
            }
            else nex.push_back(dat[j]);
        }
        dat.clear();
        rep(j,nex.size()) dat.push_back(nex[j]);
        nex.clear();
        rep(j,dat.size()){
            if(dat[j]=="+"){
                ll a=stoll(nex[nex.size()-1])+stoll(dat[j+1]);
                nex.pop_back();
                nex.push_back(to_string(a));
                j++;
            }
            else if(dat[j]=="-"){
                ll a=stoll(nex[nex.size()-1])-stoll(dat[j+1]);
                nex.pop_back();
                nex.push_back(to_string(a));
                j++;
            }
            else nex.push_back(dat[j]);
        }
        ll cnt=stoll(nex[0]);
        if(cnt==N){
            ans++;
            ans_str+=(a+'0');
            ans_str+=(b+'0');
            ans_str+=(c+'0');
            ans_str+=(d+'0');
        }
    }
    cout<<ans<<"\n";
    if(ans==1) cout<<ans_str<<"\n";
}
accuracy: 0.5
time_ms: 10
memory_kb: 3632
final_rank: 19

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

#pragma GCC target("avx2")
#pragma GCC optimize("O3")
#pragma GCC optimize("unroll-loops")

#define REP(i, n) for (ll i = 0; i < (n); i++)
#define REP2(i, a, b) for (ll i = (a); i < (b); i++)
using namespace std;
using ll = long long;
template <class T> using V = vector<T>;


string S;
int it = 0;
int len;

V<V<V<V<ll>>>> nums(int& it);
V<V<V<V<ll>>>> muls(int& it);
V<V<V<V<ll>>>> adds(int& it);
V<V<V<V<ll>>>> factors(int& it);

V<V<V<V<ll>>>> nums(int& it) {
	V<V<V<V<ll>>>> params(8, V<V<V<ll>>>(8, V<V<ll>>(8, V<ll>(8, 0))));
	if ('a' <= S[it] and S[it] <= 'd') {
		while ('a' <= S[it] and S[it] <= 'd') {
			params[1][0][0][0] *= 10;
			params[0][1][0][0] *= 10;
			params[0][0][1][0] *= 10;
			params[0][0][0][1] *= 10;
			if (S[it] == 'a') params[1][0][0][0] += 1;
			if (S[it] == 'b') params[0][1][0][0] += 1;
			if (S[it] == 'c') params[0][0][1][0] += 1;
			if (S[it] == 'd') params[0][0][0][1] += 1;
			it++;
		}
		return params;
	} else {
		ll ret{};
		while (isdigit(S[it])) {
			ret *= 10;
			ret += S[it] - '0';
			it++;
		}
		params[0][0][0][0] = ret;
		return params;
	}
}


V<V<V<V<ll>>>> muls(int& it) {
	auto res = factors(it);
	for (; it < len; ) {
		if (S[it] == '*') {
			auto tmp = factors(++it);
			auto nres = V<V<V<V<ll>>>>(8, V<V<V<ll>>>(8, V<V<ll>>(8, V<ll>(8, 0))));
			REP(a1, 4) REP(b1, 4) REP(c1, 4) REP(d1, 4) {
				REP(a2, 4) REP(b2, 4) REP(c2, 4) REP(d2, 4) {
					nres[a1 + a2][b1 + b2][c1 + c2][d1 + d2] += res[a1][b1][c1][d1] * tmp[a2][b2][c2][d2];
				}
			}
			res = nres;
		} else {
			break;
		}
	}
	return res;
}

V<V<V<V<ll>>>> adds(int& it) {
	auto res = muls(it);
	for (; it < len; ) {
		if (S[it] == '+') {
			auto tmp = muls(++it);
			REP(a1, 4) REP(b1, 4) REP(c1, 4) REP(d1, 4) {
				res[a1][b1][c1][d1] += tmp[a1][b1][c1][d1];				
			}
		} else if (S[it] == '-') {
			auto tmp = muls(++it);
			REP(a1, 4) REP(b1, 4) REP(c1, 4) REP(d1, 4) {
				res[a1][b1][c1][d1] -= tmp[a1][b1][c1][d1];				
			}
		} else {
			break;
		}
	}
	return res;
}

V<V<V<V<ll>>>> factors(int& it) {
	if (S[it] == '(') {
		auto ret = adds(++it);
		it++;
		return ret;
	} else {
		return nums(it);
	}
}

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

	cin >> S;
	len = S.length();
	ll n; cin >> n;
	auto res = adds(it);

	ll cnt{}, ans{};
	REP2(a, 0, 10) REP2(b, 0, 10) REP2(c, 0, 10) REP2(d, 0, 10) {
		ll sum{};
		REP(ac, 4) REP(bc, 4) REP(cc, 4) REP(dc, 4) {
			ll mu{1};
			mu *= res[ac][bc][cc][dc];
			if (ac > 0) mu *= pow(a, ac);
			if (bc > 0) mu *= pow(b, bc);
			if (cc > 0) mu *= pow(c, cc);
			if (dc > 0) mu *= pow(d, dc);
			sum += mu;
		}
		if (sum == n) {
			ans = a * 1000 + b * 100 + c * 10 + d;
			cnt++;
		}
	}
	cout << cnt << endl;
	if (cnt == 1) {
		printf("%04lld\n", ans);
	}
}
accuracy: 1
time_ms: 130
memory_kb: 4192
final_rank: 14

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

#pragma GCC target("avx2")
#pragma GCC optimize("O3")
#pragma GCC optimize("unroll-loops")

#define REP(i, n) for (int i = 0; i < (n); i++)
#define REP2(i, a, b) for (int i = (a); i < (b); i++)
using namespace std;
using ll = long long;
template <class T> using V = vector<T>;


string S;
int it = 0;
int len;

V<V<V<V<ll>>>> nums(int& it);
V<V<V<V<ll>>>> muls(int& it);
V<V<V<V<ll>>>> adds(int& it);
V<V<V<V<ll>>>> factors(int& it);

V<V<V<V<ll>>>> nums(int& it) {
	// cerr << "nums" << endl;
	V<V<V<V<ll>>>> params(8, V<V<V<ll>>>(8, V<V<ll>>(8, V<ll>(8, 0))));
	if ('a' <= S[it] and S[it] <= 'd') {
		while ('a' <= S[it] and S[it] <= 'd') {
			params[1][0][0][0] *= 10;
			params[0][1][0][0] *= 10;
			params[0][0][1][0] *= 10;
			params[0][0][0][1] *= 10;
			if (S[it] == 'a') params[1][0][0][0] += 1;
			if (S[it] == 'b') params[0][1][0][0] += 1;
			if (S[it] == 'c') params[0][0][1][0] += 1;
			if (S[it] == 'd') params[0][0][0][1] += 1;
			it++;
		}
		return params;
	} else {
		ll ret{};
		while (isdigit(S[it])) {
			ret *= 10;
			ret += S[it] - '0';
			it++;
		}
		params[0][0][0][0] = ret;
		return params;
	}
}


V<V<V<V<ll>>>> muls(int& it) {
	// cerr << "muls" << endl;
	auto res = factors(it);
	// cerr << res[0][0][0][0] << endl;
	for (; it < len; ) {
		if (S[it] == '*') {
			auto tmp = factors(++it);
			auto nres = V<V<V<V<ll>>>>(8, V<V<V<ll>>>(8, V<V<ll>>(8, V<ll>(8, 0))));
			REP(a1, 4) REP(b1, 4) REP(c1, 4) REP(d1, 4) {
				REP(a2, 4) REP(b2, 4) REP(c2, 4) REP(d2, 4) {
					nres[a1 + a2][b1 + b2][c1 + c2][d1 + d2] += res[a1][b1][c1][d1] * tmp[a2][b2][c2][d2];
				}
			}
			res = nres;
		} else {
			break;
		}
	}
	return res;
}
V<V<V<V<ll>>>> adds(int& it) {
	// cerr << "adds" << endl;
	auto res = muls(it);
	for (; it < len; ) {
		if (S[it] == '+') {
			auto tmp = muls(++it);
			REP(a1, 4) REP(b1, 4) REP(c1, 4) REP(d1, 4) {
				res[a1][b1][c1][d1] += tmp[a1][b1][c1][d1];				
			}
		} else if (S[it] == '-') {
			auto tmp = muls(++it);
			REP(a1, 4) REP(b1, 4) REP(c1, 4) REP(d1, 4) {
				res[a1][b1][c1][d1] -= tmp[a1][b1][c1][d1];				
			}
		} else {
			break;
		}
	}
	return res;
}
V<V<V<V<ll>>>> factors(int& it) {
	// cerr << "factors" << endl;
	if (S[it] == '(') {
		auto ret = adds(++it);
		it++;
		return ret;
	} else {
		return nums(it);
	}
}

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

	cin >> S;
	len = S.length();
	ll n; cin >> n;
	auto res = adds(it);

	ll cnt{}, ans{};
	// REP(a1, 4) REP(b1, 4) REP(c1, 4) REP(d1, 4) cout << a1 << " " << b1 << " " << c1 << " " << d1 << " " << res[a1][b1][c1][d1] << endl;
	REP2(a, 0, 10) REP2(b, 0, 10) REP2(c, 0, 10) REP2(d, 0, 10) {
		ll sum{};
		REP(ac, 4) REP(bc, 4) REP(cc, 4) REP(dc, 4) {
			ll mu{1};
			mu *= res[ac][bc][cc][dc];
			if (ac > 0) mu *= pow(a, ac);
			if (bc > 0) mu *= pow(b, bc);
			if (cc > 0) mu *= pow(c, cc);
			if (dc > 0) mu *= pow(d, dc);
			sum += mu;
		}
		if (sum == n) {
			ans = a * 1000 + b * 100 + c * 10 + d;
			cnt++;
		}
	}
	cout << cnt << endl;
	if (cnt == 1) {
		printf("%04lld\n", ans);
	}
}
accuracy: 1
time_ms: 140
memory_kb: 4200
final_rank: 15

code_snippet: #include<bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<int,int> iip;
#define rep(i,n) for(int i=0;i<n;i++)
#define rep2(i,l,n) for(int i=l;i<n;i++)
int inf=(1<<30);
ll strnum(string n,int a,int b,int c,int d){
    ll ret=0,r=1;
    for(int i=(int)n.size()-1;i>=0;i--){
        ret+=(n[i]=='a'?a:n[i]=='b'?b:n[i]=='c'?c:n[i]=='d'?d:n[i]-'0')*r;
        r*=10;
    }
    return ret;
}
ll kakko(int &i,string const &s,int a,int b,int c,int d);
ll kou(int &i,string const &s,int a,int b,int c,int d){
    ll ret=1;
    string n="";
    do{
        i++;
        if(s[i]=='(')ret*=kakko(i,s,a,b,c,d),i++;
        else{n="";
        while(s[i]=='a'||s[i]=='b'||s[i]=='c'||s[i]=='d'||('0'<=s[i]&&s[i]<='9')){
            n+=s[i];
            i++;
        }
        //cout<<n<<endl;
        ret*=strnum(n,a,b,c,d);}
    }while(s[i]=='*');
    return ret;
}
ll kakko(int &i,string const &s,int a,int b,int c,int d){
    ll ret=0;
    while(s[i]=='+'||s[i]=='-'||s[i]=='('){
        ret+=(s[i]=='-'?(-1):1)*kou(i,s,a,b,c,d);
    }
    return ret;
}
int main(){
    string s;ll n;cin>>s>>n;s="("+s+")";
    int ans=0;
    vector<int>abcd(4);
    rep(a,10)rep(b,10)rep(c,10)rep(d,10){
        int i=0;
        if(kakko(i,s,a,b,c,d)==n)ans++,abcd={a,b,c,d};
    }
    cout<<ans<<endl;
    if(ans==1)cout<<abcd[0]<<abcd[1]<<abcd[2]<<abcd[3]<<endl;
    /*int a,b,c,d;cin>>a>>b>>c>>d;
    int i=0;
    cout<<kakko(i,s,a,b,c,d)<<endl;*/
}
accuracy: 1
time_ms: 10
memory_kb: 3452
final_rank: 1

code_snippet: #pragma GCC target("avx2")
#pragma GCC optimize("O3")
#pragma GCC optimize("unroll-loops")
#include<bits/stdc++.h>
#define rep(i,n) for(ll i=0;i<(n);++i)
#define reps(i,n) for(ll i=1;i<=(n);++i)
#define repr(i,n) for(ll i=2;i*i<=(n);++i)
#define ll long long
#define all(x) (x).begin(),(x).end()
#define sz(x) ((string)(x).size())
#define pb push_back
#define pob pop_back()
#define setp(x) setprecision((ll)(x))
#define mp make_pair
using namespace std;
const ll inf=1e18;

string s;
ll n;

ll f(int i,int j,int k,int l,int &a){
  ll res=0;
  stack<ll> b,c;
  ll x=0;
  while(s[a]!=')'&&a!=s.size()){
    if(s[a]=='(') {
      a++;
      x=f(i,j,k,l,a);
    }
    else if(s[a]=='a') x=x*10+i;
    else if(s[a]=='b') x=x*10+j;
    else if(s[a]=='c') x=x*10+k;
    else if(s[a]=='d') x=x*10+l;
    else if((s[a]-'0')>=0&&(s[a]-'0')<=9) x=x*10+(int)(s[a]-'0');
    else {
      if(!b.empty()&&b.top()==2){
        ll u=c.top();
        c.pop();
        x*=u;
        b.pop();
      }
      c.push(x);
      if(s[a]=='+') b.push(0);
      else if(s[a]=='-') b.push(1);
      else b.push(2);
      x=0;
    }
    a++;
  }
  if(!b.empty()&&b.top()==2){
    ll u=c.top();
    c.pop();
    x*=u;
    b.pop();
  }
  c.push(x);
  while(!b.empty()){
    if(b.top()==0) res+=c.top();
    else res-=c.top();
    b.pop();
    c.pop();
  }
  res+=c.top();
  return res;
}

int main(){
  cin>>s>>n;
  vector<string> p;
  rep(i,10)rep(j,10)rep(k,10)rep(l,10){
    int d=0;
    ll x=f(i,j,k,l,d);
    if(x==n) {
      string t;
      t+=(char)(i+'0');
      t+=(char)(j+'0');
      t+=(char)(k+'0');
      t+=(char)(l+'0');
      p.pb(t);
    }
  }
  if(p.size()!=1) cout<<p.size()<<endl;
  else {
    cout<<1<<endl;
    cout<<p[0]<<endl;
  }
}
accuracy: 1
time_ms: 20
memory_kb: 3716
final_rank: 9

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

#define rep(i, n) for (int i = 0; i < (int)(n); ++i)
#define rrep(i, n) for (int i = (int)(n) - 1; i >= 0; --i)
#define all(v) begin(v), end(v)

using ll = long long;

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

using namespace std;

ll calc(string);

int main() {
    string S; cin >> S;
    ll N; cin >> N;
    vector<string> ans;
    rep (a, 10) rep (b, 10) rep (c, 10) rep (d, 10) {
        string T = S;
        rep (i, T.size()) {
            if (T[i] == 'a') T[i] = a + '0';
            else if (T[i] == 'b') T[i] = b + '0';
            else if (T[i] == 'c') T[i] = c + '0';
            else if (T[i] == 'd') T[i] = d + '0';
        }
        if (calc(T) == N) {
            string res(4, '0');
            res[0] += a;
            res[1] += b;
            res[2] += c;
            res[3] += d;
            ans.push_back(res);
        }
    }
    cout << ans.size() << endl;
    if (ans.size() == 1) cout << ans[0] << endl;
}

ll calc(string S) {
    S = '(' + S + ')';
    int N = S.size();
    vector<int> LR(N, -1);
    vector<ll> V(N);
    rep (i, N) {
        if ('0' <= S[i] && S[i] <= '9') {
            int r = i;
            ll v = 0;
            while (r < N && '0' <= S[r] && S[r] <= '9') {
                v = v * 10 + (S[r] - '0');
                ++r;
            }
            LR[i] = r - 1;
            LR[r - 1] = i;
            V[i] = v;
            i = r;
        }
    }
    rrep (bl, N) {
        if (S[bl] == '(') {
            for (int i = bl + 1; S[i] != ')'; ) {
                if (LR[i] != -1) i = LR[i] + 1;
                else {
                    if (S[i] == '*') {
                        int l = LR[i - 1], r = LR[i + 1];
                        ll v = V[l] * V[i + 1];
                        V[l] = v;
                        LR[l] = r;
                        LR[r] = l;
                        i = r + 1;
                    }
                    else ++i;
                }
            }
            for (int i = bl + 1; S[i] != ')'; ) {
                if (LR[i] != -1) i = LR[i] + 1;
                else {
                    if (S[i] == '+') {
                        int l = LR[i - 1], r = LR[i + 1];
                        ll v = V[l] + V[i + 1];
                        V[l] = v;
                        LR[l] = r;
                        LR[r] = l;
                        i = r + 1;
                    }
                    else if (S[i] == '-') {
                        int l = LR[i - 1], r = LR[i + 1];
                        ll v = V[l] - V[i + 1];
                        V[l] = v;
                        LR[l] = r;
                        LR[r] = l;
                        i = r + 1;
                    }
                    else ++i;
                }
            }
            V[bl] = V[bl + 1];
            int br = LR[bl + 1] + 1;
            LR[bl] = br;
            LR[br] = bl;
        }
    }
    return V[0];
}
accuracy: 1
time_ms: 10
memory_kb: 3668
final_rank: 8

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

vector<string> RPN(string S) {
	vector<string> A;
	vector<stack<int>> st(1);
	/* Only four arithmetic operations */
	string support = "0123456789+-*/()";
	for (int i = 0; i < (int) S.size(); i++){
		if (support.find(S[i]) == string::npos){
			continue;
		}
		if (S[i] == '('){
			st.resize(st.size() + 1);
		} else if (S[i] == ')'){
			while (!st.back().empty()){
				A.resize(A.size() + 1);
				A.back().push_back(st.back().top());
				st.back().pop();
			}
			st.pop_back();
		} else {
			if (isdigit(S[i])){
				if (i == 0 or !isdigit(S[i - 1])){
					A.resize(A.size() + 1);
				}
				A.back().push_back(S[i]);
			} else {
				if ((int) st.back().size() >= 1 and (S[i] == '+' or S[i] == '-')){
					while (!st.back().empty()){
						A.resize(A.size() + 1);
						A.back().push_back(st.back().top());
						st.back().pop();
					}
				}
				st.back().push(S[i]);
			}
		}
	}
	while (!st.back().empty()){
		A.resize(A.size() + 1);
		A.back().push_back(st.back().top());
		st.back().pop();
	}
	return A;
}

int main() {
	string S;
	cin >> S;
	long long N;
	cin >> N;
	vector<tuple<int, int, int, int>> ans;
	for (int a = 0; a < 10; a++){
		for (int b = 0; b < 10; b++){
			for (int c = 0; c < 10; c++){
				for (int d = 0; d < 10; d++){
					string S2 = S;
					for (int i = 0; i < (int) S.size(); i++){
						if (S[i] == 'a'){
							S2[i] = '0' + a;
						}
						if (S[i] == 'b'){
							S2[i] = '0' + b;
						}
						if (S[i] == 'c'){
							S2[i] = '0' + c;
						}
						if (S[i] == 'd'){
							S2[i] = '0' + d;
						}
					}
					auto R = RPN(S2);
					stack<long long> st;
					for (string s : R){
						if (s == "+"){
							long long r = st.top();
							st.pop();
							long long l = st.top();
							st.pop();
							st.push(l + r);
						} else if (s == "-"){
							long long r = st.top();
							st.pop();
							long long l = st.top();
							st.pop();
							st.push(l - r);
						} else if (s == "*"){
							long long r = st.top();
							st.pop();
							long long l = st.top();
							st.pop();
							st.push(l * r);
						} else {
							st.push(stoi(s));
						}
					}
					if (st.top() == N){
						ans.push_back({a, b, c, d});
					}
				}
			}
		}
	}
	cout << ans.size() << '\n';
	if ((int) ans.size() == 1){
		auto [a, b, c, d] = ans[0];
		cout << a << b << c << d << '\n';
	}
}
accuracy: 1
time_ms: 60
memory_kb: 3580
final_rank: 11

code_snippet: #include <bits/stdc++.h>
using namespace std;
#define ll long long
#define gcd(x,y) __gcd(x,y) //最大公約数
#define lcm(x,y) x/(__gcd(x,y))*y //最小公倍数

int main() {
    string s, n;
    cin >> s >> n;
    vector<string> ve;
    stack<string> st;
    vector<string> ans;
    string t = "", u = "";
    for (int i = 0; i < s.size(); i++) {
        if (s[i] == 'a' || s[i] == 'b' || s[i] == 'c' || s[i] == 'd') {
            if (u != "") {
                ve.push_back(u);
                u = "";
            }
            t += s[i];
        } else if ('0' <= s[i] && s[i] <= '9') {
            if (t != "") {
                ve.push_back(t);
                t = "";
            }
            u += s[i];
        } else {
            if (t != "") {
                ve.push_back(t);
                t = "";
            }
            if (u != "") {
                ve.push_back(u);
                u = "";
            }
            if (s[i] == '(') {
                st.push({s[i]});
            } else if (s[i] == ')') {
                while (st.top() != "(") {
                    ve.push_back(st.top());
                    st.pop();
                }
                st.pop();
            } else {
                if (s[i] == '*' || s[i] == '/') {
                    while (!st.empty() && st.top() == "*" && st.top() == "/") {
                        ve.push_back(st.top());
                        st.pop();
                    }
                    st.push({s[i]});
                } else if (s[i] == '+' || s[i] == '-') {
                    while (!st.empty() && st.top() != "(" && st.top() != ")") {
                        ve.push_back(st.top());
                        st.pop();
                    }
                    st.push({s[i]});
                }
            }
        }
    }
    if (t != "") {
      ve.push_back(t);
    }
    if (u != "") {
      ve.push_back(u);
    }
    while (!st.empty()) {
        ve.push_back(st.top());
        st.pop();
    }
    for (int a = 0; a < 10; a++) {
        for (int b = 0; b < 10; b++) {
            for (int c = 0; c < 10; c++) {
                for (int d = 0; d < 10; d++) {
                    stack<string> st;
                    for (int i = 0; i < ve.size(); i++) {
                        if (ve[i] != "+" && ve[i] != "-" && ve[i] != "*" && ve[i] != "/") {
                            string num = ve[i];
                            if ('a' <= ve[i][0] && ve[i][0] <= 'd') {
                                num = "";
                                for (int j = 0; j < ve[i].size(); j++) {
                                    if (ve[i][j] == 'a') {
                                        num += '0' + a;
                                    } else if (ve[i][j] == 'b') {
                                        num += '0' + b;
                                    } else if (ve[i][j] == 'c') {
                                        num += '0' + c;
                                    } else if (ve[i][j] == 'd') {
                                        num += '0' + d;
                                    }
                                }
                            }
                            st.push(num);
                        } else {
                            string ys = st.top();
                            st.pop();
                            string xs = st.top();
                            st.pop();
                            long long x = stoll(xs), y = stoll(ys);
                            if (ve[i] == "+") {
                                x += y;
                            } else if (ve[i] == "-") {
                                x -= y;
                            } else if (ve[i] == "*") {
                                x *= y;
                            } else if (ve[i] == "/") {
                                x /= y;
                            }
                            st.push(to_string(x));
                        }
                    }
                    if (st.top() == n) {
                        t = "";
                        t += a + '0';
                        t += b + '0';
                        t += c + '0';
                        t += d + '0';
                        ans.push_back(t);
                    }
                    st.pop();
                }
            }
        }
    }
    cout << ans.size() << "\n";
    if (ans.size() == 1) {
      cout << ans[0] << "\n";
    }
    return(0);
}
accuracy: 1
time_ms: 130
memory_kb: 3876
final_rank: 13

code_snippet: #include <bits/stdc++.h>
using namespace std;
#define ll long long
#define gcd(x,y) __gcd(x,y) //最大公約数
#define lcm(x,y) x/(__gcd(x,y))*y //最小公倍数

int main() {
    string s, n;
    cin >> s >> n;
    vector<string> ve;
    stack<string> st;
    vector<string> ans;
    string t = "", u = "";
    for (int i = 0; i < s.size(); i++) {
        if (s[i] == 'a' || s[i] == 'b' || s[i] == 'c' || s[i] == 'd') {
            if (u != "") {
                ve.push_back(u);
                u = "";
            }
            t += s[i];
        } else if ('0' <= s[i] && s[i] <= '9') {
            if (t != "") {
                ve.push_back(t);
                t = "";
            }
            u += s[i];
        } else {
            if (t != "") {
                ve.push_back(t);
                t = "";
            }
            if (u != "") {
                ve.push_back(u);
                u = "";
            }
            if (s[i] == '(') {
                st.push({s[i]});
            } else if (s[i] == ')') {
                while (st.top() != "(") {
                    ve.push_back(st.top());
                    st.pop();
                }
                st.pop();
            } else {
                if (s[i] == '*' || s[i] == '/') {
                    if (!st.empty() && st.top() == "*" && st.top() != "/") {
                        ve.push_back(st.top());
                        st.pop();
                    }
                    st.push({s[i]});
                } else if (s[i] == '+' || s[i] == '-') {
                    if (!st.empty() && st.top() != "(" && st.top() != ")") {
                        ve.push_back(st.top());
                        st.pop();
                    }
                    st.push({s[i]});
                }
            }
        }
    }
    if (t != "") {
      ve.push_back(t);
    }
    if (u != "") {
      ve.push_back(u);
    }
    while (!st.empty()) {
        ve.push_back(st.top());
        st.pop();
    }
    for (int a = 0; a < 10; a++) {
        for (int b = 0; b < 10; b++) {
            for (int c = 0; c < 10; c++) {
                for (int d = 0; d < 10; d++) {
                    stack<string> st;
                    for (int i = 0; i < ve.size(); i++) {
                        if (ve[i] != "+" && ve[i] != "-" && ve[i] != "*" && ve[i] != "/") {
                            string num = ve[i];
                            if ('a' <= ve[i][0] && ve[i][0] <= 'd') {
                                num = "";
                                for (int j = 0; j < ve[i].size(); j++) {
                                    if (ve[i][j] == 'a') {
                                        num += '0' + a;
                                    } else if (ve[i][j] == 'b') {
                                        num += '0' + b;
                                    } else if (ve[i][j] == 'c') {
                                        num += '0' + c;
                                    } else if (ve[i][j] == 'd') {
                                        num += '0' + d;
                                    }
                                }
                            }
                            st.push(num);
                        } else {
                            string ys = st.top();
                            st.pop();
                            string xs = st.top();
                            st.pop();
                            long long x = stoll(xs), y = stoll(ys);
                            if (ve[i] == "+") {
                                x += y;
                            } else if (ve[i] == "-") {
                                x -= y;
                            } else if (ve[i] == "*") {
                                x *= y;
                            } else if (ve[i] == "/") {
                                x /= y;
                            }
                            st.push(to_string(x));
                        }
                    }
                    if (st.top() == n) {
                        t = "";
                        t += a + '0';
                        t += b + '0';
                        t += c + '0';
                        t += d + '0';
                        ans.push_back(t);
                    }
                    st.pop();
                }
            }
        }
    }
    cout << ans.size() << "\n";
    if (ans.size() == 1) {
      cout << ans[0] << "\n";
    }
    return(0);
}
accuracy: 0.5333333333333333
time_ms: 10
memory_kb: 3748
final_rank: 18

code_snippet: //パスワード

#include <stdio.h>
#include <iostream>
#include <vector>
#include <set>
#include <functional>
#include <queue>
#include <numeric>
#include <queue>
#include <stack>
#include <math.h>
using ull = unsigned long long;
using ll = unsigned long long;
bool IsAlphabet(char c){
    return 'a' <= c && c <= 'z';
}
bool IsNumber(char c){
    return '0' <= c && c <= '9';
}
ll GetDigit(ll value){
    return std::log10(value) + 1;
}
int GetOperatorPriority(char c){
    switch(c){
        case '+':
        case '-':
            return 0;
        case '*':
            return 1;
        case '&'://結合演算子
            return 2;
        case '(':
            return -1;
    }
    throw;
}
auto GetOperator(char c) -> std::function<ll(ll , ll)>{
    switch(c){
        case '+':
            return [](ll left , ll right){return left + right;};
        case '-':
            return [](ll left , ll right){return left - right;};
        case '*':
            return [](ll left , ll right){return left * right;};
        /*
        case '&'://結合演算子
            return [](int left , int right){return left * (int)std::pow(10 , GetDigit(right)) + right;};
        */
    }
    throw;
}
std::string SwapUniqueOperator(const std::string& str){
    std::string result;
    for(int i = 0; i < str.length(); i++){
        if(i != 0 && IsAlphabet(str[i - 1]) && IsAlphabet(str[i])) result += '&';
        result += str[i];
    }
    return std::move(result);
}
std::string CastRPN(const std::string& str){
    std::string result;
    std::queue<std::string> operands;
    std::stack<char> operators;
    auto StackFlush = [&](bool AllFlag = false){
        while(!operands.empty()){
            //if(!AllFlag && operands.front() == "(")
                //break;
            result += operands.front();
            result += ' ';
            operands.pop();
        }
        while(!operators.empty()){
            if(!AllFlag && operators.top() == '(') 
                break;
            result += operators.top();
            result += ' ';
            operators.pop();
        }
    };
    for(int i = 0; i < str.length(); i++){
        if(IsNumber(str[i])){
            std::string nums;
            nums += str[i];
            while(i + 1 < str.length() && IsNumber(str[i + 1])){
                nums += str[i + 1];
                i++;
            }
            operands.push(std::move(nums));
        }else if(IsAlphabet(str[i])){
            std::string vals;
            vals += str[i];
            while(i + 1 < str.length() && IsAlphabet(str[i + 1])){
                vals += str[i + 1];
                i++;
            }
            operands.push(std::move(vals));
        }else if(str[i] == '('){
            operators.push('(');
            //operands.push("(");
        }else if(str[i] == ')'){
            StackFlush();
            operators.pop();
            //operands.pop();
        }else{//operator
            if(!operators.empty() && GetOperatorPriority(str[i]) <= GetOperatorPriority(operators.top()))
                StackFlush();
            operators.push(str[i]);
        }
    }
    StackFlush(true);
    return std::move(result);
}
ll solve(const std::string &str , const int (&values)[4]){
    auto GetAlphabetIndex = [](char c){
        return c - 'a';
    };
    std::stack<ll> operands;
    int ZeroFlag = 0;
    for(int i = 0; i < str.length(); i++){
        if(IsAlphabet(str[i])){
            int index = GetAlphabetIndex(str[i]);
            operands.push(values[index]);
            while(i + 1 < str.length() && IsAlphabet(str[i + 1])){
                //std::cout << operands.top() << std::endl;
                index = GetAlphabetIndex(str[i + 1]);
                operands.top() *= 10;
                operands.top() += values[index];
                i++;
            }
        }else if(IsNumber(str[i])){
            operands.push(str[i] - '0');
            while(i + 1 < str.length() && IsNumber(str[i + 1])){
                operands.top() *= 10;
                operands.top() += str[i + 1] - '0';
                i++;
            }
        }else if(str[i] == ' '){
            continue;
        }else{ //operator
            auto ope = GetOperator(str[i]);
            ll right = operands.top();
            operands.pop();
            ll left = operands.top();
            operands.pop();
            if(str[i] == '&' && left == 0) ZeroFlag++;
            else if(str[i] == '&') {
                left *= std::pow(10 , ZeroFlag);
                ZeroFlag = 0;
            }
            else ZeroFlag = 0;
            ll value = ope(left , right);
            //std::cout << "left:" << left << " right:" << right << " ope = " << value << std::endl;
            operands.push(value);
        }
    }
    return operands.top();
}
int main(){
    std::string S;
    ll N;
    std::cin >> S >> N;
    //S = SwapUniqueOperator(S);
    //std::cout << S << std::endl;
    S = CastRPN(S);
    int values[4];
    //std::cout << S << std::endl;
    //std::cout << solve(S , values) << std::endl;
    
    ll value;
    int num;
    int count = 0;
    for(int i = 0; i < 10000; i++){
        values[3] = i % 10;
        values[2] = i % 100 / 10;
        values[1] = i % 1000 / 100;
        values[0] = i / 1000;
        value = solve(S , values);
        if(value == N){
            num = i;
            count++;
        }
    }
    std::cout << count << std::endl;
    if(count == 1) printf("%04d\n" , num);
    return 0;
}
accuracy: 1
time_ms: 10
memory_kb: 3588
final_rank: 7

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

string S;
int it;
unordered_map<char, int> mp {
    { 'a', 0 },
    { 'b', 0 },
    { 'c', 0 },
    { 'd', 0 },
};

ll number();
ll parse();

bool isVariable(char c) {
    return c == 'a' or c == 'b' or c == 'c' or c == 'd';
}

bool isDigit(char c) {
    return '0' <= c and c <= '9';
}

ll factor() {
    ll res = 0LL;
    if (S[it] == '(') {
        it++;
        res = parse();
        assert(S[it] == ')');
        it++;
        return res;
    }
    else if (isVariable(S[it])) {
        while (isVariable(S[it])) {
            res = (res * 10) + mp[S[it]];
            it++;    
        }
    }
    else if (isDigit(S[it])) {
        while (isDigit(S[it])) {
            res = (res * 10) + (S[it] - '0');
            it++;
        }
    }
    else {
        assert(false);
    }
    return res;
}

ll term() {
    ll lhs = factor();
    while (true) {
        if (it == (int)S.size()) break;
        if (S[it] == ')') break;
        if (S[it] == '+' or S[it] == '-') break;
        assert(S[it] == '*');
        it++;
        ll rhs = factor();
        lhs = lhs * rhs;
    }
    return lhs;
}

ll parse() {
    ll lhs = term();
    while (true) {
        if (it == (int)S.size()) break;
        if (S[it] == ')') break;
        char op = S[it];
        assert(op == '+' or op == '-');
        it++;
        ll rhs = term();
        lhs = lhs + (op == '+' ? rhs : -rhs);
    }
    return lhs;
}

int main() {
    cin >> S;
    tuple<int, int, int, int> ans{ -1, -1, -1, -1 };
    ll N; cin >> N;
    int num = 0;
    for (int i = 0 ; i < 10 ; i++) {
        for (int j = 0 ; j < 10 ; j++) {
            for (int k = 0 ; k < 10 ; k++) {
                for (int l = 0 ; l < 10 ; l++) {
                    mp['a'] = i;
                    mp['b'] = j;
                    mp['c'] = k;
                    mp['d'] = l;
                    it = 0;
                    if (parse() == N) {
                        ans = { i, j, k, l };
                        num++;
                    }
                }
            }
        }
    }
    cout << num << endl;
    if (num == 1) {
        auto [a, b, c, d] = ans;
        cout << a << b << c << d << endl;
    }
}
accuracy: 1
time_ms: 10
memory_kb: 3480
final_rank: 3

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

using ll = long long int;

int prec(char c) {
  switch (c) {
    case '+':
    case '-':
      return 1;
      break;

    case '*':
      return 2;
      break;

    case ')':
      return 0;
      break;

    default:
      return -1;
      break;
  }
}

ll invoke(char c, ll lhs, ll rhs) {
  switch (c) {
    case '+':
      return lhs + rhs;
      break;

    case '-':
      return lhs - rhs;
      break;

    case '*':
      return lhs * rhs;
      break;

    default:
      return -1;
      break;
  }
}

bool take_left(char c) { return c == '+' || c == '-' || c == '*' || c == ')'; }

int main() {
  std::string s;
  ll n;
  std::cin >> s >> n;

  int ans = 0;
  std::tuple<int, int, int, int> ans2;

  for (int a = 0; a < 10; a++) {
    for (int b = 0; b < 10; b++) {
      for (int c = 0; c < 10; c++) {
        for (int d = 0; d < 10; d++) {
          int pos = 0;
          std::stack<ll> stack;
          std::stack<char> ops;
          while (pos < s.size()) {
            if (std::isdigit(s[pos])) {
              ll tmp = 0;
              while (pos < s.size() && std::isdigit(s[pos])) {
                tmp *= 10;
                tmp += s[pos] - '0';
                pos++;
              }
              stack.push(tmp);
            } else if (std::isalpha(s[pos])) {
              ll tmp = 0;
              while (pos < s.size() && std::isalpha(s[pos])) {
                tmp *= 10;
                if (s[pos] == 'a') {
                  tmp += a;
                } else if (s[pos] == 'b') {
                  tmp += b;
                } else if (s[pos] == 'c') {
                  tmp += c;
                } else if (s[pos] == 'd') {
                  tmp += d;
                }
                pos++;
              }
              stack.push(tmp);
            } else if (std::ispunct(s[pos])) {
              if (take_left(s[pos])) {
                while (!ops.empty() && prec(ops.top()) >= prec(s[pos])) {
                  assert(stack.size() >= 2);
                  ll rhs = stack.top();
                  stack.pop();
                  ll lhs = stack.top();
                  stack.pop();
                  stack.push(invoke(ops.top(), lhs, rhs));
                  ops.pop();
                }
              }
              if (s[pos] == ')') {
                assert(ops.top() == '(');
                ops.pop();
              } else {
                ops.push(s[pos]);
              }
              pos++;
            }
          }

          while (!ops.empty()) {
            assert(stack.size() >= 2);
            ll rhs = stack.top();
            stack.pop();
            ll lhs = stack.top();
            stack.pop();
            stack.push(invoke(ops.top(), lhs, rhs));
            ops.pop();
          }

          assert(stack.size() == 1);

          if (stack.top() == n) {
            ans++;
            ans2 = {a, b, c, d};
          }
        }
      }
    }
  }

  std::cout << ans << '\n';
  if (ans == 1) {
    auto& [a, b, c, d] = ans2;
    std::cout << a << b << c << d << '\n';
  }
}
accuracy: 1
time_ms: 10
memory_kb: 3452
final_rank: 1

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

using ll = long long int;

enum kind {
  NUM,
  VAR,
  PROD,
  ADD,
  SUB,
};

struct tree {
  using ptr = std::shared_ptr<tree>;

  kind k;
  ll val;
  ptr left, right;

  tree(kind k, ll val, ptr left, ptr right)
      : k(k), val(val), left(left), right(right) {}

  static ptr integer(ll v) { return ptr(new tree(NUM, v, nullptr, nullptr)); }

  static ptr var(int v) { return ptr(new tree(VAR, v, nullptr, nullptr)); }

  static ptr prod(ptr left, ptr right) {
    return ptr(new tree(PROD, -1, left, right));
  }

  static ptr add(ptr left, ptr right) {
    return ptr(new tree(ADD, -1, left, right));
  }

  static ptr sub(ptr left, ptr right) {
    return ptr(new tree(SUB, -1, left, right));
  }

  ll eval(const std::array<ll, 4> &vars) {
    switch (k) {
      case NUM:
        return val;
        break;

      case VAR:
        return vars[val];
        break;

      case PROD:
        return left->eval(vars) * right->eval(vars);
        break;

      case ADD:
        return left->eval(vars) + right->eval(vars);
        break;

      case SUB:
        return left->eval(vars) - right->eval(vars);
        break;

      default:
        return -1;
        break;
    }
  }
};

struct parser {
  std::string str;
  size_t pos;

  parser(std::string str) : str(str), pos(0) {}

  char peek() { return str[pos]; }

  char get() { return str[pos++]; }

  bool eof() { return pos >= str.size(); }

  ll digit() {
    char c = get();
    assert(std::isdigit(c));
    return c - '0';
  }

  bool isdigit() { return !eof() && std::isdigit(peek()); }

  bool islower() { return !eof() && std::islower(peek()); }

  bool peek(char c) { return !eof() && peek() == c; }

  tree::ptr integer() {
    ll res = 0;
    while (isdigit()) {
      res *= 10;
      res += digit();
    }
    return tree::integer(res);
  }

  tree::ptr var() {
    tree::ptr res = tree::var(get() - 'a');

    while (islower()) {
      res =
          tree::add(tree::prod(res, tree::integer(10)), tree::var(get() - 'a'));
    }

    return res;
  }

  tree::ptr par() {
    get();
    tree::ptr res = expr();
    get();
    return res;
  }

  tree::ptr atomic() {
    if (islower()) {
      return var();
    } else if (isdigit()) {
      return integer();
    } else if (peek('(')) {
      return par();
    } else {
      std::cerr << "invalid char at " << pos << "\n";
      assert(false);
    }
  }

  tree::ptr term() {
    tree::ptr res = atomic();
    while (peek('*')) {
      get();
      res = tree::prod(res, atomic());
    }
    return res;
  }

  tree::ptr expr() {
    tree::ptr res = term();
    while (peek('+') || peek('-')) {
      if (peek('+')) {
        get();
        res = tree::add(res, term());
      } else {
        get();
        res = tree::sub(res, term());
      }
    }
    return res;
  }
};

int main() {
  std::string s;
  ll n;
  std::cin >> s >> n;

  parser psr(s);

  tree::ptr expr = psr.expr();
  assert(psr.eof());

  ll ans = 0;
  std::array<ll, 4> ans2;

  for (int a = 0; a < 10; a++) {
    for (int b = 0; b < 10; b++) {
      for (int c = 0; c < 10; c++) {
        for (int d = 0; d < 10; d++) {
          if (expr->eval({a, b, c, d}) == n) {
            ans++;
            if (ans == 1) {
              ans2 = {a, b, c, d};
            }
          }
        }
      }
    }
  }

  std::cout << ans << "\n";
  if (ans == 1) {
    for (auto x : ans2) {
      std::cout << x;
    }
    std::cout << "\n";
  }
}
accuracy: 1
time_ms: 10
memory_kb: 3520
final_rank: 4

code_snippet: #include<bits/stdc++.h>
using namespace std;
#define rep(i,a,b) for(int i=(int)(a);i<(int)(b);i++)
const int INF = 1e9+1e7;
using ll=long long;
int main(){
	string S;
	cin>>S;
	ll val;
	cin>>val;
	vector<int> ans;
	vector<int> p(4);
	int N=S.size();
	int ind=0;
	auto f=[&](auto self)->ll{
		vector<ll> s;
		int op=0;
		auto g=[&](ll v){
			if(op==0) s.push_back(v);
			if(op==-1) s.push_back(-v);
			if(op==1){
				assert(!s.empty());
				ll a=(*s.rbegin());
				a*=v;
				s.at(s.size()-1)=a;
			}
		};
		while(ind!=N){
			if(S[ind]=='-'){
				op=-1;
				ind++;
			}
			else if(S[ind]=='+'){
				op=0;
				ind++;
			}
			else if(S[ind]=='*'){
				op=1;
				ind++;
			}
			else if(S[ind]=='('){
				ind++;
				g(self(self));
			}
			else if(S[ind]==')'){
				ind++;
				break;
			}
			else if('a'<=S[ind]&&S[ind]<='d'){
				ll tmp=0;
				while(ind!=N&&'a'<=S[ind]&&S[ind]<='d'){
					tmp*=10ll;
					tmp+=p[S[ind]-'a'];
					ind++;
				}
				g(tmp);
			}
			else{
				assert('0'<=S[ind]&&S[ind]<='9');
				ll tmp=0;
				while(ind!=N&&'0'<=S[ind]&&S[ind]<='d'){
					tmp*=10ll;
					tmp+=(S[ind]-'0');
					ind++;
				}
				g(tmp);
			}
		}
		ll res=0;
		for(auto x:s) res+=x;
		return res;
	};
	rep(num,0,10000){
		int tmp=num;
		rep(i,0,4) p[3-i]=tmp%10,tmp/=10;
		ind=0;
		if(val==f(f)) ans.push_back(num);
	}
	cout<<ans.size()<<"\n";
	if(ans.size()==1){
		printf("%04d\n",ans[0]);
	}
}
accuracy: 1
time_ms: 10
memory_kb: 3576
final_rank: 5