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.

题目大意:给一个最多5个字母的式子,要求用不同的数字替换这些字母,中间填一个符号,问有多少种填法能使等式成立。

思路:暴力枚举。

PS:易错点:不能有前导0,。可以有单个0。一个数字只能出现一次。除法不能用除号(整除的问题)。做除法前要判断被零除的问题(移项了不代表不用判断)。有些字母可能不在式子里出现。

代码(15MS):

 #include <cstdio>
#include <iostream>
#include <cstring>
#include <algorithm>
using namespace std; const int MAXN = ; char s1[MAXN], s2[MAXN], s3[MAXN];
int trans[MAXN];
bool exist[MAXN], use[MAXN];
int ans; int s_to_i(char *s) {
if(trans[s[] - 'A'] == && s[]) return -;
int ret = ;
for(int i = ; s[i]; ++i)
ret = ret * + trans[s[i] - 'A'];
return ret;
} void dfs(int dep) {
if(dep == ) {
int a1 = s_to_i(s1), a2 = s_to_i(s2), a3 = s_to_i(s3);
if(a1 == - || a2 == - || a3 == -) 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 + );
return ;
}
for(int i = ; i <= ; ++i) {
if(use[i]) continue;
trans[dep] = i;
use[i] = true;
dfs(dep + );
use[i] = false;
}
} void check(char *s) {
for(int i = ; s[i]; ++i)
exist[s[i] - 'A'] = true;
} int main() {
int T;
scanf("%d", &T);
while(T--) {
scanf("%s%s%s", s1, s2, s3);
memset(exist, , sizeof(exist));
check(s1), check(s2), check(s3);
ans = ;
dfs();
printf("%d\n", ans);
}
}

最新文章

  1. Android中View的事件分发机制
  2. unity 计时器
  3. loj 1308(点双连通分量应用)
  4. SQL Server:分离和重新附加数据库
  5. LVS+keepalived实现负载均衡
  6. php去除换行(回车换行)的方法
  7. start apache2 failed in Ubuntu
  8. SharePoint 2013 Pop-Up Dialogs
  9. I/O CPU
  10. 一步一步学python(七) - 更加抽象
  11. 基于Ubuntu 14.04构建mysql5.6 Docker镜像
  12. linux_初始参数选择
  13. 将图片转为base64
  14. PDF文件怎么转换成PPT
  15. hive数据类型
  16. Ubuntu下SSH无密码验证配置
  17. Win10系列:UWP界面布局基础6
  18. PEP_2007相关问题记录
  19. MySQL递归查询父节点或递归查询子节点-陈远波
  20. Spring data jpa Specification查询关于日期的范围搜索

热门文章

  1. 记录使用antd时样式不生效的问题
  2. Openresty最佳案例 | 第2篇:Lua入门
  3. 框架篇 -- Struts2
  4. iOS开发CGRectGetMidX. CGRectGetMidY.CGRectGetMinY. CGRectGetMaxY. CGRectGetMinX. CGRectGetMaxX的使用
  5. Angularjs基础(二)
  6. 表单转换为JSON
  7. chromium之tracked_objects
  8. ABAP术语-Function Library
  9. mysql数据库关于事务的问题?求解答
  10. fluentd安装和配置,收集docker日志