[HDU3699]组合情况DFS枚举

来源:互联网 发布:数据库表设计例子 编辑:程序博客网 时间:2024/06/06 01:19

A hard Aoshu Problem

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 62768/32768 K (Java/Others)
Total Submission(s): 885    Accepted Submission(s): 450


Problem Description
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
2A A ABCD BCD B
 

Sample Output
572
 

Source
2010 Asia Fuzhou Regional Contest

#define DeBUG#include <iostream>#include <cstdio>#include <cstring>#include <cmath>#include <cstdlib>#include <algorithm>#include <vector>#include <stack>#include <queue>#include <string>#include <set>#include <sstream>#include <map>#include <bitset>using namespace std ;#define zero {0}#define INF 2000000000#define EPS 1e-6typedef long long LL;const double PI = acos(-1.0);inline int sgn(double x){    return fabs(x) < EPS ? 0 : (x < 0 ? -1 : 1);}const int MAXN = 12;char s1[MAXN];char s2[MAXN];char s3[MAXN];int trans[MAXN];bool exist[MAXN], use[MAXN];int ans;int s_to_i(char *s){    if (trans[s[0] - 'A'] == 0 && s[1]) return -1;    int ret = 0;    for (int i = 0; s[i]; i++)    {        ret = ret * 10 + trans[s[i] - 'A'];    }    return ret;}void dfs(int dep){    if (dep == 5)    {        int a1 = s_to_i(s1), a2 = s_to_i(s2), a3 = s_to_i(s3);        if (a1 == -1 || a2 == -1 || a3 == -1)return ;        if (a1 + a2 == a3)++ans;        if (a1 - a2 == a3)++ans;        if (a1 * a2 == a3)++ans;        if (a2 && a1 == a2 * a3)++ans;        return ;    }    if (!exist[dep])    {        dfs(dep + 1);        return ;    }    for (int i = 0; i < 10; i++)    {        if (use[i])            continue;        trans[dep] = i;        use[i] = true;        dfs(dep + 1);        use[i] = false;    }}void check(char *s){    for (int i = 0; s[i]; i++)    {        exist[s[i] - 'A'] = true;    }}int main(){#ifdef DeBUGs    freopen("C:\\Users\\Sky\\Desktop\\1.in", "r", stdin);#endif    int T;    scanf("%d", &T);    while (T--)    {        scanf("%s%s%s", s1, s2, s3);        memset(exist, 0, sizeof(exist));        check(s1), check(s2), check(s3);        ans = 0;        dfs(0);        printf("%d\n", ans);    }    return 0;}


0 0
原创粉丝点击