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 Scala (judge verdict: Accepted)

import scala.annotation.tailrec
import scala.collection.mutable

object Main extends App {

  import scala.io.StdIn._
  val target = "01+-*()="
  val exp = readLine.trim.toList
  val symbol = if (exp.contains('=')) target.filterNot(_ == '=') else target
  val suspects = exp.filter(_.isLetter).distinct
  var result = 0
  for (usage ← symbol.combinations(suspects.length)){
    for (map ← usage.permutations.map{u ⇒ suspects.zip(u).toMap.withDefault(c ⇒ c)}){
      exp.map(map) match {
        case Q() ⇒ result += 1
        case _ ⇒
      }
    }
  }
  println(result)

  object Q{
    def unapply(arg: List[Char]): Boolean = {
      arg match {
        case l E '='::(r E Nil) ⇒ l == r
        case _ ⇒ false
      }
    }
  }
  object E{
    def unapply(arg: List[Char]): Option[(Int, List[Char])] = {
      arg match {
        case l T t ⇒ Some(takeAll(t, l))
        case _ ⇒ None
      }
    }
    @tailrec
    private[this] def takeAll(arg: List[Char], left: Int): (Int, List[Char]) = {
      arg match {
        case '-'::(r T t) ⇒ takeAll(t, left - r)
        case '+'::(r T t) ⇒ takeAll(t, left + r)
        case _ ⇒ (left, arg)
      }
    }
  }
  object T{
    def unapply(arg: List[Char]): Option[(Int, List[Char])] = {
      arg match {
        case l F t ⇒ Some(takeAll(t, l))
        case _ ⇒ None
      }
    }
    @tailrec
    private[this] def takeAll(arg: List[Char], left: Int): (Int, List[Char]) = {
      arg match {
        case '*'::(r F t) ⇒ takeAll(t, left * r)
        case _ ⇒ (left, arg)
      }
    }
  }
  object F{
    def unapply(arg: List[Char]): Option[(Int, List[Char])] = {
      arg match {
        case n N t ⇒ Some(n, t)
        case '-'::(f F t) ⇒ Some(-f, t)
        case '('::(e E ')'::t) ⇒ Some(e, t)
        case _ ⇒ None
      }
    }
  }
  object N{
    def unapply(arg: List[Char]): Option[(Int, List[Char])] = {
      arg.span(_.isDigit) match {
        case ('0'::Nil, t) ⇒ Some(0, t)
        case (n@'1'::_, t) ⇒ Some(n.foldLeft(0){(a, b) ⇒ a * 2 + b.asDigit}, t)
        case _ ⇒ None
      }
    }
  }
  implicit class Extension[T](val value :T) extends AnyVal {
    @inline
    def let[A](@inline func: T ⇒ A): A = func(value)
    @inline
    def also(@inline func: T ⇒ Unit): T = {func(value); value}
  }
}