Problem:
Problem E
Infallibly Crack Perplexing Cryptarithm
You are asked to crack an encrypted equation over binary numbers.
The original equation consists only of binary digits ("0" and "1"), operator symbols ("+", "-", and "*"), parentheses ("(" and ")"), and an equal sign ("="). The encryption replaces some occurrences of characters in an original arithmetic equation by letters. Occurrences of one character are replaced, if ever, by the same letter. Occurrences of two different characters are never replaced by the same letter. Note that the encryption does not always replace all the occurrences of the same character; some of its occurrences may be replaced while others may be left as they are. Some characters may be left unreplaced. Any character in the Roman alphabet, in either lowercase or uppercase, may be used as a replacement letter. Note that cases are significant, that is, "a" and "A" are different letters. Note that not only digits but also operator symbols, parentheses and even the equal sign are possibly replaced.
The arithmetic equation is derived from the start symbol of $Q$ of the following context-free grammar.
$Q ::= E=E$
$E ::= T | E+T | E-T$
$T ::= F | T*F $
$F ::= N | -F | (E) $
$N ::= 0 | 1B $
$B ::= \epsilon | 0B | 1B$
Here, $\epsilon$ means empty.
As shown in this grammar, the arithmetic equation should have one equal sign. Each side of the equation is an expression consisting of numbers, additions, subtractions, multiplications, and negations. Multiplication has precedence over both addition and subtraction, while negation precedes multiplication. Multiplication, addition and subtraction are left associative. For example,
x-y+z
means
(x-y)+z
, not
x-(y+z)
. Numbers are in binary notation, represented by a sequence of binary digits, 0 and 1. Multi-digit numbers always start with 1. Parentheses and negations may appear redundantly in the equation, as in
((--x+y))+z
.
Write a program that, for a given encrypted equation, counts the number of different possible original correct equations. Here, an equation should conform to the grammar and it is correct when the computed values of the both sides are equal.
For Sample Input 1, C must be = because any equation has one
=
between two expressions. Then, because
A
and
M
should be different, although there are equations conforming to the grammar, none of them can be correct.
For Sample Input 2, the only possible correct equation is
-0=0
.
For Sample Input 3 (B-A-Y-L-zero-R), there are three different correct equations,
0=-(0)
,
0=(-0)
, and
-0=(0)
. Note that one of the two occurrences of zero is not replaced with a letter in the encrypted equation.
Input
The input consists of a single test case which is a string of Roman alphabet characters, binary digits, operator symbols, parentheses and equal signs. The input string has at most 31 characters.
Output
Print in a line the number of correct equations that can be encrypted into the input string.
Sample Input 1
ACM
Sample Output 1
0
Sample Inp
...(truncated)

Submitted solutions:

code_snippet: #include <bits/stdc++.h>
using namespace std;
using ll = long long;
#define rep2(i, n) for (ll i = 0; i < (n); i++)
#define rep3(i, a, b) for (ll i = a; i < (b); i++)
#define overload(a, b, c, d, ...) d
#define rep(...) overload(__VA_ARGS__, rep3, rep2)(__VA_ARGS__)
#define all(a) begin(a), end(a)
#define sz(a) ssize(a)
bool chmin(auto& a, auto b) {
    return a > b ? a = b, 1 : 0;
}
bool chmax(auto& a, auto b) {
    return a < b ? a = b, 1 : 0;
}

struct ParseError {};

struct Parser {
    using CopyIter = string::const_iterator;
    using Iter = CopyIter&;

    string line;
    CopyIter ed;
    Parser(string s): line(s) {}
    bool parse() {
        // cerr << line << endl;
        ed = line.cend();
        auto it = line.cbegin();
        try {
            return equation(it);
        } catch(ParseError) {
            return 0;
        }
    }
    bool equation(Iter it) {
        auto L = expr(it);
        skip(it, '=');
        auto R = expr(it);
        if (it != ed) throw ParseError();
        // cerr << "calc " << L << ' ' << R << endl;
        return L == R;
    }
    void skip(Iter it, char c) {
        if (access(it) != c) throw ParseError();
        // cerr << access(it) << ' ' << c << endl;
        it++;
    }
    char access(Iter it) {
        if (it == ed) throw ParseError();
        return *it;
    }
    ll expr(Iter it) {
        auto res = term(it);
        while (it != ed && (*it == '+' || *it == '-')) {
            if (*it == '+') {
                skip(it, '+');
                res += term(it);
            } else {
                skip(it, '-');
                res -= term(it);
            }
        }
        return res;
    }
    ll term(Iter it) {
        auto res = factor(it);
        while (it != ed && *it == '*') {
            skip(it, '*');
            res *= factor(it);
        }
        return res;
    }
    ll factor(Iter it) {
        if (access(it) == '-') {
            skip(it, '-');
            auto res = factor(it);
            return -res;
        } else if (access(it) == '(') {
            skip(it, '(');
            auto res = expr(it);
            skip(it, ')');
            return res;
        } else {
            return number(it);
        }
    }
    ll number(Iter it) {
        if (access(it) != '0' && access(it) != '1') throw ParseError();
        if (access(it) == '0') {
            skip(it, '0');
            return 0;
        }
        ll res = 0;
        while (it != ed && isdigit(*it)) {
            res = 2 * res + (access(it) - '0');
            skip(it, *it);
        }
        return res;
    }
};

int main() {
    cin.tie(0)->sync_with_stdio(0);
    string line;
    getline(cin, line);
    const int N = 8;
    const string S = "01-+*=()";
    bool used[N] = {};
    char mapping[256] = {};
    int ans = 0;
    auto dfs = [&](auto&& self, int i) -> void {
        if (i == ssize(line)) {
            int delta = Parser(line).parse();
            // if (delta) cerr << line << endl;
            ans += delta;
            return;
        }
        if (isalpha(line[i])) {
            char prv = line[i];
            if (mapping[line[i]] == 0) {
                rep(j, 0, N) {
                    if (!used[j]) {
                        used[j] = true;
                        mapping[prv] = S[j];
                        line[i] = S[j];
                        self(self, i + 1);
                        line[i] = prv;
                        mapping[prv] = 0;
                        used[j] = false;
                    }
                }
            } else {
                line[i] = mapping[prv];
                self(self, i + 1);
                line[i] = prv;
            }
        } else {
            self(self, i + 1);
        }
    };
    dfs(dfs, 0);
    cout << ans << '\n';
}
accuracy: 1
time_ms: 70
memory_kb: 3584
final_rank: 6

code_snippet: #include <iostream>
#include <algorithm>
#include <vector>
#include <string>
#include <cassert>

char exs[]{'0', '1', '+', '-', '*', '(', ')', '='};

bool character(char c) {
    bool res{};
    for (int i{} ; i < 8 ; i++) {
        res |= exs[i] == c;
    }
    return !res;
}

bool equalcond(const std::string& S) {
    return std::count(S.begin(), S.end(), '=') == 1;
}

long long P10[60];

int log(long long v) {
    return (int)std::to_string(v).size();
}

struct parser {

    static constexpr long long NG{(long long)9e18};
    static constexpr long long EMPTY{-1};

    parser(const std::string& s) : S{s} {}

    bool fin() {
        return it == (int)S.size();
    }

    bool Q() {
        long long l{E()};
        if (l == NG) return false;
        if (fin() or S[it] != '=') return false;
        it++;
        long long r{E()};
        if (r == NG) return false;
        if (!fin()) return false;
        it++;
        // std::cout << S << std::endl;
        // std::cout << l << ' ' << r << std::endl;
        // if (l == r) {
        //     std::cout << S << std::endl;
        //     std::cout << l << ' ' << r << std::endl;
        // }
        return l == r;
    }

    long long E() {
        long long l{T()}; 
        // std::cout << "current " << l << std::endl;
        if (l == NG) return NG;
        while (!fin()) {
            if (S[it] == '+') {
                it++;
                long long r{T()};
                if (r == NG) return NG;
                l += r;
            }
            else if (S[it] == '-') {
                it++;
                long long r{T()};
                if (r == NG) return NG;
                l -= r;
            }
            else {
                break;
            }
            // std::cout << "current " << l << std::endl;
        }
        return l;
    }

    long long T() {
        long long l{F()};
        if (l == NG) return NG;
        while (!fin() and S[it] == '*') {
            it++;
            long long r{F()};
            if (r == NG) return NG;
            l *= r;
        }
        return l;
    }

    long long F() {
        if (fin()) return NG;
        if (S[it] == '-') {
            it++;
            long long f{F()};
            return f == NG ? NG : -f;
        }
        else if (S[it] == '(') {
            it++;
            if (fin()) return NG;
            long long res{E()};
            if (fin() or S[it] != ')') return NG;
            else {
                it++;
                return res;
            }
        }
        else {
            return N();
        }
    }

    long long N() {
        if (fin()) return NG;
        if (S[it] == '0') {
            it++;
            if (!fin() and (S[it] == '0' or S[it] == '1')) return NG;
            return 0LL;
        }
        else if (S[it] == '1') {
            long long res{};
            while (S[it] == '0' or S[it] == '1') {
                res <<= 1;
                if (S[it] == '1') res |= 1;
                it++;
            }
            return res;
        }
        else {
            return NG;
        }
    }

    int it{}; 
    std::string S;
};

bool check(std::string S) {
    for (auto c : S) assert(!character(c));
    if (!equalcond(S)) return false;
    return parser{S}.Q();
}

int main() {
    P10[0] = 1;
    for (int i{1} ; i < 60 ; i++) P10[i] = P10[i - 1] << 1;
    std::string S;
    std::cin >> S;
    std::vector<char> C;
    for (auto c : S) if (character(c)) C.push_back(c);
    std::sort(C.begin(), C.end());
    C.erase(std::unique(C.begin(), C.end()), C.end());
    if (C.size() > 8u) {
        std::cout << 0 << '\n';
        return 0;
    }
    std::vector<std::pair<char, char>> ch;
    auto change{[&]() -> std::string {
        std::string T{S};
        for (int i{} ; i < (int)T.size() ; i++) {
            for (int j{} ; j < (int)ch.size() ; j++) if (T[i] == ch[j].first) {
                T[i] = ch[j].second;
            }
        }
        return T;
    }};
    ch.reserve(C.size());
    int bit{};
    auto dfs{[&](auto dfs) -> int {
        if (ch.size() == (int)C.size()) {
            assert(__builtin_popcount(bit) == (int)C.size());
            return check(change());
        }
        int res{};
        int cur{(int)ch.size()};
        for (int i{} ; i < 8 ; i++) if (!(bit & (1 << i))) {
            bit ^= 1 << i;
            ch.push_back({C[cur], exs[i]});
            res += dfs(dfs);
            ch.pop_back();
            bit ^= 1 << i; 
        }
        return res;
    }};
    std::cout << dfs(dfs) << '\n';
}
accuracy: 1
time_ms: 10
memory_kb: 3452
final_rank: 1

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

