HDOJ3699 A hard Aoshu Problem[暴力]
2014-12-03 20:14
337 查看
[align=left]Problem Description[/align]
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.
[align=left]Input[/align]
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.
[align=left]Output[/align]
For each test case, print an integer in a line. It represents the number of solutions.
[align=left]Sample Input[/align]
2
A A A
BCD BCD B
[align=left]Sample Output[/align]
5
72
题意:给三个串s1,s2,s3,长度最多是8,由ABCDE五个字母组成,每个字母代表不同的数字,问能使s1+s2=s3或s1-s2=s3或s1*s2=s3或s1/s2=s3成立的方案有多少种。注意不能有前导零。
只需枚举每个字母代表什么数字即可,某个字母不存在就把它跳过。
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.
[align=left]Input[/align]
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.
[align=left]Output[/align]
For each test case, print an integer in a line. It represents the number of solutions.
[align=left]Sample Input[/align]
2
A A A
BCD BCD B
[align=left]Sample Output[/align]
5
72
题意:给三个串s1,s2,s3,长度最多是8,由ABCDE五个字母组成,每个字母代表不同的数字,问能使s1+s2=s3或s1-s2=s3或s1*s2=s3或s1/s2=s3成立的方案有多少种。注意不能有前导零。
只需枚举每个字母代表什么数字即可,某个字母不存在就把它跳过。
#include<iostream> #include<cassert> #include<cstdio> #include<cstring> #include<algorithm> #include<cmath> #include<string> #include<iterator> #include<cstdlib> #include<vector> #include<stack> #include<queue> #include<map> #include<set> using namespace std; #define debug(x) cout<<"debug "<<x<<endl; #define rep(i,f,t) for(int i = (f),_end_=(t); i <= _end_; ++i) #define rep2(i,f,t) for(int i = (f),_end_=(t); i < _end_; ++i) #define dep(i,f,t) for(int i = (f),_end_=(t); i >= _end_; --i) #define dep2(i,f,t) for(int i = (f),_end_=(t); i > _end_; --i) #define clr(c, x) memset(c, x, sizeof(c) ) typedef long long int64; const int INF = 0x5f5f5f5f; const double eps = 1e-8; //***************************************************** int num[128]; //某个字母代表的数字,-1表示该字母不存在. char a[6],b[6],c[6]; string s1,s2,s3; int n1,n2,n3; bool ok(int i){ dep(j,i-1,'A')if(num[j] == num[i])return false; return true; } bool zerook(int i){ if(s1[0]==i&&s1.size()!=1 || s2[0]==i&&s2.size()!=1 || s3[0]==i&&s3.size()!=1)return false; return true; } int dfs(int i) { if(i == 'F'){ n1 = n2 = n3 = 0; rep(j,0,s1.size()-1){ n1 *= 10; n1 += num[s1[j]]; } rep(j,0,s2.size()-1){ n2 *= 10; n2 += num[s2[j]]; } rep(j,0,s3.size()-1){ n3 *= 10; n3 += num[s3[j]]; } int ans = 0; if(n1+n2 == n3)++ans; if(n1-n2 == n3)++ans; if(n1*n2 == n3)++ans; if(n2 && n1%n2==0 && n1/n2 == n3)++ans; //注意要整除 return ans; } if(num[i] == -1){ return dfs(i+1); }else{ int ans = 0; num[i] = 0; if(!zerook(i))++num[i]; for(; num[i] < 10; ++num[i]){ if(ok(i)){ ans += dfs(i+1); } } return ans; } } int main() { int T; scanf("%d",&T); while(T--){ clr(num,-1); scanf("%s%s%s",a,b,c); s1 = a;s2 = b;s3 = c; rep(i,0,s1.size()-1)num[s1[i]] = 0; rep(i,0,s2.size()-1)num[s2[i]] = 0; rep(i,0,s3.size()-1)num[s3[i]] = 0; int ans = dfs('A'); printf("%d\n",ans); } return 0; }
相关文章推荐
- HDOJ3699 A hard Aoshu Problem[暴力]
- HDOJ 题目A very hard Aoshu problem(暴力,DFS)
- HDU 3699 A hard Aoshu Problem (暴力搜索)
- HDU 3699(J) ——A hard Aoshu Problem(DFS暴力搜索)
- 2010 Asia Fuzhou Regional Contest HDOJ 3699 A hard Aoshu Problem
- HDU 3699 A hard Aoshu Problem(暴力枚举)(2010 Asia Fuzhou Regional Contest)
- hdu 3699 10 福州 现场 J - A hard Aoshu Problem 暴力 难度:0
- HDU 3699 A hard Aoshu Problem (暴力搜索)
- A hard Aoshu Problem HDU - 3699 模拟 map
- A hard Aoshu Problem HDU - 3699
- 【HDOJ】4403 A very hard Aoshu problem
- HDU 4403 A very hard Aoshu problem (DFS暴力)
- HDU 4403 A very hard Aoshu problem(DFS+暴力)
- HDU3699(POJ 3989)A hard Aoshu Problem(暴力模拟dfs)
- HDU 3699 A hard Aoshu Problem
- HDU 4403 A very hard Aoshu problem (暴力+状态压缩)
- HDU 4403 A very hard Aoshu problem (DFS暴力)
- B - A very hard Aoshu problem 【状态压缩+暴力】
- hdu_4403 A very hard Aoshu problem 暴力搜索
- [ACM] hdu 4403 A very hard Aoshu problem (DFS暴搜数字)