Problem:
知識の証明
あなたが住んでいる集合住宅の入口のドアには，パスワード式のロックがかけられている．このパスワードはちょうど 4 桁の 0 から 9 の範囲の数字で構成されており，あなたはいつも，集合住宅の管理人から伝えられたパスワード
P
を使って，このドアのロックを解除している．
ある日，あなたは集合住宅の住民全員が，自分と同じパスワード
P
を使っているのかどうか気になり，同じ集合住宅に住む友人に尋ねることにした．あなたとあなたの友人は，自分のパスワードを互いに伝えあうことで，同じパスワードを使っているのかどうか確かめ合うことができる．しかしながら，パスワードがそれぞれの住民に個別に割り当てられている可能性を考えると，この方法は好ましくない．自分のパスワードを知っているのは自分だけであるべきで，他人に伝えるべきではないからである．
この事態を防ぐために，あなたとあなたの友人は，自分のパスワードをハッシュ関数に入力し，得られたハッシュ値を互いに伝えあうことにした．ここで使用するハッシュ関数の計算式
S
は小文字のアルファベット 'a', 'b', 'c', 'd' と記号 '[', ']', '+', '*', '^' からなり，以下のBNFで定義される <Hash> によって表される．
<Hash> ::= <Letter> | '['<Op><Hash><Hash>']'
<Op> ::= '+' | '*' | '^'
<Letter> ::= 'a' | 'b' | 'c' | 'd'
ここで 'a', 'b', 'c', 'd' はそれぞれ 4 桁のパスワードの先頭から 1 桁目，2 桁目，3 桁目，4 桁目の数字を表す．'+', '*', '^' は演算子であり，以下のような意味を表す．
'+' : 続く 2 つの <Hash> を二進数で表したときの論理和を取る
'*' : 続く 2 つの <Hash> を二進数で表したときの論理積を取る
'^' : 続く 2 つの <Hash> を二進数で表したときの排他的論理和を取る
ここで，論理和，論理積，排他的論理和の真理値表はそれぞれ以下のようになる．
A
B
[+AB]
0
0
0
1
0
1
0
1
1
1
1
1
A
B
[*AB]
0
0
0
1
0
0
0
1
0
1
1
1
A
B
[^AB]
0
0
0
1
0
1
0
1
1
1
1
0
例として，ハッシュ関数 [+c[+a[^bd]]] にパスワード 0404 を入力すると，ハッシュ値として 0 が得られる．同じハッシュ値が得られるパスワードとして 0000, 0101, 0202, 0303, 0505, 0606, 0707, 0808, 0909 がある．
あなたのパスワード
P
をハッシュ関数
S
に入力した結果を出力せよ．また，ハッシュ値からパスワードを一意に特定可能なハッシュ関数の使用を防ぐため，あなたのパスワードと同じハッシュ値になるパスワードの数も出力せよ．
Input
入力は最大で 50 個のデータセットから構成される．各データセットは次の形式で表される．
S
P
各データセットの1行目は，ハッシュ関数の計算式
S
である．各データセットの2行目は，4 桁の 0 から 9 の範囲の数字で構成されるパスワード
P
である．ハッシュ関数
S
の長さは 80 以下であると仮定してよい．
入力の終了は '.' の1文字だけを含む行で表される．
Output
各データセットについて，
P
を
S
に入力して得られるハッシュ値と，
P
と同じハッシュ値が得られるパスワードの数を空白区切りで出力せよ．
Sample Input
[+c[+a[^bd]]]
0404
[*b[*[*cd]a]]
7777
[^[^ab][^cd]]
1295
a
9876
[^dd]
9090
.
Output for the Sample Input
0 10
7 1
15 544
9 1000
0 10000

Submitted solutions:

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

using namespace std;
using ll = long long;

// clang-format off

int letter(const string& s, int& r, const string& psw) {
    ++r;
    int idx = s[r-1] - 'a';
    return psw[idx] - '0';
}

int hsh(const string& s, int& r, const string& psw) {
    if (s[r]=='[') {
        ++r; // [
        char op=s[r];
        ++r;
        int v1 = hsh(s, r, psw);
        int v2 = hsh(s, r, psw);
        int v3;
        if (op == '+') {
            v3 = v1 | v2;
        } else if (op == '^') {
            v3 = v1 ^ v2;
        } else {
            v3 = v1 & v2;
        }
        ++r; // ]
        return v3;
    } else {
        return letter(s, r, psw);
    }
}

int main(void) {
    
    string s, psw;
    while(cin>>s, s!=".") {
        cin>>psw;
        int r =0;
        int v = hsh(s, r, psw);
        int ans=0;
        for(int i=0;i<10000;++i){
            string p=to_string(i);
            while (p.size()<4) p.insert(p.begin(), '0');
            int _r=0;
            if (v==hsh(s, _r, p)) {
                ++ans;
            }
        }
        cout << v << " " << ans << endl;
    }



    
}
accuracy: 1
time_ms: 20
memory_kb: 3584
final_rank: 12

code_snippet: #include <bits/stdc++.h>
#include <atcoder/all>
using namespace std;
using namespace atcoder;
using mint = modint998244353;

//多倍長整数//
//#include <boost/multiprecision/cpp_int.hpp>
//namespace mp = boost::multiprecision;
//using bint = mp::cpp_int;

const int INF = 2e9;
const int MOD = 998244353;
const long long LINF = 5e18;

using ll = long long;
using vi = vector<int>;
using vl = vector<long long>;
using vs = vector<string>;
using vc = vector<char>;
using vb = vector<bool>;
using vvi = vector<vector<int>>;
using vvvi = vector<vector<vector<int>>>;
using vvvvi = vector<vector<vector<vector<int>>>>;
using vvl = vector<vector<long long>>;
using vvvl = vector<vector<vector<long long>>>;
using vvvvl = vector<vector<vector<vector<long long>>>>;
using vvc = vector<vector<char>>;
using vvb = vector<vector<bool>>;
using vvvb = vector<vector<vector<bool>>>;
using vvvvb = vector<vector<vector<vector<bool>>>>;