using namespace std;

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

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

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

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

/**
 * @brief template
 */

ll ANS = 0;
int K = 8;
string Moji = "01+-*()=";

pair<ll,bool> Number(string,int&);
pair<ll,bool> Factor(string,int&);
pair<ll,bool> Term(string,int&);
pair<ll,bool> Expr(string,int&);

pair<ll,bool> Number(string S, int& it) {
    //cout << "N" << ' ' << S << ' ' << it << endl;
    if (it == S.size()) return {0,false};
    if (!isdigit(S[it])) return {0,false};
    if (S[it] == '0') {
        it++;
        return {0,true};
    }
    ll Ret = 0;
    while(it != S.size() && isdigit(S[it])) {
        Ret *= 2;
        Ret += S[it] - '0';
        it++;
    }
    return {Ret,true};
}

pair<ll,bool> Factor(string S, int& it) {
    //cout << "F" << ' ' << S << ' ' << it << endl;
    if (it == S.size()) return {0,false};
    if (isdigit(S[it])) return Number(S,it);
    else if (S[it] == '-') {
        it++;
        auto [Ret,B] = Factor(S,it);
        if (!B) return {0,false};
        return {-Ret,true};
    }
    else if (S[it] == '(') {
        it++;
        auto [Ret,B] = Expr(S,it);
        if (!B) return {0,false};
        if (it == S.size() || S[it] != ')') return {0,false};
        it++;
        return {Ret,B};
    }
    else return {0,false};
}

pair<ll,bool> Term(string S, int& it) {
    //cout << "T" << ' ' << S << ' ' << it << endl;
    if (it == S.size()) return {0,false};
    auto [Ret,B] = Factor(S,it);
    if (!B) return {0,false};
    while(it != S.size() && S[it] != ')' && S[it] != '+' && S[it] != '-') {
        if (S[it] == '*') {
            it++;
            auto [Ret2, B2] = Factor(S,it);
            if (!B2) return {0,false};
            Ret *= Ret2;
        }
        else return {0,false};
    }
    return {Ret,true};
}

pair<ll,bool> Expr(string S, int& it) {
    //cout << "E" << ' ' << S << ' ' << it << endl;
    if (it == S.size()) return {0,false};
    auto [Ret,B] = Term(S,it);
    if (!B) return {0,false};
    while(it != S.size() && S[it] != ')') {
        if (S[it] == '+') {
            it++;
            auto [Ret2, B2] = Term(S,it);
            if (!B2) return {0,false};
            Ret += Ret2;
        }
        else if (S[it] == '-') {
            it++;
            auto [Ret2, B2] = Term(S,it);
            if (!B2) return {0,false};
            Ret -= Ret2;
        }
        else return {0,false};
    }
    return {Ret,true};
}

bool Solve(string S) {
    vector<int> Eq;
    rep(i,0,S.size()) {
        if (S[i] == '=') Eq.push_back(i);
    }
    if (Eq.size() != 1) return false;
    int ID = Eq[0];
    int it = 0;
    auto Left = Expr(S.substr(0,ID),it);
    if (it != ID) return false;
    it = 0;
    auto Right = Expr(S.substr(ID+1,S.size()-ID-1),it);
    if (it != S.size()-ID-1) return false;
    return (Left.second && Right.second && Left.first == Right.first);
}

void DFS(string S, vector<bool> used) {
    rep(i,0,S.size()) {
        if (isalpha(S[i])) {
            char C = S[i];
            rep(j,0,K) {
                string T = S;
                if (used[j]) continue;
                rep(k,0,S.size()) {
                    if (T[k] == C) T[k] = Moji[j];
                }
                used[j] = true;
                DFS(T,used);
                used[j] = false;
            }
            return;
        }
    }
    if (Solve(S)) {
        //cout << S << endl;
        ANS++;
    }
}

int main() {
    string S;
    cin >> S;
    vector<bool> used(8,false);
    DFS(S,used);
    cout << ANS << endl;
}
accuracy: 1
time_ms: 20
memory_kb: 3440
final_rank: 3

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

string s;
int n, i;
int E();
int T();
int F();
int N();

int E() {
    int ans = T();
    while(i < n) {
        if(s[i] == '+') {
            i++;
            ans += T();
        } else if(s[i] == '-') {
            i++;
            ans -= T();
        } else break;
    }
    return ans;
}

int T() {
    int ans = F();
    while(i < n) {
        if(s[i] == '*') {
            i++;
            ans *= F();
        } else break;
    }
    return ans;
}

int F() {
    if(s[i] == '-') {
        i++;
        return -F();
    } else if(s[i] == '(') {
        i++;
        int ans = E();
        if(i == n or s[i] != ')') throw logic_error("");
        i++;
        return ans;
    } else if(isdigit(s[i])) {
        return N();
    } else throw logic_error("");
}

int N() {
    if(s[i] == '0' and i + 1 < n and isdigit(s[i + 1])) throw logic_error("");
    int ans = 0;
    while(i < n and isdigit(s[i])) {
        ans <<= 1;
        ans += (s[i] - '0');
        i++;
    }
    return ans;
}


int main() {
    string text; cin >> text;
    const int len = text.size();
    string symbols = "01+-*()=";
    sort(symbols.begin(), symbols.end());

    string chars = "";
    for(char c : text) {
        if(not binary_search(symbols.begin(), symbols.end(), c)) {
            chars.push_back(c);
        }
    }
    sort(chars.begin(), chars.end());
    chars.erase(unique(chars.begin(), chars.end()), chars.end());

    set<string> vis;
    int ans = 0;
    do {
        string cur_text = text;
        for(char& c : cur_text) {
            for(int i = 0; i < int(chars.size()); i++) {
                if(c == chars[i]) {
                    c = symbols[i];
                    break;
                }
            }
        }

        const auto [eq_pos, pos_cnt] = [&] {
            int pos = -1, cnt = 0;
            for(int i = 0; i < len; i++) {
                if(cur_text[i] == '=') {
                    pos = i;
                    cnt++;
                }
            }
            return make_pair(pos, cnt);
        }();
        if(pos_cnt != 1) continue;
        if(eq_pos == 0 or eq_pos == len - 1) continue;
        if(vis.count(cur_text)) continue;
        vis.insert(cur_text);

        try {
            s = cur_text.substr(0, eq_pos);
            n = s.size();
            i = 0;
            const int L = E();
            if(i != n) throw logic_error("");

            s = cur_text.substr(eq_pos + 1);
            n = s.size();
            i = 0;
            const int R = E();
            if(i != n) throw logic_error("");

            ans += (L == R);
        } catch(...) {}
    } while(next_permutation(symbols.begin(), symbols.end()));

    cout << ans << endl;
    return 0;
}
accuracy: 1
time_ms: 90
memory_kb: 7832
final_rank: 10

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

#define rep(i, n) for (int i = 0; i < (n); i++)
#define per(i, n) for (int i = (n)-1; i >= 0; i--)
#define rep2(i, l, r) for (int i = (l); i < (r); i++)
#define per2(i, l, r) for (int i = (r)-1; i >= (l); i--)
#define each(e, x) for (auto &e : x)
#define sz(x) (int)x.size()
#define all(x) begin(x), end(x)
#define rall(x) rbegin(x), rend(x)
#define eb emplace_back
#define MM << ' ' <<
#define TT template <typename T>
using ll = long long;
using pii = pair<int, int>;
using pll = pair<ll, ll>;

TT bool chmin(T &x, T y) { return x > y ? (x = y, true) : false; }
TT bool chmax(T &x, T y) { return x < y ? (x = y, true) : false; }

TT using minheap = priority_queue<T, vector<T>, greater<T>>;
TT using maxheap = priority_queue<T>;

TT void print(vector<T> v, T x = 0) {
    int n = sz(v);
    rep(i, n) cout << v[i] + x << (i == n - 1 ? '\n' : ' ');
    if (n == 0) cout << '\n';
}

TT void printn(vector<T> v, T x = 0) {
    int n = sz(v);
    rep(i, n) cout << v[i] + x << '\n';
}

TT void rearrange(vector<T> &v) {
    sort(all(v));
    v.erase(unique(all(v)), end(v));
}

TT int lb(vector<T> &v, T x) { return lower_bound(all(v), x) - begin(v); }
TT int ub(vector<T> &v, T x) { return upper_bound(all(v), x) - begin(v); }

const int inf = (1 << 30) - 1;
const ll INF = (1LL << 60) - 1;

struct io_setup {
    io_setup() {
        ios_base::sync_with_stdio(false);
        cin.tie(NULL);
        cout << fixed << setprecision(15);
    }
} io_setup;

struct parser {
    string s;
    parser(string s) : s(s) {}

    using Eval = pair<bool, ll>;
    const Eval iv = pair(false, -1);

    char get(int idx) {
        return idx >= sz(s) ? '$' : s[idx];
    }

    bool Q() {
        int idx = 0;
        auto ret_l = E(idx);
        if (!ret_l.first)
            return false;
        idx++;
        if (get(idx) != '=')
            return false;
        idx++;
        auto ret_r = E(idx);
        if (!ret_r.first)
            return false;
        idx++;
        if (idx != sz(s))
            return false;
        return ret_l.second == ret_r.second;
    }

    Eval E(int &idx) {
        auto ret = T(idx);
        if (!ret.first)
            return iv;
        while (1) {
            char op = get(idx + 1);
            if (op == '+' || op == '-') {
                idx += 2;
                auto val = T(idx);
                if (!val.first)
                    return iv;
                ret.second += (op == '+' ? val.second : -val.second);
            }
            else
                break;
        }
        return ret;
    }

    Eval T(int &idx) {
        auto ret = F(idx);
        if (!ret.first)
            return iv;
        while (1) {
            char op = get(idx + 1);
            if (op == '*') {
                idx += 2;
                auto val = F(idx);
                if (!val.first)
                    return iv;
                ret.second *= val.second;
            }
            else
                break;
        }
        return ret;
    }

    Eval F(int &idx) {
        char op = get(idx);
        if (op == '-') {
            idx++;
            auto ret = F(idx);
            ret.second *= -1;
            return ret;
        }
        else if (op == '(') {
            idx++;
            auto ret = E(idx);
            idx++;
            if (get(idx) != ')')
                return iv;
            return ret;
        }
        else
            return N(idx);
    }

