[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.
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
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
- [HDU3699]组合情况DFS枚举
- hdu3699(不等式dfs)
- hdu 2489 Minimal Ratio Tree dfs枚举组合情况+最小生成树★
- hdu 2489 Minimal Ratio Tree dfs枚举组合情况+最小生成树★
- poj2245(枚举||组合数学||dfs)
- 2016蓝桥杯省赛C/C++B组7题剪邮票 DFS枚举组合情况BFS判联通
- NYOJ 32 组合数(枚举,dfs)
- bzoj 1024: [SCOI2009]生日快乐(暴力dfs枚举所有情况)
- openjudge 2985 数字组合(dfs子集枚举)
- 51nod 1268 和为K的组合 -dfs枚举
- HDU3699(POJ 3989)A hard Aoshu Problem(暴力模拟dfs)
- DFS 枚举
- Codeforces Round #331 (Div. 2) .D - Wilbur and Trees, 枚举情况的DFS
- 51nod 1268 和为K的组合【Dp/Dfs/折半枚举】
- 用DFS实现排列和组合两种枚举方式【附带例题HDU1015,POJ1753题解】
- UVA 11181 Probability|Given dfs模拟组合情况(周赛F题)
- hdu 2566 统计硬币 dfs 不能排除组合重复的情况 代码不完整
- DFS暴力求某集合上的某种的组合情况
- Mysql----浅入浅出之插入删除等操作
- hdu 2102 A计划 (bfs+队列)
- Decorator装饰模式
- 链接分析算法系列-机器学习排序
- ym——Android从零开始(17)(多线程下载-上)(新)
- [HDU3699]组合情况DFS枚举
- 最大公约数的算法
- 1054. The Dominant Color (20)
- 最近遇到的一系列问题
- 基于memcache架构1TB高速内存池方案设计
- 不要返回局部对象的引用或指针
- ym——Android从零开始(18)(多线程下载-下)(新)
- error LNK2005: _main already defined in xxxx.obj
- jqGrid API