#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define dump(x)  cout << #x << " = " << (x) << endl;
#define Yes(n) cout << ((n) ? "Yes" : "No"  ) << endl
#define ALL(obj) (obj).begin(),(obj).end()

ll solve(const string&s, int pass) {
	stack<ll>stk;
	for(int i=s.size()-1; i>=0; i--) {
		if(s[i]=='[' || s[i]==']') continue;
		if(s[i]=='a'){ stk.push((pass%10000-pass%1000)/1000); continue;}
		if(s[i]=='b'){ stk.push((pass%1000-pass%100)/100); continue;}
		if(s[i]=='c'){ stk.push((pass%100-pass%10)/10); continue;}
		if(s[i]=='d'){ stk.push((pass%10-pass%1)/1); continue;}
		
		ll v1 = stk.top(); stk.pop();
		ll v2 = stk.top(); stk.pop();
		if(s[i]=='+'){ stk.push(v1|v2); continue;}
		if(s[i]=='*'){ stk.push(v1&v2); continue;}
		if(s[i]=='^'){ stk.push(v1^v2); continue;}
	}
	return stk.top();
}

int main(){
	while(1) {
		string s;
		ll pass,h,cnt=0;
		cin >> s >> pass;
		if(s==".") break;
		h = solve(s,pass);
		for(int i=0; i<10000; i++) {
			if(h==solve(s,i)) cnt++;
		}
		cout << h << " " << cnt << endl;
	}
	return 0;
}
accuracy: 1
time_ms: 40
memory_kb: 3584
final_rank: 16

code_snippet: #include<bits/stdc++.h>
using namespace std;
using ll=long long;
string S,T;
int num[4];
int idx;

bool isletter(char c){
    return (c=='a'||c=='b'||c=='c'||c=='d');
}

int hash_(){
    if(isletter(S[idx])){
        idx++;
        int t=num[S[idx-1]-'a'];
        return t;
    }
    idx++;
    char e=S[idx];
    idx++;
    int first=hash_();
    int second=hash_(); 
    idx++;
    if(e=='+')return first|second;
    if(e=='*')return first&second;
    if(e=='^')return first^second;
    return 0;
}

void solve(){   
    cin>>T;
    for(int i=0; i<4; i++)num[i]=T[i]-'0';
    idx=0;
    int base=hash_();
    cout<<base<<" ";
    int cnt=0;
    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++){
        num[0]=a;
        num[1]=b;
        num[2]=c;
        num[3]=d;
        idx=0;
        if(hash_()==base){
            cnt++;
        }
    }
    cout<<cnt<<endl;
}

int main() {
    for(;;){
        cin>>S;
        if(S==".")return 0;
        solve();
    } 
}
accuracy: 1
time_ms: 20
memory_kb: 3440
final_rank: 4

code_snippet: #include <bits/stdc++.h>
#include <unordered_map>
#include <stdlib.h>
using namespace std;
#define rep(i, a, n) for(ll i = a; i < n; i++)
#define rrep(i, a, n) for(ll i = a; i >= n; i--)
#define ll long long
#define pii pair<int, int>
#define pll pair<ll, ll>
#define all(x) (x).begin(), (x).end()
//constexpr ll MOD = 1000000007;
constexpr ll MOD = 998244353;
constexpr int IINF = 1001001001;
constexpr ll INF = 1LL<<60;
template<class t,class u> void chmax(t&a,u b){if(a<b)a=b;}
template<class t,class u> void chmin(t&a,u b){if(b<a)a=b;}


ll gcd(ll a, ll b){
    if(a%b == 0){
      return b;
    }else{
      return gcd(b, a%b);
    }
}

ll lcm(ll a, ll b){
    return a*b / gcd(a, b);
}

ll powMod(ll x, ll n) {
    if (n == 0) return 1 % MOD;
    ll val = powMod(x, n / 2);
    val *= val;
    val %= MOD;
    if (n % 2 == 1) val *= x;
    return val % MOD;
}

ll is_str(char s){
    return 'a' <= s && s <= 'z';
}

int main() {
    while(true){
        string s; cin >> s;
        ll n = s.size();
        if(s == ".") break;
        string t; cin >> t;
        unordered_map<ll,ll> mp;
        vector<ll> vec(4);
        auto calc = [&](auto calc, ll l, ll r) -> ll {
            if(s[l] == '[') return calc(calc, l+1,r-1);
            if(is_str(s[l])) return vec[s[l]-'a'];
            ll x = l+1;
            ll num1, num2;
            if(is_str(s[x])){
                num1 = vec[s[x]-'a'];
                x++;
            }else{
                ll cnt = 1;
                x++;
                while(cnt != 0){
                    if(s[x] == '[') cnt++;
                    if(s[x] == ']') cnt--;
                    x++;
                }
                num1 = calc(calc, l+2, x-1);
            }
            if(is_str(s[x])){
                num2 = vec[s[x]-'a'];
                x++;
            }else{
                ll xl = x;
                ll cnt = 1;
                x++;
                while(cnt != 0){
                    // cerr << l << " " << r << " " << xl << " " << x << endl;
                    if(s[x] == '[') cnt++;
                    if(s[x] == ']') cnt--;
                    x++;
                }
                num2 = calc(calc, xl, r-1);
            }

            if(s[l] == '+') return num1|num2;
            if(s[l] == '*') return num1&num2;
            if(s[l] == '^') return num1^num2;
            return -1;
        };
        rep(i,0,10)rep(j,0,10)rep(k,0,10)rep(l,0,10){
            vec[0] = i;
            vec[1] = j;
            vec[2] = k;
            vec[3] = l;
            mp[calc(calc, 0, n)]++;
        }
        rep(i,0,4) vec[i] = t[i]-'0';
        ll num = calc(calc,0,n);
        cout << num << " " <<  mp[num] << endl;
    }
    return 0;
}
accuracy: 1
time_ms: 40
memory_kb: 3312
final_rank: 1

code_snippet: #include <cstddef>
#pragma GCC target("avx2")
#pragma GCC optimize("O3")
#pragma GCC optimize("unroll-loops")

