2014
11-30

# A hard Aoshu Problem

Math Olympiad is called “Aoshu” in China. Aoshu is very popular in elementary schools. Nowadays, Aoshu is getting more and more difficult. Here is a classic Aoshu problem:

ABBDE __ ABCCC = BDBDE

In the equation above, a letter stands for a digit(0 � 9), and different letters stands for different digits. You can fill the blank with ‘+’, ‘-‘ , ‘×’ or ‘÷’.

How to make the equation right? Here is a solution:

12245 + 12000 = 24245

In that solution, A = 1, B = 2, C = 0, D = 4, E = 5, and ‘+’ is filled in the blank.

When I was a kid, finding a solution is OK. But now, my daughter’s teacher tells her to find all solutions. That’s terrible. I doubt whether her teacher really knows how many solutions are there. So please write a program for me to solve this kind of problems.

The first line of the input is an integer T( T <= 20) indicating the number of test cases.

Each test case is a line which is in the format below:

s1 s2 s3

s1, s2 and s3 are all strings which are made up of capital letters. Those capital letters only include ‘A’,’B’,’C’,’D’ and ‘E’, so forget about ‘F’ to ‘Z’. The length of s1,s2 or s3 is no more than 8.

When you put a ‘=’ between s2 and s3, and put a operator( ‘+’,’-‘, ‘×’ or ‘÷’.) between s1 and s2, and replace every capital letter with a digit, you get a equation.

You should figure out the number of solutions making the equation right.

Please note that same letters must be replaced by same digits, and different letters must be replaced by different digits. If a number in the equation is more than one digit, it must not have leading zero.

The first line of the input is an integer T( T <= 20) indicating the number of test cases.

Each test case is a line which is in the format below:

s1 s2 s3

s1, s2 and s3 are all strings which are made up of capital letters. Those capital letters only include ‘A’,’B’,’C’,’D’ and ‘E’, so forget about ‘F’ to ‘Z’. The length of s1,s2 or s3 is no more than 8.

When you put a ‘=’ between s2 and s3, and put a operator( ‘+’,’-‘, ‘×’ or ‘÷’.) between s1 and s2, and replace every capital letter with a digit, you get a equation.

You should figure out the number of solutions making the equation right.

Please note that same letters must be replaced by same digits, and different letters must be replaced by different digits. If a number in the equation is more than one digit, it must not have leading zero.

2
A A A
BCD BCD B

5
72

#include <map>
#include <set>
#include <queue>
#include <stack>
#include <cctype>
#include <cstdio>
#include <string>
#include <vector>
#include <cstring>
#include <iostream>
#include <algorithm>
using namespace std;

typedef long long LL;
typedef pair<int, int> PII;

#define FILE freopen("in","r",stdin)
#define foreach(it,a) for(__typeof((a).begin()) it=(a).begin(); it!=(a).end(); it++)

/*****************************************************************/

char s1[10], s2[10], s3[10];
int p[10];
bool vis[10];
int ans, n;
void check() {
//for (int i = 0; i < n; i++) printf("%d ", p[i]); printf("\n");
if (strlen(s1) > 1 && p[s1[0] - 65] == 0) return;
if (strlen(s2) > 1 && p[s2[0] - 65] == 0) return;
if (strlen(s3) > 1 && p[s3[0] - 65] == 0) return;
int x1 = 0, x2 = 0, x3 = 0;
for (int i = 0; s1[i]; i++) x1 = x1 * 10 + p[s1[i] - 65];
for (int i = 0; s2[i]; i++) x2 = x2 * 10 + p[s2[i] - 65];
for (int i = 0; s3[i]; i++) x3 = x3 * 10 + p[s3[i] - 65];
if (x1 + x2 == x3) ans++;
if (x1 - x2 == x3) ans++;
if ((LL)x1 * x2 == x3) ans++;
if (x2 != 0 && x1 % x2 == 0 && x1 / x2 == x3) ans++;
}
void dfs(int k, int x) {
if (k == n) {
check();
return;
}
for (int i = 0; i < 10; i++) if (!vis[i]) {
p[k] = i;
vis[i] = true;
dfs(k + 1, i + 1);
vis[i] = false;
}
}
void init(char ch) {
for (int i = 0; s1[i]; i++) if (s1[i] == ch) return;
for (int i = 0; s2[i]; i++) if (s2[i] == ch) return;
for (int i = 0; s3[i]; i++) if (s3[i] == ch) return;
for (int i = 0; s1[i]; i++) if (s1[i] > ch) s1[i]--;
for (int i = 0; s2[i]; i++) if (s2[i] > ch) s2[i]--;
for (int i = 0; s3[i]; i++) if (s3[i] > ch) s3[i]--;
init(ch);
}
bool has(char ch) {
for (int i = 0; s1[i]; i++) if (s1[i] >= ch) return true;
for (int i = 0; s2[i]; i++) if (s2[i] >= ch) return true;
for (int i = 0; s3[i]; i++) if (s3[i] >= ch) return true;
return false;
}
int main() {
// FILE;
int T;
scanf("%d", &T);
while (T--) {
scanf("%s%s%s", s1, s2, s3);
n = 0;
for (char ch = 'A'; ch <= 'E'; ch++)
if (has(ch)) {
n++;
init(ch);
}
//printf("%s %s %s\n", s1, s2, s3);
ans = 0;
memset(vis, 0, sizeof(vis));
dfs(0, 0);
printf("%d\n", ans);
}
return 0;
}