    Eval N(int &idx) {
        auto fd = get(idx);
        if (fd == '0')
            return pair(true, 0);
        else if (fd == '1') {
            idx++;
            return B(idx);
        }
        else
            return iv;
    }

    Eval B(int &idx, ll v = 1) {
        char d = get(idx);
        if (d == '0' || d == '1') {
            v = v * 2 + (d - '0');
            idx++;
            return B(idx, v);
        }
        else {
            idx--;
            return pair(true, v);
        }
    }
};

int main() {
    string s;
    cin >> s;
    int n = sz(s);
    map<char, vector<int>> m;
    rep(i, n) if (isalpha(s[i])) m[s[i]].eb(i);
    string symbol = "=+-*()01";
    int k = sz(m), l = sz(symbol);
    int ans = 0;
    auto dfs = [&](int idx, vector<int> p, auto &&dfs) ->void {
        if (idx == k) {
            string x = s;
            int i = 0;
            for (auto [c, ls] : m) {
                each(e, ls) x[e] = symbol[p[i]];
                i++;
            }
            parser q(x);
            if (q.Q())
                ans++;
            return;
        }
        rep(t, l) {
            bool ok = true;
            rep(i, idx) if (p[i] == t) ok = false;
            if (ok) {
                p[idx] = t;
                dfs(idx + 1, p, dfs);
            }
        }
    };
    dfs(0, vector<int>(k), dfs);
    cout << ans << endl;
}
accuracy: 1
time_ms: 10
memory_kb: 3460
final_rank: 2

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

using ll = long long;

pair<ll, bool> parseN(const string&, int&);
pair<ll, bool> parseF(const string&, int&);
pair<ll, bool> parseT(const string&, int&);
pair<ll, bool> parseE(const string&, int&);

pair<ll, bool> parseN(const string& s, int& i) {
    vector<int> bits;

    while(i < s.size() && (s[i] == '0' || s[i] == '1')) {
        bits.push_back(s[i] - '0');
        i++;
    }

    if(bits.empty()) return {0, false};
    if(bits.size() > 1 && bits[0] == 0) return {0, false};

    ll ret = 0;
    for (auto bit: bits) {
        ret *= 2;
        ret += bit;
    }

    return {ret, true};
}

pair<ll, bool> parseF(const string& s, int& i) {
    if(i >= s.size()) return {0, false};

    if(s[i] == '-') {
        i++;
        auto ret = parseF(s, i);
        ret.first *= -1;
        return ret;
    }

    if(s[i] == '(') {
        i++;
        auto [e, ok] = parseE(s, i);
        if(!ok) return {0, false};
        if(i >= s.size()) return {0, false};
        if(s[i] != ')') return {0, false};
        i++;
        return {e, true};
    }

    return parseN(s, i);
}

pair<ll, bool> parseT(const string& s, int& i) {
    ll ret = 0;

    ll f = 0;
    bool ok = true;

    tie(f, ok) = parseF(s, i);
    if(!ok) return {0, false};
    ret = f;

    while(i < s.size() && s[i] == '*') {
        i++;

        tie(f, ok) = parseF(s, i);
        if(!ok) return {0, false};

        ret *= f;
    }

    return {ret, true};
}

pair<ll, bool> parseE(const string& s, int& i) {
    ll ret = 0;

    ll t;
    bool ok;

    tie(t, ok) = parseT(s, i);
    if(!ok) return {0, false};
    ret = t;

    while (i < s.size() && (s[i] == '+' || s[i] == '-')) {
        bool addition = s[i] == '+';
        i++;

        tie(t, ok) = parseT(s, i);
        if(!ok) return {0, false};

        if(addition) ret += t;
        else ret -= t;
    }

    return {ret, true};
}

pair<bool, bool> parseQ(const string& s) {
    int i = 0;

    bool ok;
    ll e1, e2;

    tie(e1, ok) = parseE(s, i);
    if(!ok) return {false, false};

    if(i >= s.size()) return {false, false};
    if(s[i] != '=') return {false, false};
    i++;

    tie(e2, ok) = parseE(s, i);
    if(!ok) return {false, false};
    if(i != s.size()) return {false, false};

    return {e1 == e2, true};
}

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

    set<char> char_set;
    for (int i = 0; i < s.size(); i++) char_set.insert(s[i]);

    const string valid_chars = "01+-*()=";
    vector<char> before_chars;
    for (int i = 0; i < valid_chars.size(); i++) {
        if(char_set.count(valid_chars[i])) {
            char_set.erase(valid_chars[i]);
        }
        before_chars.push_back(valid_chars[i]);
    }

    vector<char> after_chars;
    for (auto c: char_set) after_chars.push_back(c);

    if(after_chars.size() > valid_chars.size()) {
        cout << 0 << endl;
        return 0;
    }

    assert(before_chars.size() >= after_chars.size());

    if (after_chars.empty()) {
        auto [eq, ok] = parseQ(s);
        if(eq && ok) cout << 1 << endl;
        else cout << 0 << endl;
        return 0;
    }

    int ans = 0;

    sort(before_chars.begin(), before_chars.end());
    map<string, bool> done;
    do {
        string new_s = s;

        for (int i = 0; i < new_s.size(); i++) {
            for (int j = 0; j < after_chars.size(); j++) {
                if(new_s[i] == after_chars[j]) {
                    new_s[i] = before_chars[j];
                }
            }
        }

        if(done[new_s])continue;
        done[new_s] = true;

        auto [eq, ok] = parseQ(new_s);
        if(eq && ok) ans++;

    }while(next_permutation(before_chars.begin(),before_chars.end()));

    cout << ans << endl;
}
accuracy: 1
time_ms: 20
memory_kb: 8492
final_rank: 9

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

#define all(a) begin(a), end(a)
#define rall(a) rbegin(a), rend(a)
#define uniq(a) (a).erase(unique(all(a)), (a).end())
#define SZ(x) ((int)(x).size())
#define pb(x) push_back(x)
#define eb(x) emplace_back(x)
#define vsum(x) reduce(all(x))
#define vmax(a) *max_element(all(a))
#define vmin(a) *min_element(all(a))
#define LB(c, x) distance((c).begin(), lower_bound(all(c), (x)))
#define UB(c, x) distance((c).begin(), upper_bound(all(c), (x)))
#define mp make_pair
#define endl '\n'
using ll = long long;
using ull = unsigned long long;
using ld = long double;
using Pi = pair<int, int>;
using Pl = pair<ll, ll>;
using Vi = vector<int>;
using Vl = vector<ll>;
using Vc = vector<char>;
using VVi = vector<vector<int>>;
using VVl = vector<vector<ll>>;
using VVc = vector<vector<char>>;
template <typename T, typename U> using P = pair<T, U>;
template <typename T> using V = vector<T>;
template <typename T> using VV = V<V<T>>;
constexpr ll inf = 1000000000ll;
constexpr ll INF = 4000000004000000000LL;
constexpr ld eps = 1e-15;
constexpr ld PI = 3.141592653589793;
constexpr int popcnt(ull x) { return __builtin_popcountll(x); }
template <typename T> using mat = vector<vector<T>>;
constexpr ll dy[9] = {0, 1, 0, -1, 1, 1, -1, -1, 0};
constexpr ll dx[9] = {1, 0, -1, 0, 1, -1, -1, 1, 0};
constexpr ll sign(ll a) { return (a > 0) - (a < 0); }
constexpr ll fdiv(ll a, ll b) { return a / b - ((a ^ b) < 0 && a % b); }
constexpr ll cdiv(ll a, ll b) { return -fdiv(-a, b); }
constexpr ull bit(int n) { return 1ull << n; }
template <typename T> constexpr T mypow(T x, ll n) {
    T ret = 1;
    while (n) {
        if (n & 1) ret *= x;
        x *= x;
        n >>= 1;
    }
    return ret;
}
constexpr ll modpow(ll x, ll n, ll mod) {
    ll ret = 1;
    while (n) {
        if (n & 1) ret *= x;
        x *= x;
        n >>= 1;
        x %= mod;
        ret %= mod;
    }
    return ret;
}
template <typename T> T xor64(T lb, T ub) {
    static ull x = 88172645463325252ull;
    x ^= x << 7;
    return lb + (x ^= x >> 9) % (ub - lb);
}
constexpr ll safemod(ll x, ll mod) { return (x % mod + mod) % mod; }
template <typename T> constexpr T sq(const T &a) { return a * a; }
template <typename T> using priority_queue_rev = priority_queue<T, vector<T>, greater<T>>;
template <typename T, typename U> bool chmax(T &a, const U &b) { return a < b ? a = b, true : false; }
template <typename T, typename U> bool chmin(T &a, const U &b) { return a > b ? a = b, true : false; }
template <typename T> T make_vector(T &&a) { return a; }
template <typename... Ts> auto make_vector(int h, Ts &&... ts) { return vector(h, make_vector(ts...)); }
template <typename T, typename U> ostream &operator<<(ostream &os, const pair<T, U> &a) {
    os << "(" << a.first << ", " << a.second << ")";
    return os;
}
template <typename T, typename U, typename V> ostream &operator<<(ostream &os, const tuple<T, U, V> &a) {
    os << "(" << get<0>(a) << ", " << get<1>(a) << ", " << get<2>(a) << ")";
    return os;
}
template <typename T> ostream &operator<<(ostream &os, const vector<T> &a) {
    os << "(";
    for (auto itr = a.begin(); itr != a.end(); ++itr) os << *itr << (next(itr) != a.end() ? ", " : "");
    os << ")";
    return os;
}
template <typename T> ostream &operator<<(ostream &os, const set<T> &a) {
    os << "(";
    for (auto itr = a.begin(); itr != a.end(); ++itr) os << *itr << (next(itr) != a.end() ? ", " : "");
    os << ")";
    return os;
}
template <typename T> ostream &operator<<(ostream &os, const multiset<T> &a) {
    os << "(";
    for (auto itr = a.begin(); itr != a.end(); ++itr) os << *itr << (next(itr) != a.end() ? ", " : "");
    os << ")";
    return os;
}
template <typename T, typename U> ostream &operator<<(ostream &os, const map<T, U> &a) {
    os << "(";
    for (auto itr = a.begin(); itr != a.end(); ++itr) os << *itr << (next(itr) != a.end() ? ", " : "");
    os << ")";
    return os;
}
template <typename T> void print(const T &a) { cout << a << endl; }
template <typename T> void print(const vector<T> &v) {
    for (auto &e : v) cout << e << " ";
    cout << endl;
}
template <typename T> void scan(vector<T> &a) {
    for (auto &i : a) cin >> i;
}
struct timer {
    clock_t start_time;
    void start() { start_time = clock(); }
    int lap() {
        // return x ms.
        return (clock() - start_time) * 1000 / CLOCKS_PER_SEC;
    }
};
template <typename T = int> struct Edge {
    int from, to;
    T cost;
    int idx;

    Edge() = default;

    Edge(int from, int to, T cost = 1, int idx = -1) : from(from), to(to), cost(cost), idx(idx) {}

    operator int() const { return to; }
};
template <typename T = int> struct Graph {
    vector<vector<Edge<T>>> g;
    int es;

    Graph() = default;

    explicit Graph(int n) : g(n), es(0) {}

    size_t size() const { return g.size(); }

    void add_directed_edge(int from, int to, T cost = 1) { g[from].emplace_back(from, to, cost, es++); }

    void add_edge(int from, int to, T cost = 1) {
        g[from].emplace_back(from, to, cost, es);
        g[to].emplace_back(to, from, cost, es++);
    }

    void read(int M, int padding = -1, bool weighted = false, bool directed = false) {
        for (int i = 0; i < M; i++) {
            int a, b;
            cin >> a >> b;
            a += padding;
            b += padding;
            T c = T(1);
            if (weighted) cin >> c;
            if (directed)
                add_directed_edge(a, b, c);
            else
                add_edge(a, b, c);
        }
    }
};
#ifdef ONLINE_JUDGE
#define dump(...) (void(0))
#else
void debug() { cerr << endl; }
template <typename Head, typename... Tail> void debug(Head &&head, Tail &&... tail) {
    cerr << head;
    if (sizeof...(Tail)) cerr << ", ";
    debug(tail...);
}
#define dump(...) cerr << __LINE__ << ": " << #__VA_ARGS__ << " = ", debug(__VA_ARGS__)
#endif
struct rep {
    struct itr {
        ll v;
        itr(ll v) : v(v) {}
        void operator++() { ++v; }
        ll operator*() const { return v; }
        bool operator!=(itr i) const { return v < *i; }
    };
    ll l, r;
    rep(ll l, ll r) : l(l), r(r) {}
    rep(ll r) : rep(0, r) {}
    itr begin() const { return l; };
    itr end() const { return r; };
};
struct per {
    struct itr {
        ll v;
        itr(ll v) : v(v) {}
        void operator++() { --v; }
        ll operator*() const { return v; }
        bool operator!=(itr i) const { return v > *i; }
    };
    ll l, r;
    per(ll l, ll r) : l(l), r(r) {}
    per(ll r) : per(0, r) {}
    itr begin() const { return r - 1; };
    itr end() const { return l - 1; };
};
struct io_setup {
    static constexpr int PREC = 20;
    io_setup() {
        cout << fixed << setprecision(PREC);
        cerr << fixed << setprecision(PREC);
    };
} iOS;