// #ifndef ONLINE_JUDGE
// #define _GLIBCXX_DEBUG // 配列外参照のエラー
// #endif

// スタンダードライブラリ
#include <bits/stdc++.h>
using namespace std;

// 型関連
using ll = long long;
using lint = long long;
using pll = pair<ll, ll>;
using plll = tuple<ll, ll, ll>;
using pii = pair<int, int>;
using piii = tuple<ll, ll, ll>;
using vi = vector<int>;
using vvi = vector<vector<int>>;
using vvvi = vector<vector<vector<int>>>;
using vvvvi = vector<vector<vector<vector<int>>>>;
using vl = vector<ll>;
using vvl = vector<vector<ll>>;
using vvvl = vector<vector<vector<ll>>>;
using vvvvl = vector<vector<vector<vector<ll>>>>;

template <class T> using prique = priority_queue<T>;

// 型定数
constexpr ll mod = 998244353;
constexpr ll MOD = 1000000007;
int INF32 = 2e9;
ll INF64 = 9e18;
#define endl '\n';

int dx[4] = {1, 0, -1, 0};
int dy[4] = {0, 1, 0, -1};

/////////////////////////// print関連
template <typename T> void print(vector<T> A);
void print();
template <class Head, class... Tail> void print(Head &&head, Tail &&...tail);
void print(string S);
template <typename T> void print(vector<vector<T>> &F);

inline void Yes(bool f);

///////////////////ここから//////////////////////
inline bool is_letter(char c) {
    return c == 'a' | c == 'b' || c == 'c' || c == 'd';
}

int get_val(string &S, vi& pass) {
    int ret = 0, idx = 0;

    auto hash = [&](auto hash) -> int {
        int val;
        if (is_letter(S[idx])) {
            val = pass[S[idx] - 'a'];
            idx++;
        } else {
            idx++;
            char op = S[idx];
            idx++;
            int left = hash(hash);
            int right = hash(hash);
            if (op == '+') {
                val = (left | right);
            } else if (op == '*') {
                val = (left & right);
            } else {
                val = (left ^ right);
            }
            idx++;
        }
        return val;
    };

    return hash(hash);
}

bool solve() {

    string S;
    cin >> S;
    if (S == ".") {
        return false;
    }
    string T;
    cin >> T;
    int p0 = 0;
    int p0val;
    int t = 1;
    for (int i = 3; i >= 0; i--) {
        p0 += t * (T[i] - '0');
        t*=10;
    }

    
    map<int,int> pcount;

    for (int p = 0; p < 10000; p++) {

        vi pass(4);
        int tmp=p;
        for (int i = 0; i < 4; i++) {
            pass[3 - i] = tmp % 10;
            tmp/=10;
        }

        auto val = get_val(S, pass);
        if (pcount.count(val) == 0) {
            pcount[val]=0;
        }
        pcount[val]++;

        if (p == p0) {
            p0val=val;
        }
    }
    print(p0val,pcount[p0val]);

    return true;
}

int main() {
    ios::sync_with_stdio(false);
    std::cin.tie(nullptr);
    while (solve())
        ;
}

////////////////////print/////////////////////////
// 1次元ベクトルを出力する
template <typename T> void print(vector<T> A) {
    if (A.size() == 0) {
        return;
    }
    for (size_t i = 0; i < A.size() - 1; i++) {
        cout << A[i] << ' ';
    }
    cout << A[A.size() - 1] << endl;
    return;
}

// 2次元ベクトルを出力する
template <typename T> void print(vector<vector<T>> &F) {
    for (size_t i = 0; i < F.size(); i++) {
        for (size_t j = 0; j < F[i].size(); j++) {
            cout << F[i][j];
            if (j < F[i].size() - 1) {
                cout << ' ';
            }
        }
        cout << endl;
    }
}

// 空白行を出力するprint
void print() { cout << endl; }

// 数値・文字列を空白区切りで出力する. print(a,b)など
template <class Head, class... Tail> void print(Head &&head, Tail &&...tail) {
    cout << head;
    if (sizeof...(tail) != 0)
        cout << " ";
    print(forward<Tail>(tail)...);
}

void print(string S) { cout << S << endl; }

//////////////出力関連

inline void Yes(bool f) {
    if (f) {
        cout << "Yes" << endl;
    } else {
        cout << "No" << endl;
    }
}
accuracy: 1
time_ms: 20
memory_kb: 3584
final_rank: 12

code_snippet: #include <bits/stdc++.h>
using namespace std;
using ll = long long;
using a2 = array<ll, 2>;
using a3 = array<ll, 3>;

ll INF = 1e18;

template <typename A>
void chmin(A &l, const A &r) {
    if(r < l) l = r;
}
template <typename A>
void chmax(A &l, const A &r) {
    if(l < r) l = r;
}

int Hash(string &s, int &i) {
    if('0'<=s[i]&&s[i]<='9')
    { 
        i++;
        return s[i-1] - '0';
    }
    i++;
    char ope = s[i];
    i++;
    int val1 = Hash(s, i);
    int val2 = Hash(s, i);
    i++;
    if(ope == '^') return val1 ^ val2;
    if(ope == '+') return val1 | val2;
    if(ope == '*') return val1 & val2;
}

void init() {}
string s, p;
void input() { cin >> s >> p; }
void solve() {
    map<ll, ll> cnt;
    ll idx = -1;
    for(int i = 0; i <= 9999; i++) {
        string inp = s;
        string num = to_string(i);
        while(num.size() < 4) num.insert(num.begin(), '0');
        for(auto &x : inp) {
            if('a' <= x && x <= 'd') {
                x = num[x - 'a'];
            }
        }

        int j = 0;
        int ret = Hash(inp, j);
        if(num == p) idx = ret;

        cnt[ret]++;
    }
    cout << idx << ' ' << cnt[idx] << endl;
}

ifstream in;
ofstream out;
int main(int argc, char **argv) {
    ios::sync_with_stdio(false);
    cin.tie(0);

    if(argc > 2) {
        in.open(argv[1]);
        cin.rdbuf(in.rdbuf());
        out.open(argv[2]);
        cout.rdbuf(out.rdbuf());
    }

    // init();
    while(1) {
        input();
        if(s == ".") break;
        solve();
    }

    return 0;
}
accuracy: 1
time_ms: 60
memory_kb: 3420
final_rank: 3

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


