The following cryptarithm is a multiplication problem that can be solved by substituting digits from a specified set of N digits into the positions marked with *. If the set of prime digits {2,3,5,7} is selected, the cryptarithm is called a PRIME CRYPTARITHM.
* * *
x * *
-------
* * * <-- partial product 1
* * * <-- partial product 2
-------
* * * *
Digits can appear only in places marked by `*'. Of course, leading zeroes are not allowed.
The partial products must be three digits long, even though the general case (see below) might have four digit partial products.
********** Note About Cryptarithm's Multiplication ************
In USA, children are taught to perform multidigit multiplication as described here. Consider multiplying a three digit number whose digits are 'a', 'b', and 'c' by a two digit number whose digits 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 the second number and the top number. The second partial product is the product of the first digit of the second number and the top number.
Write a program that will find all solutions to the cryptarithm above for any subset of supplied non-zero single-digits.
以下的密碼是乘法問題,可以通過将指定的N個數字組中的數字替換為标有*的位置來解決。如果選擇了素數組{2,3,5,7},則将密碼稱為PRIME CRYPTARITHM。
* * *
X * *
-------
* * * < - 部分産品1
* * * < - 部分産品2
-------
* * * *
數字隻能出現在标有“*”的地方。當然,不允許使用前導零。
部分産品必須是三位數長,即使一般情況(見下文)可能有四位數的部分産品。
**********關于Cryptarithm乘法的注釋************
在美國,教孩子們按照這裡的描述進行多位乘法。考慮将數字為'a','b'和'c'的三位數乘以一個兩位數字,其數字為'd'和'e':
[請注意,此圖表顯示的結果中的數字遠遠超過
上面所需的圖表有三位數的部分産品!]
abc < - number'abc'
xde < - number'de'; 'x'表示'倍增'
-----------
p1 * * * * < - e * abc的乘積; 第一顆星可能是0(缺席)
p2 * * * * < - d * abc的乘積; 第一顆星可能是0(缺席)
-----------
* * * * * < - p1和p2之和(e * abc + 10 * d * abc)== de * abc
請注意,“部分産品”與美國學校一樣。第一個部分産品是第二個數字和最高數字的最後一位數的乘積。第二部分産品是第二個數字的第一個數字和最高數字的乘積。
編寫一個程式,可以找到上述密碼的所有解決方案,用于所提供的非零個位數的任何子集。
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. Here is 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位數字的産品,并檢視是否使用了所有正确的數字。
代碼如下:
/*
ID: zhaoxia13
LANG: C
TASK: crypt1
*/
#include <stdio.h>
#include <stdlib.h>
int n, a[20], p[10];
int f[10], sum, x1, y1, x2, y2, ans;
void dfs(int k)
{
int i;
if(k == 5)
{
x1 = f[1]*100 + f[2]*10 + f[3];
x2 = f[5]*x1;
y2 = f[4]*x1;
if(x2 >= 1000 || y2 >= 1000)
return;
while(x2)
{
if(p[x2%10] == 0)
return;
x2=x2/10;
}
while(y2)
{
if(p[y2%10] == 0)
return;
y2 = y2/10;
}
x2 = f[5]*x1;
y2 = f[4]*x1;
sum = x2 + y2*10;
if(sum >= 10000)
return;
while(sum)
{
if(p[sum%10] == 0)
return;
sum = sum/10;
}
ans++;
return;
}
for(i = 1; i <= n; i++)
{f[k + 1] =a [i];dfs(k+1);}
}
int main()
{
int i;
freopen("crypt1.in", "r", stdin);
freopen("crypt1.out", "w", stdout);
scanf("%d", &n);
for(i = 1; i <= n; i++)
{
scanf("%d", &a[i]);
p[a[i]] = 1;
}
dfs(0);
printf("%d\n", ans);
fclose(stdin);
fclose(stdout);
return 0;
}
/*
ID: zhaoxia13
LANG: C
TASK: crypt1
*/
#include <stdio.h>
#include <stdlib.h>
int n, a[20], p[10];
int f[10], sum, x1, y1, x2, y2, ans;
void dfs(int k)
{
int i;
if(k == 5)
{
x1 = f[1]*100 + f[2]*10 + f[3];
x2 = f[5]*x1;
y2 = f[4]*x1;
if(x2 >= 1000 || y2 >= 1000)
return;
while(x2)
{
if(p[x2%10] == 0)
return;
x2=x2/10;
}
while(y2)
{
if(p[y2%10] == 0)
return;
y2 = y2/10;
}
x2 = f[5]*x1;
y2 = f[4]*x1;
sum = x2 + y2*10;
if(sum >= 10000)
return;
while(sum)
{
if(p[sum%10] == 0)
return;
sum = sum/10;
}
ans++;
return;
}
for(i = 1; i <= n; i++)
{f[k + 1] =a [i];dfs(k+1);}
}
int main()
{
int i;
freopen("crypt1.in", "r", stdin);
freopen("crypt1.out", "w", stdout);
scanf("%d", &n);
for(i = 1; i <= n; i++)
{
scanf("%d", &a[i]);
p[a[i]] = 1;
}
dfs(0);
printf("%d\n", ans);
fclose(stdin);
fclose(stdout);
return 0;
}