template <ll MOD = 1000000007> struct modint {
    ll val;
    modint(ll val = 0) : val(val >= 0 ? val % MOD : (MOD - (-val) % MOD) % MOD) {}
    static ll mod() { return MOD; }
    modint inv() const {
        ll a = val, b = MOD, u = 1, v = 0, t;
        while (b > 0) {
            t = a / b;
            swap(a -= t * b, b);
            swap(u -= t * v, v);
        }
        return modint(u);
    }
    modint pow(ll k) const {
        modint ret = 1, mul = val;
        while (k) {
            if (k & 1) ret *= mul;
            mul *= mul;
            k >>= 1;
        }
        return ret;
    }
    modint &operator+=(const modint &a) {
        if ((val += a.val) >= MOD) val -= MOD;
        return *this;
    }
    modint &operator-=(const modint &a) {
        if ((val += MOD - a.val) >= MOD) val -= MOD;
        return *this;
    }
    modint &operator*=(const modint &a) {
        (val *= a.val) %= MOD;
        return *this;
    }
    modint &operator/=(const modint &a) { return *this *= a.inv(); }
    modint operator+() const { return *this; }
    modint operator-() const { return modint(-val); }
    friend bool operator==(const modint &a, const modint &b) { return a.val == b.val; }
    friend bool operator!=(const modint &a, const modint &b) { return rel_ops::operator!=(a, b); }
    friend modint operator+(const modint &a, const modint &b) { return modint(a) += b; }
    friend modint operator-(const modint &a, const modint &b) { return modint(a) -= b; }
    friend modint operator*(const modint &a, const modint &b) { return modint(a) *= b; }
    friend modint operator/(const modint &a, const modint &b) { return modint(a) /= b; }
    friend istream &operator>>(istream &is, modint &a) {
        ll val;
        is >> val;
        a = modint(val);
        return is;
    }
    friend ostream &operator<<(ostream &os, const modint &a) { return os << a.val; }
};

using state = string::const_iterator;
struct parse_error {};

void consume(state &cur, char expected) {
    if (*cur == expected) {
        ++cur;
    } else {
        // cerr << "Expected '" << expected << "' but got '" << *cur << "'" << endl;
        // cerr << "Rest string is '";
        // while (*cur) cerr << *cur++;
        // cerr << "'" << endl;
        throw parse_error();
    }
}

struct parser {
    bool equation(state &cur) {
        ll lval = expression(cur);
        consume(cur, '=');
        ll rval = expression(cur);
        return lval == rval;
    }
    ll expression(state &cur) {
        ll ret = term(cur);
        while (*cur == '+' || *cur == '-') {
            if (*cur == '+') {
                consume(cur, '+');
                ret += term(cur);
            } else if (*cur == '-') {
                consume(cur, '-');
                ret -= term(cur);
            }
        }
        return ret;
    }
    ll term(state &cur) {
        ll ret = factor(cur);
        while (*cur == '*') {
            consume(cur, '*');
            ret *= factor(cur);
        }
        return ret;
    }
    ll factor(state &cur) {
        if (*cur == '-') {
            consume(cur, '-');
            return -factor(cur);
        } else if (*cur == '(') {
            consume(cur, '(');
            ll ret = expression(cur);
            consume(cur, ')');
            return ret;
        } else {
            return number(cur);
        }
    }
    ll number(state &cur) {
        if (*cur == '0') {
            consume(cur, '0');
            return 0;
        }
        consume(cur, '1');
        ll ret = 1;
        while (*cur == '0' || *cur == '1') {
            ret *= 2;
            ret += *cur - '0';
            ++cur;
        }
        return ret;
    }
};

int main() {
    string s;
    cin >> s;
    vector<char> vc;
    for (char c : s) {
        if ('a' <= c && c <= 'z' || 'A' <= c && c <= 'Z') vc.push_back(c);
    }
    sort(all(vc)), uniq(vc);
    string allocation = "01+-*()=";
    sort(all(allocation));
    if (vc.size() > allocation.size()) {
        cout << 0 << endl;
        return 0;
    }
    set<string> valid;
    do {
        string alloc = allocation.substr(0, vc.size());
        string t = s;
        for (ll i : rep(vc.size())) {
            for (char &c : t) {
                if (c == vc[i]) c = alloc[i];
            }
        }
        parser p;
        state b = begin(t);
        try {
            if (p.equation(b) && b == end(t)) valid.emplace(alloc);
        } catch (parse_error e) {}
    } while (next_permutation(all(allocation)));
    // dump(valid);
    cout << valid.size() << endl;
}
accuracy: 1
time_ms: 60
memory_kb: 3544
final_rank: 5

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

using namespace std;

class parse_error {};

class Parser{
    string S;
    public:
    int pos = 0;
    Parser(string S) : S(S){}

    char peek(){
        return pos < S.size() ? S[pos] : 'a';
    }

    void skip_spaces(){
        while (peek() == ' ')
            pos++;
    }

    void next(string S){
        for (const auto &s: S){
            if (peek() != s)
                throw parse_error();
            pos++;
        }
    }

    int64_t expr(){
        auto left = term();
        while (peek() == '+' or peek() == '-'){
            char ope = peek();
            pos++;
            auto right = term();
            if (ope == '+')
                left += right;
            else
                left -= right;
        }

        return left;
    }

    int64_t term(){
        auto left = factor();
        while (peek() == '*'){
            char ope = peek();
            pos++;
            auto right = factor();
            left *= right;
        }

        return left;
    }

    int64_t factor(){
        bool is_minus = false;
        while (peek() == '-'){
            next("-");
            is_minus ^= 1;
        }

        int64_t res;

        if (peek() == '('){
            next("(");
            res = expr();
            next(")");
        } else {
            res = number();
        }

        return is_minus ? -res : res;
    }

    int64_t number(){
        if (!isdigit(peek()))
            throw parse_error();
        if (peek() == '0'){
            pos++;
            return 0;
        }
        int64_t res = 0;
        while (isdigit(peek())){
            res = res * 2 + peek() - '0';
            pos++;
        }

        return res;
    }
};

vector<string> split(string S, char delim){
    vector<string> res;
    int prev = -1;
    for (int i = 0; i < S.size(); i++){
        if (S[i] == delim){
            res.push_back(S.substr(prev + 1, i - prev - 1));
            prev = i;
        }
    }
    res.push_back(S.substr(prev + 1, int(S.size()) - prev - 1));
    return res;
}

int main(){
    string S;
    cin >> S;
    string symbol = "01+-*()=";
    sort(symbol.begin(), symbol.end());
    string encryption = "";
    for (const auto &s: S){
        if (symbol.find(s) == string::npos)
            encryption += s;
    }
    sort(encryption.begin(), encryption.end());
    encryption.erase(unique(encryption.begin(), encryption.end()), encryption.end());

    if (encryption.size() > symbol.size()){
        puts("0");
        return 0;
    }

    set<string> ans;

    do {
        string T = S;
        for (int i = 0; i < encryption.size(); i++)
            replace(T.begin(), T.end(), encryption[i], symbol[i]);
        
        if (count(T.begin(), T.end(), '=') != 1)
            continue;
        auto sp = split(T, '=');
        auto left = sp[0];
        auto right = sp[1];
        Parser pl(left), pr(right);
        int64_t vl, vr;
        try {
            vl = pl.expr();
            vr = pr.expr();
        } catch (parse_error e) {
            continue;
        }
        if (pl.pos != left.size() or pr.pos != right.size())
            continue;

        if (vl != vr)
            continue;
        ans.insert(T);
    } while (next_permutation(symbol.begin(), symbol.end()));

    cout << ans.size() << endl;
}
accuracy: 1
time_ms: 80
memory_kb: 3540
final_rank: 8

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

