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 題意:所謂題目給出的牛式,就是要求用所給的數字來替代豎式中的*,然後找出所有滿足條件的豎式的個數 思路:直接暴力被乘數和乘數,只要保證被乘數是3位數,乘數是2位數,得到的part1,part2是三位數,最後的結果是四位數,並且構成這些數的所有數字都必須是給出的數字 代碼:
/*
ID: fanmeng
PROG: crypt1
LANG: C++
*/
#include <iostream>
#include <cstdio>
#include <cmath>
#include <cstring>
#include <algorithm>
#include <queue>
#include <vector>
#include <set>
#include <map>
using namespace std;
int dig[10];
int isok[10];
bool judge(int x) {
while(x) {
if(!isok[x%10]) {
return false;
}
x /= 10;
}
return true;
}
int main() {
freopen("crypt1.in","r",stdin);
freopen("crypt1.out","w",stdout);
int n;
cin >> n;
memset(isok,false,sizeof(isok));
for(int i = 0; i < n; i++) {
cin >> dig[i];
isok[dig[i]] = true;
}
int k = 0;
for(int a = 0; a < n; a++) {
for(int b = 0; b < n; b++) {
for(int c = 0; c < n; c++) {
for(int d = 0; d < n; d++) {
for(int e = 0; e < n; e++) {
int chengshu = dig[a]*100+dig[b]*10+dig[c];
int ss = dig[d]*10+dig[e];
int s1 = chengshu*ss;
if(chengshu >= 100 && ss > 9 && s1 >= 1000 && s1 <= 9999) {
int part1 = dig[d]*chengshu;
int part2 = dig[e]*chengshu;
if(part1 >= 100 && part1 <= 999 && part2 >= 100 && part2 <= 999) {
if(judge(part1)&&judge(part2)&&judge(s1)) {
k++;
}
}
}
}
}
}
}
}
cout << k << endl;
return 0;
}
PS:自己的英語水平很弱,題目讀起來很費勁,但是讀懂之後就很簡單了,加油!!!