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.

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

Output
For each test case, print an integer in a line. It represents the number of solutions.
Sample Input
2
A A A
BCD BCD B
Sample Output
5
72
题意:
给ABCDE字母赋不同值(0~9),使第一个串对应的数字与第二个串对应的数字运算等于第三个串对应的数字,问一共有多少种情况

#include<cstdio>
#include<string>
#include<cstring>
#include<cstdlib>
using namespace std;
typedef long long ll;
char s1[15],s2[15],s3[15];
bool book0[10],booknum[15],vis[15];
int num[15],cnt,l1,l2,l3;
ll ans;
char q[10];
void solve(int x) {
	if(x>cnt) {
		ll a=0,b=0,c=0;
		for(int i=0; i<l1; i++)
			a=a*10+num[s1[i]-'A'];
		for(int i=0; i<l2; i++)
			b=b*10+num[s2[i]-'A'];
		for(int i=0; i<l3; i++)
			c=c*10+num[s3[i]-'A'];

		if(a+b==c) {
			//printf("%lld+%lld=%lld\n",a,b,c);
			ans++;
		}
		if(a-b==c) {
			//printf("%lld-%lld=%lld\n",a,b,c);
			ans++;
		}
		if(a*b==c) {
			//printf("%lld*%lld=%lld\n",a,b,c);
			ans++;
		}
		if(b!=0&&a%b==0&&a/b==c) {
			//printf("%lld/%lld=%lld\n",a,b,c);
			ans++;
		}
		return ;
	}
	for(int i=0; i<10; i++) {
		if(book0[q[x]-'A']&&i==0) continue;
		if(booknum[i]) continue;
		booknum[i]=true;
		num[q[x]-'A']=i;
		solve(x+1);
		booknum[i]=false;
	}
}
int main() {
	int T;
	scanf("%d",&T);
	while(T--) {
		memset(book0,0,sizeof(book0));
		memset(booknum,0,sizeof(booknum));
		memset(vis,0,sizeof(vis));
		scanf("%s%s%s",s1,s2,s3);
	    l1=strlen(s1);
		l2=strlen(s2);
		l3=strlen(s3);
		if(l1>1) book0[s1[0]-'A']=true;
		if(l2>1) book0[s2[0]-'A']=true;
		if(l3>1) book0[s3[0]-'A']=true;
		for(int i=0; i<l1; i++)
			vis[s1[i]-'A']=true;
		for(int i=0; i<l2; i++)
			vis[s2[i]-'A']=true;
		for(int i=0; i<l3; i++)
			vis[s3[i]-'A']=true;
		cnt=0;
		for(int i=0; i<5; i++)
			if(vis[i])  q[++cnt]='A'+i;
		ans=0;
		solve(1);
		printf("%lld\n",ans);
	}
	return 0;
}