using namespace std;

class parse_error {};

class Parser{
    string S;
    public:
    int pos = 0;
    Parser(string S) : S(S){}

    char peek(){
        return pos < S.size() ? S[pos] : 'a';
    }

    void skip_spaces(){
        while (peek() == ' ')
            pos++;
    }

    void next(string S){
        for (const auto &s: S){
            if (peek() != s)
                throw parse_error();
            pos++;
        }
    }

    int64_t expr(){
        auto left = term();
        while (peek() == '+' or peek() == '-'){
            char ope = peek();
            pos++;
            auto right = term();
            if (ope == '+')
                left += right;
            else
                left -= right;
        }

        return left;
    }

    int64_t term(){
        auto left = factor();
        while (peek() == '*'){
            char ope = peek();
            pos++;
            auto right = factor();
            left *= right;
        }

        return left;
    }

    int64_t factor(){
        bool is_minus = false;
        while (peek() == '-'){
            next("-");
            is_minus ^= 1;
        }

        int64_t res;

        if (peek() == '('){
            next("(");
            res = expr();
            next(")");
        } else {
            res = number();
        }

        return is_minus ? -res : res;
    }

    int64_t number(){
        if (!isdigit(peek()))
            throw parse_error();
        if (peek() == '0')
            return 0;
        int64_t res = 0;
        while (isdigit(peek())){
            res = res * 2 + peek() - '0';
            pos++;
        }

        return res;
    }
};

vector<string> split(string S, char delim){
    vector<string> res;
    int prev = -1;
    for (int i = 0; i < S.size(); i++){
        if (S[i] == delim){
            res.push_back(S.substr(prev + 1, i - prev - 1));
            prev = i;
        }
    }
    res.push_back(S.substr(prev + 1, int(S.size()) - prev - 1));
    return res;
}

int main(){
    string S;
    cin >> S;
    string symbol = "01+-*()=";
    sort(symbol.begin(), symbol.end());
    string encryption = "";
    for (const auto &s: S){
        if (symbol.find(s) == string::npos)
            encryption += s;
    }
    sort(encryption.begin(), encryption.end());
    encryption.erase(unique(encryption.begin(), encryption.end()), encryption.end());

    if (encryption.size() > symbol.size()){
        puts("0");
        return 0;
    }

    set<string> ans;

    do {
        string T = S;
        for (int i = 0; i < encryption.size(); i++)
            replace(T.begin(), T.end(), encryption[i], symbol[i]);
        
        if (count(T.begin(), T.end(), '=') != 1)
            continue;
        auto sp = split(T, '=');
        auto left = sp[0];
        auto right = sp[1];
        Parser pl(left), pr(right);
        int64_t vl, vr;
        try {
            vl = pl.expr();
            vr = pr.expr();
        } catch (parse_error e) {
            continue;
        }
        if (pl.pos != left.size() or pr.pos != right.size())
            continue;

        if (vl != vr)
            continue;
        ans.insert(T);
    } while (next_permutation(symbol.begin(), symbol.end()));

    cout << ans.size() << endl;
}
accuracy: 0.18333333333333332
time_ms: 60
memory_kb: 3444
final_rank: 17

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

using namespace std;

class parse_error {};

class Parser{
    string S;
    public:
    int pos = 0;
    Parser(string S) : S(S){}

    char peek(){
        return pos < S.size() ? S[pos] : 'a';
    }

    void skip_spaces(){
        while (peek() == ' ')
            pos++;
    }

    void next(string S){
        for (const auto &s: S){
            if (peek() != s)
                throw parse_error();
            pos++;
        }
    }

    int64_t expr(){
        auto left = term();
        while (peek() == '+' or peek() == '-'){
            char ope = peek();
            pos++;
            auto right = term();
            if (ope == '+')
                left += right;
            else
                left -= right;
        }

        return left;
    }

    int64_t term(){
        auto left = factor();
        while (peek() == '*'){
            char ope = peek();
            pos++;
            auto right = factor();
            left *= right;
        }

        return left;
    }

    int64_t factor(){
        bool is_minus = false;
        while (peek() == '-'){
            next("-");
            is_minus ^= 1;
        }

        int64_t res;

        if (peek() == '('){
            next("(");
            res = expr();
            next(")");
        } else {
            res = number();
        }

        return is_minus ? -res : res;
    }

    int number(){
        if (!isdigit(peek()))
            throw parse_error();
        bool is_zero = peek() == '0';
        int res = 0;
        int cnt = 0;
        while (isdigit(peek())){
            res = res * 2 + peek() - '0';
            pos++;
            cnt++;
        }

        if (cnt > 1 and is_zero)
            throw parse_error();
            
        return res;
    }
};

vector<string> split(string S, char delim){
    vector<string> res;
    int prev = -1;
    for (int i = 0; i < S.size(); i++){
        if (S[i] == delim){
            res.push_back(S.substr(prev + 1, i - prev - 1));
            prev = i;
        }
    }
    res.push_back(S.substr(prev + 1, int(S.size()) - prev - 1));
    return res;
}

int main(){
    string S;
    cin >> S;
    string symbol = "01+-*()=";
    sort(symbol.begin(), symbol.end());
    string encryption = "";
    for (const auto &s: S){
        if (symbol.find(s) == string::npos)
            encryption += s;
    }
    sort(encryption.begin(), encryption.end());
    encryption.erase(unique(encryption.begin(), encryption.end()), encryption.end());

    if (encryption.size() > symbol.size()){
        puts("0");
        return 0;
    }

    set<string> ans;

    do {
        string T = S;
        for (int i = 0; i < encryption.size(); i++)
            replace(T.begin(), T.end(), encryption[i], symbol[i]);
        
        if (count(T.begin(), T.end(), '=') != 1)
            continue;
        auto sp = split(T, '=');
        auto left = sp[0];
        auto right = sp[1];
        Parser pl(left), pr(right);
        int64_t vl, vr;
        try {
            vl = pl.expr();
            vr = pr.expr();
        } catch (parse_error e) {
            continue;
        }
        if (pl.pos != left.size() or pr.pos != right.size())
            continue;

        if (vl != vr)
            continue;
        ans.insert(T);
    } while (next_permutation(symbol.begin(), symbol.end()));

    cout << ans.size() << endl;
}
accuracy: 1
time_ms: 80
memory_kb: 3536
final_rank: 7

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

using namespace std;

class parse_error {};

class Parser{
    string S;
    public:
    int pos = 0;
    Parser(string S) : S(S){}

    char peek(){
        return pos < S.size() ? S[pos] : 'a';
    }

    void skip_spaces(){
        while (peek() == ' ')
            pos++;
    }

    void next(string S){
        for (const auto &s: S){
            if (peek() != s)
                throw parse_error();
            pos++;
        }
    }

    int64_t expr(){
        auto left = term();
        while (peek() == '+' or peek() == '-'){
            char ope = peek();
            pos++;
            auto right = term();
            if (ope == '+')
                left += right;
            else
                left -= right;
        }

        return left;
    }

    int64_t term(){
        auto left = factor();
        while (peek() == '*'){
            char ope = peek();
            pos++;
            auto right = factor();
            left *= right;
        }

        return left;
    }

    int64_t factor(){
        bool is_minus = false;
        while (peek() == '-'){
            next("-");
            is_minus ^= 1;
        }

        int64_t res;

        if (peek() == '('){
            next("(");
            res = expr();
            next(")");
        } else {
            res = number();
        }

        return is_minus ? -res : res;
    }

    int number(){
        if (!isdigit(peek()))
            throw parse_error();
        if (peek() == '0')
            throw parse_error();
        int res = 0;
        while (isdigit(peek())){
            res = res * 2 + peek() - '0';
            pos++;
        }
        return res;
    }
};

vector<string> split(string S, char delim){
    vector<string> res;
    int prev = -1;
    for (int i = 0; i < S.size(); i++){
        if (S[i] == delim){
            res.push_back(S.substr(prev + 1, i - prev - 1));
            prev = i;
        }
    }
    res.push_back(S.substr(prev + 1, int(S.size()) - prev - 1));
    return res;
}

int main(){
    string S;
    cin >> S;
    string symbol = "01+-*()=";
    sort(symbol.begin(), symbol.end());
    string encryption = "";
    for (const auto &s: S){
        if (symbol.find(s) == string::npos)
            encryption += s;
    }
    sort(encryption.begin(), encryption.end());
    encryption.erase(unique(encryption.begin(), encryption.end()), encryption.end());

    if (encryption.size() > symbol.size()){
        puts("0");
        return 0;
    }

    set<string> ans;

    do {
        string T = S;
        for (int i = 0; i < encryption.size(); i++)
            replace(T.begin(), T.end(), encryption[i], symbol[i]);
        
        if (count(T.begin(), T.end(), '=') != 1)
            continue;
        auto sp = split(T, '=');
        auto left = sp[0];
        auto right = sp[1];
        Parser pl(left), pr(right);
        int64_t vl, vr;
        try {
            vl = pl.expr();
            vr = pr.expr();
        } catch (parse_error e) {
            continue;
        }
        if (pl.pos != left.size() or pr.pos != right.size())
            continue;

        if (vl != vr)
            continue;
        ans.insert(T);
    } while (next_permutation(symbol.begin(), symbol.end()));

    cout << ans.size() << endl;
}
accuracy: 0.18333333333333332
time_ms: 60
memory_kb: 3464
final_rank: 18

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

using namespace std;

class parse_error {};

class Parser{
    string S;
    public:
    int pos = 0;
    Parser(string S) : S(S){}

    char peek(){
        return pos < S.size() ? S[pos] : 'a';
    }

    void skip_spaces(){
        while (peek() == ' ')
            pos++;
    }

    void next(string S){
        for (const auto &s: S){
            if (peek() != s)
                throw parse_error();
            pos++;
        }
    }

    int64_t expr(){
        auto left = term();
        while (peek() == '+' or peek() == '-'){
            char ope = peek();
            pos++;
            auto right = term();
            if (ope == '+')
                left += right;
            else
                left -= right;
        }

        return left;
    }

    int64_t term(){
        auto left = factor();
        while (peek() == '*'){
            char ope = peek();
            pos++;
            auto right = factor();
            left *= right;
        }

        return left;
    }

