The following cryptarithm is a multiplication problem that can besolved by substituting digits from a specified set of N digits into thepositions marked with *. If the set of prime digits {2,3,5,7} isselected, the cryptarithm is called a PRIME CRYPTARITHM.
* * * x * * ------- * * * <-- partial product 1 * * * <-- partial product 2 ------- * * * *Digits can appear only in places marked by `*'. Of course, leadingzeroes are not allowed.
The partial products must be three digits long, even though thegeneral case (see below) might have four digit partial products.
********** Note About Cryptarithm's Multiplication ************
In USA, children are taught to perform multidigit multiplicationas described here. Consider multiplying a three digit numberwhose digits are 'a', 'b', and 'c' by a two digit number whosedigits are 'd' and 'e':
[Note that this diagram shows far more digits in its results than the required diagram above which has three digit partial products!] a b c <-- number 'abc' x d e <-- number 'de'; the 'x' means 'multiply' ----------- p1 * * * * <-- product of e * abc; first star might be 0 (absent) p2 * * * * <-- product of d * abc; first star might be 0 (absent) ----------- * * * * * <-- sum of p1 and p2 (e*abc + 10*d*abc) == de*abc
Note that the 'partial products' are as taught in USA schools.The first partial product is the product of the final digit of thesecond number and the top number. The second partial product isthe product of the first digit of the second number and the topnumber.
Write a program that will find all solutions to the cryptarithmabove for any subset of supplied non-zero single-digits.
PROGRAM NAME: crypt1
INPUT FORMAT
Line 1: | N, the number of digits that will be used |
Line 2: | N space separated non-zero digits with which to solve the cryptarithm |
SAMPLE INPUT (file crypt1.in)
5 2 3 4 6 8
OUTPUT FORMAT
A single line with the total number of solutions. Hereis the single solution for the sample input:
2 2 2 x 2 2 ------ 4 4 4 4 4 4 --------- 4 8 8 4
SAMPLE OUTPUT (file crypt1.out)
1
水题,暴力=。=
枚举所有三位数,两位数符合情况数。
CODE:
/*
ID: sotifis3
LANG: C++
TASK: crypt1
*/
#include <iostream>
#include <cstdio>
#include <cstring>
using namespace std;
int vis[15];
bool judge(int num)
{
while(num > 0){
if(vis[num%10] != 1) return false;
num /= 10;
}
return true;
}
int main()
{
//freopen("in", "r", stdin);
freopen("crypt1.in","r",stdin);
freopen("crypt1.out","w",stdout);
int n;
while(~scanf("%d", &n)){
memset(vis, 0, sizeof(vis));
for(int i = 0; i < n; ++i){
int a;
scanf("%d", &a);
vis[a] = 1;
}
int ans = 0;
for(int i = 100; i <= 999; ++i){
if(judge(i)){
for(int j = 0; j <= 9; ++j){
int jj = j * i;
if(judge(j) && judge(jj) && jj < 1000){
for(int k = 1; k <= 9; ++k){
int kk = k * i;
if(judge(k) && judge(kk) && kk < 1000){
if(judge(kk * 10 + jj) && (kk * 10 + jj) < 10000 && (kk * 10 + jj) >= 1000)
ans ++;
}
}
}
}
}
}
printf("%d\n", ans);
}
return 0;
}