3379. Infallibly Crack Perplexing Cryptarithm

单点时限: 2.0 sec

内存限制: 256 MB

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 di erent 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 signi cant, that is, a and A are di erent 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  ::= P | 0B | 1B

Here, P 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 di erent 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 di erent, 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 di erent 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.

输入格式

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.

输出格式

Print in a line the number of correct equations that can be encrypted into the input string.

样例

Input
ACM
Output
0
Input
icpc
Output
1
Input
BAYL0R
Output
3
Input
-AB+AC-A
Output
1
Input
abcdefghi
Output
0
Input
111-10=1+10*10
Output
1
Input
0=10-1
Output
0

2 人解决,2 人已尝试。

3 份提交通过,共有 57 份提交。

8.6 EMB 奖励。

创建: 6 年,5 月前.

修改: 6 年,5 月前.

最后提交: 3 年,4 月前.

来源: ICPC Asia Tsukuba Regional 2016

题目标签