    int64_t factor(){
        bool is_minus = false;
        while (peek() == '-'){
            next("-");
            is_minus ^= 1;
        }

        int64_t res;

        if (peek() == '('){
            next("(");
            res = expr();
            next(")");
        } else {
            res = number();
        }

        return is_minus ? -res : res;
    }

    int number(){
        if (!isdigit(peek()))
            throw parse_error();
        int res = 0;
        while (isdigit(peek())){
            res = res * 2 + peek() - '0';
            pos++;
        }
        return res;
    }
};

vector<string> split(string S, char delim){
    vector<string> res;
    int prev = -1;
    for (int i = 0; i < S.size(); i++){
        if (S[i] == delim){
            res.push_back(S.substr(prev + 1, i - prev - 1));
            prev = i;
        }
    }
    res.push_back(S.substr(prev + 1, int(S.size()) - prev - 1));
    return res;
}

int main(){
    string S;
    cin >> S;
    string symbol = "01+-*()=";
    sort(symbol.begin(), symbol.end());
    string encryption = "";
    for (const auto &s: S){
        if (symbol.find(s) == string::npos)
            encryption += s;
    }
    sort(encryption.begin(), encryption.end());
    encryption.erase(unique(encryption.begin(), encryption.end()), encryption.end());

    if (encryption.size() > symbol.size()){
        puts("0");
        return 0;
    }

    set<string> ans;

    do {
        string T = S;
        for (int i = 0; i < encryption.size(); i++)
            replace(T.begin(), T.end(), encryption[i], symbol[i]);
        
        if (count(T.begin(), T.end(), '=') != 1)
            continue;
        auto sp = split(T, '=');
        auto left = sp[0];
        auto right = sp[1];
        Parser pl(left), pr(right);
        int64_t vl, vr;
        try {
            vl = pl.expr();
            vr = pr.expr();
        } catch (parse_error e) {
            continue;
        }
        if (pl.pos != left.size() or pr.pos != right.size())
            continue;

        if (vl != vr)
            continue;
        ans.insert(T);
    } while (next_permutation(symbol.begin(), symbol.end()));

    cout << ans.size() << endl;
}
accuracy: 0.016666666666666666
time_ms: 20
memory_kb: 3352
final_rank: 20

code_snippet: #include <bits/stdc++.h>
using namespace std;
pair<int, bool> N(string S, int &i){
  bool zero = false;
  if (S[i] == '0'){
    zero = true;
  }
  int ans = 0;
  int L = 0;
  while (isdigit(S[i])){
    ans *= 2;
    ans += S[i] - '0';
    i++;
    L++;
  }
  if (zero && L >= 2){
    return make_pair(0, false);
  }
  if (S[i] == '('){
    return make_pair(0, false);
  }
  return make_pair(ans, true);
}
pair<int, bool> F(string S, int &i);
pair<int, bool> T(string S, int &i);
pair<int, bool> E(string S, int &i);
pair<int, bool> F(string S, int &i){
  if (isdigit(S[i])){
    return N(S, i);
  } else if (S[i] == '-'){
    i++;
    pair<int, bool> P = F(S, i);
    if (!P.second){
      return make_pair(0, false);
    }
    P.first *= -1;
    return P;
  } else if (S[i] == '('){
    i++;
    pair<int, bool> P = E(S, i);
    if (!P.second){
      return make_pair(0, false);
    }
    if (S[i] != ')'){
      return make_pair(0, false);
    }
    i++;
    return P;
  } else {
    return make_pair(0, false);
  }
}
pair<int, bool> T(string S, int &i){
  if (S[i] == ')'){
    return make_pair(0, false);
  }
  pair<int, bool> P = F(S, i);
  if (!P.second){
    return make_pair(0, false);
  }
  while (S[i] == '*'){
    i++;
    pair<int, bool> P2 = F(S, i);
    if (!P2.second){
      return make_pair(0, false);
    }
    P.first *= P2.first;
  }
  if (S[i] == '('){
    return make_pair(0, false);
  }
  return P;
}
pair<int, bool> E(string S, int &i){
  if (S[i] == ')'){
    return make_pair(0, false);
  }
  pair<int, bool> P = T(S, i);
  if (!P.second){
    return make_pair(0, false);
  }
  while (S[i] == '+' || S[i] == '-'){
    char c = S[i];
    i++;
    pair<int, bool> P2 = T(S, i);
    if (!P2.second){
      return make_pair(0, false);
    }
    if (c == '+'){
      P.first += P2.first;
    }
    if (c == '-'){
      P.first -= P2.first;
    }
  }
  if (S[i] == '('){
    return make_pair(0, false);
  }
  return P;
}
bool Q(string S){
  int i = 0;
  pair<int, bool> P1 = E(S, i);
  if (!P1.second){
    return false;
  }
  if (S[i] != '='){
    return false;
  }
  i++;
  pair<int, bool> P2 = E(S, i);
  if (!P2.second){
    return false;
  }
  if (S[i] != '$'){
    return false;
  }
  return P1.first == P2.first;
}
int dfs(string S, set<char> st){
  int N = S.size();
  int p = -1;
  for (int i = 0; i < N; i++){
    if (isalpha(S[i])){
      p = i;
      break;
    }
  }
  if (p == -1){
    if (Q(S)){
      return 1;
    } else {
      return 0;
    }
  } else {
    int ans = 0;
    for (char c : st){
      string S2 = S;
      for (int i = 0; i < N; i++){
        if (S2[i] == S[p]){
          S2[i] = c;
        }
      }
      set<char> st2 = st;
      st2.erase(c);
      ans += dfs(S2, st2);
    }
    return ans;
  }
}
int main(){
  string S;
  cin >> S;
  S += '$';
  set<char> st = {'0', '1', '+', '-', '*', '(', ')', '='};
  cout << dfs(S, st) << endl;
}
accuracy: 1
time_ms: 40
memory_kb: 3180
final_rank: 4

code_snippet: #include <bits/stdc++.h>
using namespace std;
pair<int, bool> N(string S, int &i){
  bool zero = false;
  if (S[i] == '0'){
    zero = true;
  }
  int ans = 0;
  while (isdigit(S[i])){
    ans *= 2;
    ans += S[i] - '0';
    i++;
  }
  if (zero && ans != 0){
    return make_pair(0, false);
  }
  if (S[i] == '('){
    return make_pair(0, false);
  }
  return make_pair(ans, true);
}
pair<int, bool> F(string S, int &i);
pair<int, bool> T(string S, int &i);
pair<int, bool> E(string S, int &i);
pair<int, bool> F(string S, int &i){
  if (isdigit(S[i])){
    return N(S, i);
  } else if (S[i] == '-'){
    i++;
    pair<int, bool> P = F(S, i);
    if (!P.second){
      return make_pair(0, false);
    }
    P.first *= -1;
    return P;
  } else if (S[i] == '('){
    i++;
    pair<int, bool> P = E(S, i);
    if (!P.second){
      return make_pair(0, false);
    }
    if (S[i] != ')'){
      return make_pair(0, false);
    }
    i++;
    return P;
  } else {
    return make_pair(0, false);
  }
}
pair<int, bool> T(string S, int &i){
  if (S[i] == ')'){
    return make_pair(0, false);
  }
  pair<int, bool> P = F(S, i);
  if (!P.second){
    return make_pair(0, false);
  }
  while (S[i] == '*'){
    i++;
    pair<int, bool> P2 = F(S, i);
    if (!P2.second){
      return make_pair(0, false);
    }
    P.first *= P2.first;
  }
  if (S[i] == '('){
    return make_pair(0, false);
  }
  return P;
}
pair<int, bool> E(string S, int &i){
  if (S[i] == ')'){
    return make_pair(0, false);
  }
  pair<int, bool> P = T(S, i);
  if (!P.second){
    return make_pair(0, false);
  }
  while (S[i] == '+' || S[i] == '-'){
    char c = S[i];
    i++;
    pair<int, bool> P2 = T(S, i);
    if (!P2.second){
      return make_pair(0, false);
    }
    if (c == '+'){
      P.first += P2.first;
    }
    if (c == '-'){
      P.first -= P2.first;
    }
  }
  if (S[i] == '('){
    return make_pair(0, false);
  }
  return P;
}
bool Q(string S){
  int i = 0;
  pair<int, bool> P1 = E(S, i);
  if (!P1.second){
    return false;
  }
  if (S[i] != '='){
    return false;
  }
  i++;
  pair<int, bool> P2 = E(S, i);
  if (!P2.second){
    return false;
  }
  if (S[i] != '$'){
    return false;
  }
  return P1.first == P2.first;
}
int dfs(string S, set<char> st){
  int N = S.size();
  int p = -1;
  for (int i = 0; i < N; i++){
    if (isalpha(S[i])){
      p = i;
      break;
    }
  }
  if (p == -1){
    if (Q(S)){
      return 1;
    } else {
      return 0;
    }
  } else {
    int ans = 0;
    for (char c : st){
      string S2 = S;
      for (int i = 0; i < N; i++){
        if (S2[i] == S[p]){
          S2[i] = c;
        }
      }
      set<char> st2 = st;
      st2.erase(c);
      ans += dfs(S2, st2);
    }
    return ans;
  }
}
int main(){
  string S;
  cin >> S;
  S += '$';
  set<char> st = {'0', '1', '+', '-', '*', '(', ')', '='};
  cout << dfs(S, st) << endl;
}
accuracy: 0.25
time_ms: 10
memory_kb: 3180
final_rank: 13

