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.

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

 

代码:

 1 #include <iostream>
 2 #include <cstdio>
 3 #include <cstring>
 4 using namespace std;
 5 bool p[10];
 6 bool check(int c)
 7 {
 8     while(c){
 9         if(p[c%10]==false) return false; 
10         c/=10;
11     }
12     return true;
13 }
14 int main()
15 {
16     freopen("crypt1.in","r",stdin);
17     freopen("crypt1.out","w",stdout);
18     int a[10];
19     int n;
20     cin>>n;
21     for(int i=0;i<n;i++){
22         cin>>a[i];
23         p[a[i]]=true;
24     }
25     long long cnt=0;
26     for(int i=0;i<n;i++){
27         for(int j=0;j<n;j++){
28             for(int m=0;m<n;m++){
29                 for(int k=0;k<n;k++){
30                     for(int x=0;x<n;x++){
31                         int b=a[i]*100+a[j]*10+a[m];
32                         int c=a[k]*10+a[x];
33                         int d=b*c;
34                         if(b*a[k]>1000||!check(b*a[k])) continue;
35                         if(b*a[x]>1000||!check(b*a[x])) continue;
36                         if(d>10000||!check(d)) continue;
37                         //cout<<b<<" "<<c<<endl;
38                         cnt++;
39                     }
40                 }
41             }
42         }
43     }
44     cout<<cnt<<endl;
45     return 0;
46 }

 

posted on 2016-01-25 02:19  Sunny糖果  阅读(232)  评论(0编辑  收藏  举报