string S;
string P;
map<char, int> D;

int read_Hash(int &i){
  if(S[i] == '['){
    i++;
    char op = S[i];
    i++;
    int res1 = read_Hash(i);
    int res2 = read_Hash(i);
    // cerr << i << "!s\n";
    assert(S[i] == ']');
    i++;
    if(op == '+'){
      return res1 | res2;
    }else if(op == '*'){
      return res1 & res2;
    }else{
      assert(op == '^');
      return res1 ^ res2;
    }
  }else{
    // cerr << i << "\n";
    // cerr << S[i] << "\n";
    // cerr << int(S[i]) << "\n";
    assert(string("abcd").find(S[i]) < 4);
    // cerr << "?\n";
    return D[S[i++]];
  }
  return 0;
}

int main(){
  while(true){
  cin >> S;
  if(S == "."){
    break;
  }
  cin >> P;
  int index = 0;
  D['a'] = P[0] - '0';
  D['b'] = P[1] - '0';
  D['c'] = P[2] - '0';
  D['d'] = P[3] - '0';
  int my_hash = read_Hash(index);
  int ans = 0;
  for(int a = 0; a < 10; a++){
    D['a'] = a;
    for(int b = 0; b < 10; b++){
      D['b'] = b;
      for(int c = 0; c < 10; c++){
        D['c'] = c;
        for(int d = 0; d < 10; d++){
          D['d'] = d;
          index = 0;
          int new_hase = read_Hash(index);
          if(new_hase == my_hash){
            ans++;
          }

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

}
accuracy: 1
time_ms: 50
memory_kb: 3584
final_rank: 18

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

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

  while(true) {
    string S, N;
    cin >> S >> N;


    if(S == ".") { break; }

    auto rec = [&](auto &&rec, string s) -> ll {
      ll n = s.size(), x, y, p = 2;

      if(s.size() == 1) { return N[s[0] - 'a'] - '0'; }

      if(s[p] == '[') {
        for(ll i = p, c = 0; i < n; i++) {
          if(s[i] == ']' && !--c) {
            x = rec(rec, s.substr(p, i - p));
            p = i + 1;
            break;
          }
          if(s[i] == '[') { c++; }
        }
      }
      else { x = N[s[p++] - 'a'] - '0'; }

      if(s[p] == '[') {
        for(ll i = p, c = 0; i < n; i++) {
          if(s[i] == ']' && !--c) {
            y = rec(rec, s.substr(p, i - p));
            break;
          }
          if(s[i] == '[') { c++; }
        }
      }
      else { y = N[s[p] - 'a'] - '0'; }

      if(s[1] == '+') { return x | y; }
      if(s[1] == '*') { return x & y; }
      if(s[1] == '^') { return x ^ y; }
    };

    ll ans = rec(rec, S), cnt = 0;

    N = "0000";
    for(N[0] = '0'; N[0] <= '9'; N[0]++) {
      for(N[1] = '0'; N[1] <= '9'; N[1]++) {
        for(N[2] = '0'; N[2] <= '9'; N[2]++) {
          for(N[3] = '0'; N[3] <= '9'; N[3]++) {
            if(rec(rec, S) == ans) { cnt++; }
          }
        }
      }
    }

    cout << ans << " " << cnt << "\n";
  }
}
accuracy: 1
time_ms: 90
memory_kb: 3376
final_rank: 2

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

#define REP(i, n) for (int i = 0; i < n; i++)
#define REPR(i, n) for (int i = n; i >= 0; i--)
#define FOR(i, m, n) for (ll i = m; i < n; i++)
#define FORR(i, m, n) for (ll i = m; i >= n; i--)
#define REPO(i, n) for (ll i = 1; i <= n; i++)
#define ll long long
#define INFll (ll)1ll << 60
#define MINFll (-1 * INFll)
#define ALL(n) n.begin(), n.end()
#define MOD (ll)998244353
#define P pair<ll, ll>

string s, tt;

P solve(ll i, string t){
    //cout << i << " " << t << endl;

    if(s[i] != '['){
        return P(t[s[i] - 'a'] - '0', i + 1);
    }
    i++;
    char op = s[i];
    i++;
    P res1 = solve(i, t);
    i = res1.second;
    P res2 = solve(i,t);
    i = res2.second + 1;
    ll x = res1.first, y = res2.first;
    ll res = 0;
    if(op == '+'){
        res = (x | y);
    }
    if(op == '*'){
        res = (x & y);
    }
    if(op == '^'){
        res = (x ^ y);
    }
    return P(res, i);
}
int main(){
    while(1){
    cin >> s;
    if(s == ".") return 0;
    cin >> tt;


    
    ll res = solve(0, tt).first;
    ll ans = 0;
    REP(i, 10){
        REP(j, 10){
            REP(k, 10){
                REP(l, 10){
                    string c = to_string(i) + to_string(j) + to_string(k) + to_string(l);
                    if(solve(0, c).first == res) ans++;
                }
            }
        }
    }
    cout << res <<" "<< ans << endl;
}
}
accuracy: 1
time_ms: 50
memory_kb: 3456
final_rank: 8

code_snippet: #include <bits/stdc++.h>
#define rep(i,n) for(int i=0;i<(n);i++)
using namespace std;
#define all(a) a.begin(),a.end()
#define compress(a) sort(all(a));a.erase(unique(all(a)),a.end())
typedef long long ll;
typedef pair<ll,ll> P;
constexpr ll mod=998244353;
constexpr ll inf=3e18;

vector<string>idx;

int solve(){
    string s;
    cin>>s;
    if(s==".")return 0;
    string p;
    cin>>p;

    stack<int>stk;
    stack<char>lex;
    vector<int>res(10000);
    int ans=0,cnt=0;
    
    for(auto &str:idx){
        rep(i,s.size()){
            lex.push(s[i]);
            if(s[i]=='a')stk.push(str[0]-'0');
            if(s[i]=='b')stk.push(str[1]-'0');
            if(s[i]=='c')stk.push(str[2]-'0');
            if(s[i]=='d')stk.push(str[3]-'0');
            if(s[i]==']'){
                char op;
                while(lex.size()){
                    char tp=lex.top();
                    lex.pop();
                    if(tp=='[')break;
                    if(tp=='*'||tp=='+'||tp=='^')op=tp;
                }
                lex.push('.');
                int v1=stk.top();
                stk.pop();
                int v2=stk.top();
                stk.pop();
                if(op=='*')stk.push(v1&v2);
                if(op=='+')stk.push(v1|v2);
                if(op=='^')stk.push(v1^v2);
            }       
        }

        int n=stoi(str);
        res[n]=stk.top();
        if(str==p)ans=res[n];
    }
    rep(i,10000)if(res[i]==ans)cnt++;
    cout<<ans<<" "<<cnt<<endl;

    return 1;
}

int main(){
    for(char a='0';a<='9';a++){
        for(char b='0';b<='9';b++){
            for(char c='0';c<='9';c++){
                for(char d='0';d<='9';d++){
                    string s(4,'.');
                    s[0]=a;
                    s[1]=b;
                    s[2]=c;
                    s[3]=d;
                    idx.push_back(s);
                }
            }
        }
    }
    while(solve());
}
accuracy: 1
time_ms: 80
memory_kb: 3476
final_rank: 10

code_snippet: #include <bits/stdc++.h>
using namespace std;
using ll=long long;
using ull=unsigned long long;
using status=string::const_iterator;
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define loop(n) for (int tjh = 0; tjh < (int)(n); tjh++)
string S,P;status s0;int S1,re0;
int Letter(status &s){
  return P[(*(s++))-'a']-48;
}
int Op(status &s){
  int res=((*s)=='+'?0:(*s)=='*'?1:2);
  s++;
  return res;
}
int Hash(status &s){
  if((*s)=='['){
    s++;
    int o=Op(s);
    int p=Hash(s);
    int q=Hash(s);
    s++;
    return o==0?(p|q):o==1?(p&q):(p^q);
  }
  return Letter(s);
}
signed main() {
  cin.tie(0)->sync_with_stdio(0);
  cout<<fixed<<setprecision(16);
  S.reserve(80);
  for(;cin>>S>>P,S!=".";){
    s0=S.cbegin();S1=Hash(s0);re0=0;P="0000";
    loop(10000){
      s0=S.cbegin();
      if(Hash(s0)==S1)re0++;
      rep(i,4){
	P[i]++;
	if(P[i]<='9')break;
	P[i]='0';
      }
    }
    cout<<S1<<' '<<re0<<'\n';
  }
}
accuracy: 1
time_ms: 20
memory_kb: 3584
final_rank: 12

code_snippet: # pragma GCC target("avx")
# pragma GCC optimize("O3")
# pragma GCC optimize("unroll-loops")

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

#include <atcoder/all>
using namespace atcoder;

using ll = long long;
using mint = modint998244353;

bool solving = true;

int HASH(string &S, int &i);
int LETTER(string &S, int &i);

int HASH(string &S, int &i) {
  int ret = 0;
  if(isdigit(S[i])) {
    ret = LETTER(S, i);
  }else {
    char op = S[i + 1];
    i += 2;
    int lf = HASH(S, i);
    int rg = HASH(S, i);
    i += 1;
    ret = (op == '+' ? lf | rg : op == '*' ? lf & rg : lf ^ rg);
  }
  return ret;
}

int LETTER(string &S, int &i) {
  int ret = S[i] - '0';
  i += 1;
  return ret;
}

string convert(string S, int P) {
  vector<int> d(4);
  d[0] = (P / 1000) % 10;
  d[1] = (P / 100) % 10;
  d[2] = (P / 10) % 10;
  d[3] = (P / 1) % 10;
  for(char &c : S) {
    if('a' <= c && c <= 'd') {
      c = char('0' + d[c - 'a']);
    }
  }
  return S;
}

void solve() {
  string S;
  cin >> S;
  if(S == ".") {
    solving = false;
    return;
  }
  int P;
  cin >> P;
  int i = 0;
  string X = convert(S, P);
  int hash_x = HASH(X, i);
  int ans = 0;
  for(int p = 0; p < 10000; p++) {
    i = 0;
    string Y = convert(S, p);
    int hash_y = HASH(Y, i);
    ans += (hash_x == hash_y);
  }
  cout << hash_x << " " << ans << endl;
}

int main() {
  while(solving) solve();
}
accuracy: 1
time_ms: 20
memory_kb: 3584
final_rank: 12

code_snippet: #include <bits/stdc++.h>
using namespace std;
using ll=long long;
using ull=unsigned long long;
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define loop(n) for (int tjh = 0; tjh < (int)(n); tjh++)
string S,P;vector<int>t;int p,q,r;int re0,res;
void f(){
  for(char i:S){
    switch(i){
    case '[':
      break;
    case ']':
      break;
    case '+':
      t.push_back(-1);
      break;
    case '*':
      t.push_back(-2);
      break;
    case '^':
      t.push_back(-3);
      break;
    default:
      t.push_back(P[i-'a']-48);
      break;
    }
    for(;t.size()>=3&&t[t.size()-1]>=0&&t[t.size()-2]>=0;){
      q=t.back();t.pop_back();
      p=t.back();t.pop_back();
      r=t.back();t.pop_back();
      t.push_back(r==-1?(p|q):r==-2?(p&q):(p^q));
    }
  }
  return;
}
signed main() {
  cin.tie(0)->sync_with_stdio(0);
  cout<<fixed<<setprecision(16);
  t.reserve(80);
  for(;cin>>S,S!=".";){
    cin>>P;
    f();re0=t.back();t.pop_back();
    res=0;
    P="0000";
    loop(10000){
      f();
      if(t.back()==re0)res++;
      t.pop_back();
      rep(i,4){
	P[i]++;
	if(P[i]<='9')break;
	P[i]='0';
      }
    }
    cout<<re0<<' '<<res<<'\n';
  }
}
accuracy: 1
time_ms: 40
memory_kb: 3456
final_rank: 7

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

int solve(){
    string s;
    cin>>s;
    if(s=="."){
        return 0;
    }
    string p;
    cin>>p;
    deque<int> qu; //'['=-1,']'=-2,'+'=-3,'*'=-4,'^'=-5
    int pa,pb,pc,pd;
    pd=p.at(3)-'0';
    pc=p.at(2)-'0';
    pb=p.at(1)-'0';
    pa=p.at(0)-'0';
    int a,b,c,ans;
    for(int i=0;i<s.size();i++){
        if(s.at(i)=='['){
            qu.push_front(-1);
        }
        if(s.at(i)=='+'){
            qu.push_front(-3);
        }
        if(s.at(i)=='*'){
            qu.push_front(-4);
        }
        if(s.at(i)=='^'){
            qu.push_front(-5);
        }
        if(s.at(i)=='a'){
            qu.push_front(pa);
        }
        if(s.at(i)=='b'){
            qu.push_front(pb);
        }
        if(s.at(i)=='c'){
            qu.push_front(pc);
        }
        if(s.at(i)=='d'){
            qu.push_front(pd);
        }
        if(s.at(i)==']'){
            a=qu.front();
            qu.pop_front();
            b=qu.front();
            qu.pop_front();
            c=qu.front();
            qu.pop_front();
            qu.pop_front();
            if(c==-3){
                qu.push_front(a|b);
            }
            if(c==-4){
                qu.push_front(a&b);
            }
            if(c==-5){
                qu.push_front(a^b);
            }
        }
    }
    ans=qu.front();
    qu.pop_front();
    assert(qu.size()==0 && ans>=0);
    int anscount=0;
    string q;
    for(int i=0;i<10000;i++){
        q=to_string(i);
        while(q.size()<4){
            q="0"+q;
        }
        pd=q.at(3)-'0';
        pc=q.at(2)-'0';
        pb=q.at(1)-'0';
        pa=q.at(0)-'0';
        for(int i=0;i<s.size();i++){
            if(s.at(i)=='['){
                qu.push_front(-1);
            }
            if(s.at(i)=='+'){
                qu.push_front(-3);
            }
            if(s.at(i)=='*'){
                qu.push_front(-4);
            }
            if(s.at(i)=='^'){
                qu.push_front(-5);
            }
            if(s.at(i)=='a'){
                qu.push_front(pa);
            }
            if(s.at(i)=='b'){
                qu.push_front(pb);
            }
            if(s.at(i)=='c'){
                qu.push_front(pc);
            }
            if(s.at(i)=='d'){
                qu.push_front(pd);
            }
            if(s.at(i)==']'){
                a=qu.front();
                qu.pop_front();
                b=qu.front();
                qu.pop_front();
                c=qu.front();
                qu.pop_front();
                qu.pop_front();
                if(c==-3){
                    qu.push_front(a|b);
                }
                if(c==-4){
                    qu.push_front(a&b);
                }
                if(c==-5){
                    qu.push_front(a^b);
                }
            }
        }
        if(qu.front()==ans){
            anscount+=1;
        }
        assert(qu.size()==1 && qu.front()>=0);
        qu.pop_front();
    }
    cout<<ans<<" "<<anscount<<endl;
    return 1;
}

#undef int
int main(){
	#define int long long
	while(solve()){}
}
accuracy: 1
time_ms: 60
memory_kb: 3456
final_rank: 9

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

using ll = long long;

ll func(string S) {
  ll N = S.length();
  
  if(N == 1) return S[0] - '0';
  
  assert(S[0] == '[');
  
  string T = S.substr(2, N-3);
  ll fir, sec;
  if(T[0] == '[') {
    ll l = 0, r;
    vector<ll> vec;
    for(ll i = 0; i < ssize(T); ++i) {
      if(T[i] == '[') vec.push_back(i);
      if(T[i] == ']') {
        ll v = vec.back();
        vec.pop_back();
        if(v == 0) r = i + 1;
      }
    }
    
    fir = func(T.substr(l, r));
    sec = func(T.substr(r));
  }
  else {
    fir = func(T.substr(0, 1));
    sec = func(T.substr(1));
  }
  
  char op = S[1];
  if(op == '+') return fir | sec;
  if(op == '*') return fir & sec;
  if(op == '^') return fir ^ sec;
  assert(0);
  return 0;
}

string ins(string S, string T) {
  string ret = S;
  for(auto &c : ret) {
    if(c == 'a') c = T[0];
    if(c == 'b') c = T[1];
    if(c == 'c') c = T[2];
    if(c == 'd') c = T[3];
  }
  return ret;
}

int main() {
  while(1) {
    string S; cin >> S;
    if(S == ".") break;
    
    string T; cin >> T;
    ll ans = func(ins(S, T));
    // cout << ans << endl;
    
    ll cnt = 0;
    for(ll i = 0; i < 10000; ++i) {
      string U = "";
      ll n = i;
      for(ll j = 0; j < 4; ++j) {
        U += ('0' + n % 10);
        n /= 10;
      }
      if(func(ins(S, U)) == ans) cnt += 1;
    }
    
    cout << ans << ' ' <<  cnt << endl;
    
  }
  return 0;
}
accuracy: 1
time_ms: 270
memory_kb: 3584
final_rank: 19

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

bool solve() {
  string S;
  cin >> S;
  if (S == ".") return false;
  auto HASH = [&] (const string &P) -> int {
    auto rec = [&] (auto rec, int l, int r) -> int {
      // for (int i = l; i < r; i++) cout << S[i];
      // cout << endl;
      if (r == l + 1) {
        assert('a' <= S[l] && S[l] <= 'd');
        return P[S[l] - 'a'] - '0';
      }
      assert(S[l] == '[');
      char op = S[l + 1];
      for (int i = l + 2, s = 0; i < r - 1; i++) {
        if (S[i] == '[') s++;
        else if (S[i] == ']') s--;
        if (s == 0) {
          int x = rec(rec, l + 2, i + 1);
          int y = rec(rec, i + 1, r - 1);
          return op == '+' ? x | y : op == '*' ? x & y : x ^ y;
        }
      }
      assert(false);
      return -1;
    };
    return rec(rec, 0, ssize(S));
  };
  string P;
  cin >> P;
  int HASH_P = HASH(P), ans = 0;
  for (int q = 0; q < 10000; q++) {
    string Q = to_string(q);
    while (Q.size() < 4) {
      Q = "0" + Q;
    }

    ans += HASH(Q) == HASH_P;
  }
  cout << HASH_P << ' ' << ans << '\n';
  return true;
}

int main() {
  while (solve()) {}
}
accuracy: 1
time_ms: 30
memory_kb: 3456
final_rank: 5

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

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

// #include <atcoder/all>
// using namespace atcoder;

#define rep(i, n) for (int i = 0; i < (n); ++i)
using ll = long long;

// #include "titan_cpplib/others/print.cpp"

class Parser {
public:
    using T = int;

    string s;
    int n, ptr;

    int tree_idx;
    int root;
    vector<T> val;
    vector<int> left, right;

    Parser() {}
    Parser(string s) : s(s), n(s.size()), ptr(0), tree_idx(0), root(-1), val(n), left(n, -1), right(n, -1) {}

    void consume(char expected) {
        if (next() != expected) {
            printf("Parser::Error: Expected `%c` but got `%c`\n", expected, next());
            assert(false);
        }
        ptr++;
    }

    char next() const {
        assert(ptr < n);
        return s[ptr];
    }

    bool done() const {
        return ptr == n;
    }

    bool is_digit(char c) const {
        return '0' <= c && c <= '9';
    }

    ll number() {
        // cerr << "number-start" << endl;
        ll ret = 0;
        while (!done() && is_digit(next())) {
            char v = next();
            ret *= 10;
            ret += v - '0';
            consume(v);
        }
        // cerr << "number-fin : " << ret << endl;
        return ret;
    }

    // ------------------------------------
    string t;

    void init() { ptr = 0; }

    /// @brief パースして返す
    T parse() {
        assert(ptr == 0);
        T ans = expr(0);
        assert(done());
        return ans;

        // root = build_tree();
    }

    int build_tree() {
        int idx = tree_idx;
        if (done()) {
            return -1;
        }

        if (next() == '(') {
            tree_idx++;
            consume('(');
            int ldx = build_tree();
            consume(')');
            left[idx] = ldx;

            consume('[');
            val[idx] = number();
            consume(']');

            consume('(');
            int rdx = build_tree();
            consume(')');
            right[idx] = rdx;
            return idx;
        }
        return -1;
    }

    void show_tree(int node, int dep) {
        if (node == -1) return;
        rep(_, dep) cerr << "  ";
        cerr << "node : " << val[node] << endl;
        if (left[node] != -1) {
            rep(_, dep) cerr << "  ";
            cerr << "left : " << val[left[node]] << endl;
        }
        if (right[node] != -1) {
            rep(_, dep) cerr << "  ";
            cerr << "right : " << val[right[node]] << endl;
        }
        show_tree(left[node], dep+1);
        show_tree(right[node], dep+1);
    }

    T expr(int level) {
        // rep(_, level) {
        //     cerr << "  ";
        // }
        // cerr << " " << next() << endl;

        if (next() == '[') {
            consume('[');
            char op = next();
            consume(op);
            T nxt0 = expr(level);
            T nxt1 = expr(level);
            consume(']');
            if (op == '+') return nxt0 | nxt1;
            if (op == '*') return nxt0 & nxt1;
            if (op == '^') return nxt0 ^ nxt1;
            assert(false);
        } else {
            int v = t[next()-'a'] - '0';
            consume(next());
            return v;
        }
    }
};

void solve() {
    string s;
    cin >> s;
    if (s == ".") exit(0);
    string t;
    cin >> t;

    Parser p(s);
    p.t = t;
    int res = p.parse();
    int ans = 0;

    rep(i, 10) rep(j, 10) rep(k, 10) rep(l, 10) {
        string u;
        u += '0' + i;
        u += '0' + j;
        u += '0' + k;
        u += '0' + l;
        Parser p(s);
        p.t = u;
        int u_res = p.parse();
        if (res == u_res) {
            ans++;
        }
    }

    cout << res << " " << ans << endl;
}

int main() {
    ios::sync_with_stdio(false);
    cin.tie(0);
    cout << fixed << setprecision(15);
    cerr << fixed << setprecision(15);

    int t = 1e9;// cin >> t;
    for (int i = 0; i < t; ++i) {
        solve();
    }

    return 0;
}
accuracy: 1
time_ms: 30
memory_kb: 3456
final_rank: 5

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

using namespace std;
using ll = long long;
using pll = pair<ll,ll>;
using bint = __int128_t;
#define rep(i, s, t) for (ll i = s; i < (ll)(t); i++)
#define rrep(i, s, t) for(ll i = (ll)(t) - 1; i >= (ll)(s); i--)
#define all(x) begin(x), end(x)
#define rall(x) rbegin(x), rend(x)
#define TT template<typename T>
TT using vec = vector<T>;
TT using minheap = priority_queue<T, vector<T>, greater<T>>;
TT using maxheap = priority_queue<T>;
template<class T> bool chmin(T &x, T y) { return x > y ? (x = y, true) : false; }
template<class T> bool chmax(T &x, T y) { return x < y ? (x = y, true) : false; }
struct io_setup {
    io_setup() {
        ios::sync_with_stdio(false);
        std::cin.tie(nullptr);
        cout << fixed << setprecision(15);
    }
} io_setup;

std::ostream &operator<<(std::ostream &dest, __int128_t value) {
  std::ostream::sentry s(dest);
  if (s) {
    __uint128_t tmp = value < 0 ? -value : value;
    char buffer[128];
    char *d = std::end(buffer);
    do {
      --d;
      *d = "0123456789"[tmp % 10];
      tmp /= 10;
    } while (tmp != 0);
    if (value < 0) {
      --d;
      *d = '-';
    }
    int len = std::end(buffer) - d;
    if (dest.rdbuf()->sputn(d, len) != len) {
      dest.setstate(std::ios_base::badbit);
    }
  }
  return dest;
}

__int128 parse(string &s) {
  __int128 ret = 0;
  for (size_t i = 0; i < s.length(); i++)
    if ('0' <= s[i] && s[i] <= '9')
      ret = 10 * ret + s[i] - '0';
  return ret;
}

std::istream &operator>>(std::istream &dest, __int128_t &value) {
    string s;
    dest >> s;
    value = parse(s);
    return dest;
}

void solve(string S,string A){
  int N=S.size();
  map<ll,int>mp;
  auto f=[&](auto f,int l,string u) -> pair<ll,int> {
    if('a'<=S[l]&&S[l]<='d'){
      return make_pair(u[S[l]-'a']-'0',l+1);
    }else{
      auto[vx,lx]=f(f,l+2,u);
      auto[vy,ly]=f(f,lx,u);
      if(S[l+1]=='+'){
        return make_pair(vx|vy,ly+1);
      }else if(S[l+1]=='*'){
        return make_pair(vx&vy,ly+1);
      }else{
        return make_pair(vx^vy,ly+1);
      }
    }
  };
  for(int v=0;v<10000;v++){
    string s=to_string(v);
    while(s.size()<4)s="0"+s;
    auto[val,r]=f(f,0,s);
    assert(r==N);
    mp[val]++;
  }
  int val=f(f,0,A).first;
  cout<<val<<" "<<mp[val]<<endl;
}

int main() {
  while(1){
    string S;
    cin>>S;
    if(S==".")break;
    string A;
    cin>>A;
    solve(S,A);
  }
}
accuracy: 1
time_ms: 40
memory_kb: 3584
final_rank: 16

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

ll return_hash(const string& s, const string& p){
    stack<char> op;
    stack<ll> num;
    rep(i, s.length()){
        if(s[i] == 'a')num.push(p[0] - '0');
        else if(s[i] == 'b')num.push(p[1] - '0');
        else if(s[i] == 'c')num.push(p[2] - '0');
        else if(s[i] == 'd')num.push(p[3] - '0');
        else op.push(s[i]);
        if(!op.empty() && op.top() == ']'){
            ll num2 = num.top();
            num.pop();
            ll num1 = num.top();
            num.pop();
            op.pop();
            char ope = op.top();
            op.pop();
            op.pop();
            if(ope == '+')num.push(num1 | num2);
            else if(ope == '*')num.push(num1 & num2);
            else if(ope == '^')num.push(num1 ^ num2);
        }
    }
    return num.top();
}

int main() {
    while(1){
        string s,p;
        cin >> s >> p;
        if(s == ".")break;
        ll hash = return_hash(s,p);
        ll ans = 0;
        for(ll i = 0; i <= 9999; i++){
            string pass = to_string(i);
            while(pass.length() < 4){
                pass.insert(0,"0");
            }
            if(hash == return_hash(s,pass))ans++;
        }
        cout << hash << " " << ans << endl;
    }
    return 0;
}
accuracy: 1
time_ms: 100
memory_kb: 3496
final_rank: 11

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

int main() {
	while (true) {
		string s, p; cin >> s;
		if (s == ".") return 0;
		cin >> p;
		s = '[' + s + ']';
		reverse(begin(s), end(s));

		int len = s.length();
		int ans = 0, cnt = 0;

		{
			vector<stack<int>> stk(1);
			REP(i, len) {
				if (s[i] == ']') stk.emplace_back(stack<int>{});
				else if (s[i] == '+') {
					assert(stk.back().size() >= 2);
					auto a = stk.back().top(); stk.back().pop();
					auto b = stk.back().top(); stk.back().pop();
					stk.back().emplace(a | b);
				} else if (s[i] == '*') {
					assert(stk.back().size() >= 2);
					auto a = stk.back().top(); stk.back().pop();
					auto b = stk.back().top(); stk.back().pop();
					stk.back().emplace(a & b);
				} else if (s[i] == '^') {
					assert(stk.back().size() >= 2);
					auto a = stk.back().top(); stk.back().pop();
					auto b = stk.back().top(); stk.back().pop();
					stk.back().emplace(a ^ b);
				} else if (s[i] == '[') {
					assert(stk.back().size());
					auto a = stk.back().top();
					stk.pop_back();
					stk.back().push(a);
				} else {
					// cerr << p[s[i] - 'a'] - '0' << endl;
					stk.back().push(p[s[i] - 'a'] - '0');
				}
			}
			ans = stk.back().top();
		}

		REP(ka, 10) REP(kb, 10) REP(kc, 10) REP(kd, 10) {
			vector<stack<int>> stk(1);
			REP(i, len) {
				if (s[i] == ']') stk.emplace_back(stack<int>{});
				else if (s[i] == '+') {
					assert(stk.back().size() >= 2);
					auto a = stk.back().top(); stk.back().pop();
					auto b = stk.back().top(); stk.back().pop();
					stk.back().emplace(a | b);
				} else if (s[i] == '*') {
					assert(stk.back().size() >= 2);
					auto a = stk.back().top(); stk.back().pop();
					auto b = stk.back().top(); stk.back().pop();
					stk.back().emplace(a & b);
				} else if (s[i] == '^') {
					assert(stk.back().size() >= 2);
					auto a = stk.back().top(); stk.back().pop();
					auto b = stk.back().top(); stk.back().pop();
					stk.back().emplace(a ^ b);
				} else if (s[i] == '[') {
					assert(stk.back().size());
					auto a = stk.back().top();
					stk.pop_back();
					stk.back().push(a);
				} else {
					stk.back().push(vector{ka, kb, kc, kd}[s[i] - 'a']);
				}
			}
			if (stk.back().top() == ans) cnt++;
		}

		cout << ans << " " << cnt << '\n';
	}
	// [+[*aa][+[+[+b[+aa]][^[^[^[+ba][*[*[^aa][^[+ab]a]][+bb]]]b][^[*ab]a]]][+bb]]]
}
accuracy: 1
time_ms: 570
memory_kb: 3456
final_rank: 20