code_snippet: #include <bits/stdc++.h>
using namespace std;
pair<int, bool> N(string S, int &i){
  bool zero = false;
  if (S[i] == '0'){
    zero = true;
  }
  int ans = 0;
  while (isdigit(S[i])){
    ans *= 2;
    ans += S[i] - '0';
    i++;
  }
  if (zero && ans != 0){
    return make_pair(0, false);
  }
  if (S[i] == '('){
    return make_pair(0, false);
  }
  return make_pair(ans, true);
}
pair<int, bool> F(string S, int &i);
pair<int, bool> T(string S, int &i);
pair<int, bool> E(string S, int &i);
pair<int, bool> F(string S, int &i){
  if (isdigit(S[i])){
    return N(S, i);
  } else if (S[i] == '-'){
    i++;
    pair<int, bool> P = F(S, i);
    if (!P.second){
      return make_pair(0, false);
    }
    P.first *= -1;
    return P;
  } else if (S[i] == '('){
    i++;
    pair<int, bool> P = E(S, i);
    if (!P.second){
      return make_pair(0, false);
    }
    if (S[i] != ')'){
      return make_pair(0, false);
    }
    i++;
    return P;
  } else {
    return make_pair(0, false);
  }
}
pair<int, bool> T(string S, int &i){
  pair<int, bool> P = F(S, i);
  if (!P.second){
    return make_pair(0, false);
  }
  while (S[i] == '*'){
    i++;
    pair<int, bool> P2 = F(S, i);
    if (!P2.second){
      return make_pair(0, false);
    }
    P.first *= P2.first;
  }
  if (S[i] == '('){
    return make_pair(0, false);
  }
  return P;
}
pair<int, bool> E(string S, int &i){
  pair<int, bool> P = T(S, i);
  if (!P.second){
    return make_pair(0, false);
  }
  while (S[i] == '+' || S[i] == '-'){
    char c = S[i];
    i++;
    pair<int, bool> P2 = T(S, i);
    if (!P2.second){
      return make_pair(0, false);
    }
    if (c == '+'){
      P.first += P2.first;
    }
    if (c == '-'){
      P.first -= P2.first;
    }
  }
  if (S[i] == '('){
    return make_pair(0, false);
  }
  return P;
}
bool Q(string S){
  int i = 0;
  pair<int, bool> P1 = E(S, i);
  if (!P1.second){
    return false;
  }
  if (S[i] != '='){
    return false;
  }
  i++;
  pair<int, bool> P2 = E(S, i);
  if (!P2.second){
    return false;
  }
  if (S[i] != '$'){
    return false;
  }
  return P1.first == P2.first;
}
int dfs(string S, set<char> st){
  int N = S.size();
  int p = -1;
  for (int i = 0; i < N; i++){
    if (isalpha(S[i])){
      p = i;
      break;
    }
  }
  if (p == -1){
    if (Q(S)){
      return 1;
    } else {
      return 0;
    }
  } else {
    int ans = 0;
    for (char c : st){
      string S2 = S;
      for (int i = 0; i < N; i++){
        if (S2[i] == S[p]){
          S2[i] = c;
        }
      }
      set<char> st2 = st;
      st2.erase(c);
      ans += dfs(S2, st2);
    }
    return ans;
  }
}
int main(){
  string S;
  cin >> S;
  S += '$';
  set<char> st = {'0', '1', '+', '-', '*', '(', ')', '='};
  cout << dfs(S, st) << endl;
}
accuracy: 0.25
time_ms: 10
memory_kb: 3180
final_rank: 13

code_snippet: #include <bits/stdc++.h>
using namespace std;
pair<int, bool> N(string S, int &i){
  bool zero = false;
  if (S[i] == '0'){
    zero = true;
  }
  int ans = 0;
  while (isdigit(S[i])){
    ans *= 2;
    ans += S[i] - '0';
    i++;
  }
  if (zero && ans != 0){
    return make_pair(0, false);
  }
  return make_pair(ans, true);
}
pair<int, bool> F(string S, int &i);
pair<int, bool> T(string S, int &i);
pair<int, bool> E(string S, int &i);
pair<int, bool> F(string S, int &i){
  if (isdigit(S[i])){
    return N(S, i);
  } else if (S[i] == '-'){
    i++;
    pair<int, bool> P = F(S, i);
    if (!P.second){
      return make_pair(0, false);
    }
    P.first *= -1;
    return P;
  } else if (S[i] == '('){
    i++;
    pair<int, bool> P = E(S, i);
    if (!P.second){
      return make_pair(0, false);
    }
    if (S[i] != ')'){
      return make_pair(0, false);
    }
    i++;
    return P;
  } else {
    return make_pair(0, false);
  }
}
pair<int, bool> T(string S, int &i){
  pair<int, bool> P = F(S, i);
  if (!P.second){
    return make_pair(0, false);
  }
  while (S[i] == '*'){
    i++;
    pair<int, bool> P2 = F(S, i);
    if (!P2.second){
      return make_pair(0, false);
    }
    P.first *= P2.first;
  }
  if (S[i] == '('){
    return make_pair(0, false);
  }
  return P;
}
pair<int, bool> E(string S, int &i){
  pair<int, bool> P = T(S, i);
  if (!P.second){
    return make_pair(0, false);
  }
  while (S[i] == '+' || S[i] == '-'){
    char c = S[i];
    i++;
    pair<int, bool> P2 = T(S, i);
    if (!P2.second){
      return make_pair(0, false);
    }
    if (c == '+'){
      P.first += P2.first;
    }
    if (c == '-'){
      P.first -= P2.first;
    }
  }
  if (S[i] == '('){
    return make_pair(0, false);
  }
  return P;
}
bool Q(string S){
  int i = 0;
  pair<int, bool> P1 = E(S, i);
  if (!P1.second){
    return false;
  }
  if (S[i] != '='){
    return false;
  }
  i++;
  pair<int, bool> P2 = E(S, i);
  if (!P2.second){
    return false;
  }
  if (S[i] != '$'){
    return false;
  }
  return P1.first == P2.first;
}
int dfs(string S, set<char> st){
  int N = S.size();
  int p = -1;
  for (int i = 0; i < N; i++){
    if (isalpha(S[i])){
      p = i;
      break;
    }
  }
  if (p == -1){
    if (Q(S)){
      return 1;
    } else {
      return 0;
    }
  } else {
    int ans = 0;
    for (char c : st){
      string S2 = S;
      for (int i = 0; i < N; i++){
        if (S2[i] == S[p]){
          S2[i] = c;
        }
      }
      set<char> st2 = st;
      st2.erase(c);
      ans += dfs(S2, st2);
    }
    return ans;
  }
}
int main(){
  string S;
  cin >> S;
  S += '$';
  set<char> st = {'0', '1', '+', '-', '*', '(', ')', '='};
  cout << dfs(S, st) << endl;
}
accuracy: 0.25
time_ms: 10
memory_kb: 3180
final_rank: 13

code_snippet: #include <bits/stdc++.h>
using namespace std;
pair<int, bool> N(string S, int &i){
  bool zero = false;
  if (S[i] == '0'){
    zero = true;
  }
  int ans = 0;
  while (isdigit(S[i])){
    ans *= 2;
    ans += S[i] - '0';
    i++;
  }
  if (zero && ans != 0){
    return make_pair(0, false);
  }
  return make_pair(ans, true);
}
pair<int, bool> F(string S, int &i);
pair<int, bool> T(string S, int &i);
pair<int, bool> E(string S, int &i);
pair<int, bool> F(string S, int &i){
  if (isdigit(S[i])){
    return N(S, i);
  } else if (S[i] == '-'){
    i++;
    pair<int, bool> P = F(S, i);
    if (!P.second){
      return make_pair(0, false);
    }
    P.first *= -1;
    return P;
  } else if (S[i] == '('){
    i++;
    pair<int, bool> P = E(S, i);
    if (!P.second){
      return make_pair(0, false);
    }
    if (S[i] != ')'){
      return make_pair(0, false);
    }
    i++;
    return P;
  } else {
    return make_pair(0, false);
  }
}
pair<int, bool> T(string S, int &i){
  pair<int, bool> P = F(S, i);
  if (!P.second){
    return make_pair(0, false);
  }
  while (S[i] == '*'){
    i++;
    pair<int, bool> P2 = F(S, i);
    if (!P2.second){
      return make_pair(0, false);
    }
    P.first *= P2.first;
  }
  if (S[i] == '(' || S[i] == ')'){
    return make_pair(0, false);
  }
  return P;
}
pair<int, bool> E(string S, int &i){
  pair<int, bool> P = T(S, i);
  if (!P.second){
    return make_pair(0, false);
  }
  while (S[i] == '+' || S[i] == '-'){
    char c = S[i];
    i++;
    pair<int, bool> P2 = T(S, i);
    if (!P2.second){
      return make_pair(0, false);
    }
    if (c == '+'){
      P.first += P2.first;
    }
    if (c == '-'){
      P.first -= P2.first;
    }
  }
  if (S[i] == '(' || S[i] == ')'){
    return make_pair(0, false);
  }
  return P;
}
bool Q(string S){
  int i = 0;
  pair<int, bool> P1 = E(S, i);
  if (!P1.second){
    return false;
  }
  if (S[i] != '='){
    return false;
  }
  i++;
  pair<int, bool> P2 = E(S, i);
  if (!P2.second){
    return false;
  }
  if (S[i] != '$'){
    return false;
  }
  return P1.first == P2.first;
}
int dfs(string S, set<char> st){
  int N = S.size();
  int p = -1;
  for (int i = 0; i < N; i++){
    if (isalpha(S[i])){
      p = i;
      break;
    }
  }
  if (p == -1){
    if (Q(S)){
      return 1;
    } else {
      return 0;
    }
  } else {
    int ans = 0;
    for (char c : st){
      string S2 = S;
      for (int i = 0; i < N; i++){
        if (S2[i] == S[p]){
          S2[i] = c;
        }
      }
      set<char> st2 = st;
      st2.erase(c);
      ans += dfs(S2, st2);
    }
    return ans;
  }
}
int main(){
  string S;
  cin >> S;
  S += '$';
  set<char> st = {'0', '1', '+', '-', '*', '(', ')', '='};
  cout << dfs(S, st) << endl;
}
accuracy: 0.05
time_ms: 10
memory_kb: 3136
final_rank: 19

