HDU 3699
A hard Aoshu Problem
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 62768/32768 K (Java/Others) Total Submission(s): 548 Accepted Submission(s): 271
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
Source
Recommend
chenyongf
模拟题,每个字母代表一个数字,最多有五个字母,暴力枚举即可。
1 #include<iostream> 2 using namespace std; 3 char s1[10],s2[10],s3[10]; //记录输入的字符 4 char ss1[10],ss2[10],ss3[10]; 5 char solvechar[10],beginchar[10]; 6 char solveint[10]; 7 char judgerepeat[10]; 8 int lena,lenb,lenc; 9 int num_beginchar; 10 int num_solvechar; 11 int sum; 12 void solve(int already_char) 13 { 14 if(already_char==num_solvechar) //检验是否符合条件 15 { 16 int a,b,c; 17 int i,j; 18 for(i=0;i<already_char;i++) 19 { 20 for(j=0;j<lena;j++) 21 { 22 if(s1[j]==solvechar[i]) 23 ss1[j]=solveint[i]; 24 } 25 for(j=0;j<lenb;j++) 26 { 27 if(s2[j]==solvechar[i]) 28 ss2[j]=solveint[i]; 29 } 30 for(j=0;j<lenc;j++) 31 { 32 if(s3[j]==solvechar[i]) 33 ss3[j]=solveint[i]; 34 } 35 } 36 sscanf(ss1,"%d",&a); //把字符数组转化为整数 37 sscanf(ss2,"%d",&b); 38 sscanf(ss3,"%d",&c); 39 if(a*b==c) 40 sum++; 41 if(a-b==c) 42 sum++; 43 if(a+b==c) 44 sum++; 45 if(b!=0 && a/b==c && a%b==0) //必须为整除 46 sum++; 47 return; 48 } 49 int beginnum; 50 if(strlen(beginchar) && strchr(beginchar,solvechar[already_char])!=NULL) 51 beginnum=1; 52 else 53 beginnum=0; 54 int i,j; 55 for(j=beginnum;j<10;j++) 56 { 57 if(strchr(judgerepeat,(j+'0'))==NULL) // 枚举 各种情况 58 { 59 judgerepeat[already_char]=j+'0'; 60 solveint[already_char]=j+'0'; 61 solve(already_char+1); 62 judgerepeat[already_char]='\0'; //把上一步处理过的数字删除 63 } 64 } 65 return; 66 } 67 int main() 68 { 69 freopen("in.txt","r",stdin); 70 int T; 71 cin>>T; 72 while(T--) 73 { 74 sum=0; 75 memset(ss1,0,sizeof(ss1)); 76 memset(ss2,0,sizeof(ss2)); 77 memset(ss3,0,sizeof(ss3)); 78 memset(beginchar,0,sizeof(beginchar)); 79 memset(solveint,0,sizeof(solveint)); 80 memset(judgerepeat,0,sizeof(judgerepeat)); 81 memset(solvechar,0,sizeof(solvechar)); 82 cin>>s1>>s2>>s3; 83 int i,j; 84 lena=strlen(s1); 85 lenb=strlen(s2); 86 lenc=strlen(s3); 87 num_beginchar=0; 88 if(strchr(beginchar,s1[0])==NULL&&lena>1) //记录第一个字母 89 { 90 beginchar[num_beginchar++]=s1[0]; 91 } 92 if(strchr(beginchar,s2[0])==NULL&&lenb>1) 93 { 94 beginchar[num_beginchar++]=s2[0]; 95 } 96 if(strchr(beginchar,s3[0])==NULL&&lenc>1) 97 { 98 beginchar[num_beginchar++]=s3[0]; 99 } 100 num_solvechar=0; 101 for(i=0;i<lena;i++) //记录 有多少个字母需要处理 102 { 103 if(strchr(solvechar,s1[i])==NULL) 104 { 105 solvechar[num_solvechar++]=s1[i]; 106 } 107 } 108 for(i=0;i<lenb;i++) 109 { 110 if(strchr(solvechar,s2[i])==NULL) 111 { 112 solvechar[num_solvechar++]=s2[i]; 113 } 114 } 115 for(i=0;i<lenc;i++) 116 { 117 if(strchr(solvechar,s3[i])==NULL) 118 { 119 solvechar[num_solvechar++]=s3[i]; 120 } 121 } 122 solve(0); 123 cout<<sum<<endl; 124 } 125 return 0; 126 }
浙公网安备 33010602011771号