Problem:
MathJax.Hub.Config({ tex2jax: { inlineMath: [["$","$"], ["\\(","\\)"]], processEscapes: true }});

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 Input 2

icpc

Sample Output 2

1

Sample Input 3

BAYL0R

Sample Output 3

3

Sample Input 4

-AB+AC-A

Sample Output 4

1

Sample Input 5

abcdefghi

Sample Output 5

0

Sample Input 6

111-10=1+10*10

Sample Output 6

1

Sample Input 7

0=10-1

Sample Output 7

0

Solution in C++14 (judge verdict: Accepted)

#include <bits/stdc++.h>

using namespace std;

#define REP(i,a,b) for(int i=a;i<(int)b;i++)
#define rep(i,n) REP(i,0,n)
#define all(c) (c).begin(), (c).end()
#define zero(a) memset(a, 0, sizeof a)
#define minus(a) memset(a, -1, sizeof a)
#define watch(a) { cout << #a << " = " << a << endl; }
template<class T1, class T2> inline bool minimize(T1 &a, T2 b) { return b < a && (a = b, 1); }
template<class T1, class T2> inline bool maximize(T1 &a, T2 b) { return a < b && (a = b, 1); }

typedef long long ll;
int const inf = 1<<29;

string S;
int idx;
int N, M;
string alphas;
int curr;

bool consume(char c) {
  if(idx >= N) return false;
  if(S[idx] != c) return false;
  idx ++;
  return true;
}

int E();

int convert(string const& b) {
  int n = b.size();
  int ret = 0;
  for(int i=0; i<n; i++) {
    ret *= 2;
    assert(isdigit(b[i]));
    ret += b[i] - '0';
  }
  return ret;
}

string B() {
  string ret;
  while(1) {
    if(S[idx] == '0') ret += '0';
    else if(S[idx] == '1') ret += '1';
    else break;
    idx++;
  }
  return ret;
}

int Nu() {
  if(consume('0')) {
    return 0;
  }
  else if(consume('1')) {
    return convert("1" + B());
  }
  else {
    return -inf;
  }
}

int F() {
  if(consume('-')) {
    int r = F();
    if(r == -inf) return -inf;
    return -r;
  }
  else if(consume('(')) {
    int r = E();
    if(r == -inf) return -inf;
    if(!consume(')')) return -inf;
    return r;
  }
  else {
    return Nu();
  }
}

int T() {
  int num1 = F();
  if(num1 == -inf) return -inf;
  while(consume('*')) {
    int num2 = F();
    if(num2 == -inf) return -inf;
    num1 *= num2;
  }
  return num1;
}

int E() {
  int term1 = T();
  if(term1 == -inf) return -inf;
  while(1) {
    bool cont = 0;
    while(consume('+')) {
      cont = 1;
      int term2 = T();
      if(term2 == -inf) return -inf;
      term1 += term2;
    }
    while(consume('-')) {
      cont = 1;
      int term2 = T();
      if(term2 == -inf) return -inf;
      term1 -= term2;
    }
    if(!cont) break;
  }
  return term1;
}

bool Q() {
  if(idx == N) return curr == 0;

  int expr1 = E();
  if(expr1 == -inf) return false;

  if(!consume('=')) return false;

  int expr2 = E();
  if(expr2 == -inf) return false;
  if(expr1 != expr2) return false;

  return idx == N;
}

int main() {

  string T = "01+-*()=";
  sort(T.begin(), T.end());
  int Tsize = T.size();

  string RS; cin >> RS;
  N = RS.size();
  for(auto c: RS) if(isalpha(c)) alphas.push_back(c);
  sort(alphas.begin(), alphas.end());
  alphas.erase(unique(alphas.begin(), alphas.end()), alphas.end());
  M = alphas.size();

  if(M > T.size()) {
    cout << 0 << endl;
    exit(0);
  }

  int ans = 0;
  set<string> st;
  do {
    S = RS;
    rep(i, N)
      if(isalpha(S[i])) S[i] = T[alphas.find(S[i])];
    if(!st.count(S)) {
      st.insert(S);
      idx = curr = 0;
      ans += Q();
    }
  } while(next_permutation(T.begin(), T.end()));
  
  cout << ans << endl;

  return 0;
}