code_snippet: #include <bits/stdc++.h>
using namespace std;
pair<int, bool> N(string S, int &i){
  bool zero = false;
  if (S[i] == '0'){
    zero = true;
  }
  int ans = 0;
  while (isdigit(S[i])){
    ans *= 2;
    ans += S[i] - '0';
    i++;
  }
  if (zero && ans != 0){
    return make_pair(0, false);
  }
  return make_pair(ans, true);
}
pair<int, bool> F(string S, int &i);
pair<int, bool> T(string S, int &i);
pair<int, bool> E(string S, int &i);
pair<int, bool> F(string S, int &i){
  if (isdigit(S[i])){
    return N(S, i);
  } else if (S[i] == '-'){
    i++;
    pair<int, bool> P = F(S, i);
    P.first *= -1;
    return P;
  } else if (S[i] == '('){
    i++;
    pair<int, bool> P = E(S, i);
    if (!P.second){
      return make_pair(0, false);
    }
    if (S[i] != ')'){
      return make_pair(0, false);
    }
    i++;
    return P;
  } else {
    return make_pair(0, false);
  }
}
pair<int, bool> T(string S, int &i){
  pair<int, bool> P = F(S, i);
  if (!P.second){
    return make_pair(0, false);
  }
  while (S[i] == '*'){
    i++;
    pair<int, bool> P2 = F(S, i);
    if (!P2.second){
      return make_pair(0, false);
    }
    P.first *= P2.first;
  }
  return P;
}
pair<int, bool> E(string S, int &i){
  pair<int, bool> P = T(S, i);
  if (!P.second){
    return make_pair(0, false);
  }
  while (S[i] == '+' || S[i] == '-'){
    char c = S[i];
    i++;
    pair<int, bool> P2 = T(S, i);
    if (!P2.second){
      return make_pair(0, false);
    }
    if (c == '+'){
      P.first += P2.first;
    }
    if (c == '-'){
      P.first -= P2.first;
    }
  }
  return P;
}
bool Q(string S){
  int i = 0;
  pair<int, bool> P1 = E(S, i);
  if (!P1.second){
    return false;
  }
  if (S[i] != '='){
    return false;
  }
  i++;
  pair<int, bool> P2 = E(S, i);
  if (!P2.second){
    return false;
  }
  if (S[i] != '$'){
    return false;
  }
  return P1.first == P2.first;
}
int dfs(string S, set<char> st){
  int N = S.size();
  int p = -1;
  for (int i = 0; i < N; i++){
    if (isalpha(S[i])){
      p = i;
      break;
    }
  }
  if (p == -1){
    if (Q(S)){
      return 1;
    } else {
      return 0;
    }
  } else {
    int ans = 0;
    for (char c : st){
      string S2 = S;
      for (int i = 0; i < N; i++){
        if (S2[i] == S[p]){
          S2[i] = c;
        }
      }
      set<char> st2 = st;
      st2.erase(c);
      ans += dfs(S2, st2);
    }
    return ans;
  }
}
int main(){
  string S;
  cin >> S;
  S += '$';
  set<char> st = {'0', '1', '+', '-', '*', '(', ')', '='};
  cout << dfs(S, st) << endl;
}
accuracy: 0.25
time_ms: 10
memory_kb: 3180
final_rank: 13

code_snippet: // I SELL YOU...! 
#include<iostream>
#include<vector>
#include<algorithm>
#include<functional>
#include<queue>
#include<stdexcept>
#include<chrono>
#include<iomanip>
#include<map>
#include<set>
using namespace std;
using ll = long long;
using P = pair<ll,ll>;
using TP = tuple<ll,ll,ll>;
void init_io(){
  cin.tie(0);
  ios::sync_with_stdio(false);
  cout << fixed << setprecision(18);
}
vector<char> calc_cs(const string &s) {
  set<char> st;
  for(auto c:s){
    if (('A'<=c&&c<='Z')||('a'<=c&&c<='z')){
      st.insert(c);
    }
  }
  vector<char> res;
  for(auto c:st){
    res.push_back(c);
  }
  return res;
}
ll calc_t(const string &s, ll &idx);
ll calc_e(const string &s, ll &idx);

ll calc_f(const string &s, ll &idx) {
  ll n = s.size();
  if (idx >= n) {
    throw runtime_error("E");
  }
  if(s[idx]=='0') {
    idx++;
    return 0;
  }
  if (s[idx]=='1'){
    ll v = 0;
    while(idx < n && (s[idx]=='0'||s[idx]=='1')) {
      v *= 2;
      v += (s[idx]-'0');
      idx++;
    }
    return v;
  }else if(s[idx]=='-') {
    idx++;
    return -calc_f(s,idx);
  }else if(s[idx]=='(') {
    idx++;
    ll v = calc_e(s,idx);
    if (s[idx]==')') {
      idx++;
      return v;
    } else {
      throw runtime_error("E");
    }
  } else {
    throw runtime_error("E");
  }
}
ll calc_t(const string &s, ll &idx) {
  ll n = s.size();
  if (idx >= n) {
    throw runtime_error("E");
  }
  ll v = calc_f(s,idx);
  while(idx<n&&s[idx]=='*'){
    idx++;
    v *= calc_f(s,idx);
  }
  return v;
}
ll calc_e(const string &s, ll &idx) {
  ll n = s.size();
  if (idx >= n) {
    throw runtime_error("E");
  }
  ll v0 = calc_t(s,idx);
  while(idx<n&&(s[idx]=='+'||s[idx]=='-')) {
    char c = s[idx];
    idx++;
    ll v1 = calc_t(s,idx);
    if (c=='+') v0 += v1;
    else v0 -= v1;
  }
  return v0;
}
ll calc_q(const string &s, ll &idx, bool pri = false) {
  ll n = s.size();
  ll v0 = calc_e(s,idx);
  if (idx >= n) {
    throw runtime_error("E");
  }
  if(s[idx] != '=') {
    throw runtime_error("E");
  }
  idx++;
  ll v1 = calc_e(s,idx);
  if (idx != n){
    throw runtime_error("E");
  }
  if (pri) {
    cout << v0 <<" " << s <<" "<< v1 << endl;
  }
  return (v0 == v1);
}
bool check(const string &s){
  ll n = s.size();
  try{
    ll idx = 0;
    ll val = calc_q(s,idx);
    if (idx!=n) throw runtime_error("E");
    if (val == 1){
      return true;
    }else{
      return false;
    }
  }catch(runtime_error e) {
    return false;
  }
}
signed main(){
  init_io();
  string s;
  cin >> s;
  vector<char> cs = calc_cs(s),sy={'=','+','-','*','(',')','0','1'};
  set<string> ans_s;
  ll n=s.size(),m = sy.size();
  if (cs.size() > sy.size()) {
    cout << 0 << endl;
    return 0;
  }
  while(cs.size() != sy.size()) {
    cs.push_back(' ');
  }
  sort(cs.begin(),cs.end());
  set<string> used;
  ll ans = 0;
  do{
    string rs = s;
    if(used.find(rs) == used.end()) {
      used.insert(rs);
      if(check(rs)) {
        ans++;
      }
    }
    for(int j=0;j<m;j++){
      for(int i=0;i<n;i++){
        if(s[i] == cs[j]) {
          rs[i] = sy[j];
        }
      }
      if(used.find(rs)==used.end()) {
        used.insert(rs);
        if(check(rs)) {
          ans++;
        }
      }
    }
  }while(next_permutation(cs.begin(),cs.end()));
  cout << ans << endl;
}
accuracy: 1
time_ms: 330
memory_kb: 17080
final_rank: 11

code_snippet: // I SELL YOU...! 
#include<iostream>
#include<vector>
#include<algorithm>
#include<functional>
#include<queue>
#include<stdexcept>
#include<chrono>
#include<iomanip>
#include<map>
#include<set>
using namespace std;
using ll = long long;
using P = pair<ll,ll>;
using TP = tuple<ll,ll,ll>;
void init_io(){
  cin.tie(0);
  ios::sync_with_stdio(false);
  cout << fixed << setprecision(18);
}
vector<char> calc_cs(const string &s) {
  set<char> st;
  for(auto c:s){
    if (('A'<=c&&c<='Z')||('a'<=c&&c<'z')){
      st.insert(c);
    }
  }
  vector<char> res;
  for(auto c:st){
    res.push_back(c);
  }
  return res;
}
ll calc_t(const string &s, ll &idx);
ll calc_e(const string &s, ll &idx);

ll calc_f(const string &s, ll &idx) {
  ll n = s.size();
  if (idx >= n) {
    throw runtime_error("E");
  }
  if(s[idx]=='0') {
    idx++;
    return 0;
  }
  if (s[idx]=='1'){
    ll v = 0;
    while(idx < n && (s[idx]=='0'||s[idx]=='1')) {
      v *= 2;
      v += (s[idx]-'0');
      idx++;
    }
    return v;
  }else if(s[idx]=='-') {
    idx++;
    return -calc_f(s,idx);
  }else if(s[idx]=='(') {
    idx++;
    ll v = calc_e(s,idx);
    if (s[idx]==')') {
      idx++;
      return v;
    } else {
      throw runtime_error("E");
    }
  } else {
    throw runtime_error("E");
  }
}
ll calc_t(const string &s, ll &idx) {
  ll n = s.size();
  if (idx >= n) {
    throw runtime_error("E");
  }
  ll v = calc_f(s,idx);
  while(idx<n&&s[idx]=='*'){
    idx++;
    v *= calc_f(s,idx);
  }
  return v;
}
ll calc_e(const string &s, ll &idx) {
  ll n = s.size();
  if (idx >= n) {
    throw runtime_error("E");
  }
  ll v0 = calc_t(s,idx);
  while(idx<n&&(s[idx]=='+'||s[idx]=='-')) {
    char c = s[idx];
    idx++;
    ll v1 = calc_t(s,idx);
    if (c=='+') v0 += v1;
    else v0 -= v1;
  }
  return v0;
}
ll calc_q(const string &s, ll &idx, bool pri = false) {
  ll n = s.size();
  ll v0 = calc_e(s,idx);
  if (idx >= n) {
    throw runtime_error("E");
  }
  if(s[idx] != '=') {
    throw runtime_error("E");
  }
  idx++;
  ll v1 = calc_e(s,idx);
  if (idx != n){
    throw runtime_error("E");
  }
  if (pri) {
    cout << v0 <<" " << s <<" "<< v1 << endl;
  }
  return (v0 == v1);
}
bool check(const string &s){
  ll n = s.size();
  try{
    ll idx = 0;
    ll val = calc_q(s,idx);
    if (idx!=n) throw runtime_error("E");
    if (val == 1){
      return true;
    }else{
      return false;
    }
  }catch(runtime_error e) {
    return false;
  }
}
signed main(){
  init_io();
  string s;
  cin >> s;
  vector<char> cs = calc_cs(s),sy={'=','+','-','*','(',')','0','1'};
  set<string> ans_s;
  ll n=s.size(),m = sy.size();
  if (cs.size() > sy.size()) {
    cout << 0 << endl;
    return 0;
  }
  while(cs.size() != sy.size()) {
    cs.push_back(' ');
  }
  sort(cs.begin(),cs.end());
  set<string> used;
  ll ans = 0;
  do{
    string rs = s;
    if(used.find(rs) == used.end()) {
      used.insert(rs);
      if(check(rs)) {
        ans++;
      }
    }
    for(int j=0;j<m;j++){
      for(int i=0;i<n;i++){
        if(s[i] == cs[j]) {
          rs[i] = sy[j];
        }
      }
      if(used.find(rs)==used.end()) {
        used.insert(rs);
        if(check(rs)) {
          ans++;
        }
      }
    }
  }while(next_permutation(cs.begin(),cs.end()));
  cout << ans << endl;
}
accuracy: 0.2833333333333333
time_ms: 280
memory_kb: 17240
final_rank: 12