题目:
A hard Aoshu Problem
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 62768/32768 K (Java/Others)Total Submission(s): 874 Accepted Submission(s): 444
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.
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.
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
链接:http://acm.hdu.edu.cn/showproblem.php?pid=3699
题意:
有字符串s1,s2,s3,求有多少种取值可以使得s1_s2 = s3(_表示加减乘除四种运算),每个字符串由‘A','B','C','D','E'组成,且长度都不超过8,每个字母代表一个数字(0,1,2,...,9);
解题思路:
回溯法,先统计出现了A-E中的哪几个字母,对于出现过的字母,每个字母枚举所有可能的取值(0,1,2,...9)。
WA点:1.不能有前导0,2.除法,只有b!=0时才有a/b=c,这个地方还不能写成a/b=c,因为题目要求必须是整除,a/b=c不一定是整除,如5/2=2;
代码:
#include <cstdio>
#include <map>
#include <cstring>
using namespace std;
const int MAXN = 15;
char s1[MAXN], s2[MAXN], s3[MAXN];
int ans = 0, vis[MAXN];
int len1, len2, len3;
map<char , int> mp;
void dfs(int cur, int ar[])
{
if(5 == cur)
{
int a = 0, b = 0, c = 0;
if(len1 >= 2 && 0 == ar[s1[0]-'A'])
return;
if(len2 >= 2 && 0 == ar[s2[0]-'A'])
return ;
if(len3 >= 2 && 0 == ar[s3[0]-'A'])
return ;
for(int i = 0; i < len1; i++)
{
a = 10 * a + ar[s1[i]-'A'];
}
for(int i = 0; i < len2; i++)
{
b = 10 * b + ar[s2[i]-'A'];
}
for(int i = 0; i < len3; i++)
{
c = 10 * c + ar[s3[i]-'A'];
}
if(a + b == c) ans++;
if(a - b == c) ans++;
if(a * b == c) ans++;
if(0 != b && a == b * c) ans++;
//printf("%d %d %d\n", a, b, c);
return ;
}
if(mp['A' + cur])
{
for(int i = 0; i < 10; i++)
{
if(!vis[i])
{
vis[i] = 1;
ar[cur] = i;
dfs(cur + 1, ar);
vis[i] = 0;
}
}
}
else
{
dfs(cur + 1, ar);
}
}
int main()
{
int t;
scanf("%d", &t);
getchar();
while(t--)
{
mp.clear();
ans = 0;
int a[MAXN];
memset(a, 0, sizeof(a));
memset(vis, 0, sizeof(vis));
scanf("%s%s%s", s1, s2, s3);
len1 = strlen(s1);
len2 = strlen(s2);
len3 = strlen(s3);
for(int i = 0; i < len1; i++)
{
if(!mp[s1[i]]) mp[s1[i]] = s1[i] - 'A' + 1;
}
for(int i = 0; i < len2; i++)
{
if(!mp[s2[i]]) mp[s2[i]] = s2[i] - 'A' + 1;
}
for(int i = 0; i < len3; i++)
{
if(!mp[s3[i]]) mp[s3[i]] = s3[i] - 'A' + 1;
}
dfs(0, a);
printf("%d\n", ans);
}
return 0;
}

本文介绍了一个经典的奥数问题,即通过填入不同的数学运算符使等式成立,并给出了使用回溯法解决该问题的具体实现。

被折叠的 条评论
为什么被折叠?



