usaco 1.3 Prime Cryptarithm

Prime Cryptarithm

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.

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 digits from the set {1,2,3,4,5,6,7,8,9}.

PROGRAM NAME: crypt1

INPUT FORMAT

Line 1: N, the number of digits that will be used
Line 2: N space separated 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 unique 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


题目看了半天才看懂。。。
使题目中提供的数字能够被上式所匹配即可,也就是说,将输入的数字放入规定的式子中,使式子成立。
简单模拟。


代码:


View Code
 1 /*
 2 ID: jings_h1
 3 PROG: crypt1
 4 LANG: C++
 5 */
 6 
 7 #include<iostream>
 8 #include<fstream>
 9 using namespace std;
10 int n;
11 int a[10];
12 bool istrue(int b){
13      int k;
14      if(b/1000!=0){
15          k=1000;
16         // b=b%10000;
17          }
18      else{
19          //b=b%1000;
20          k=100;
21          }
22      for(;k!=0;k=k/10){
23              int temp=b/k;
24              b=b%k;
25              int g;
26              for(g=0;g<n;g++){
27                      if(a[g]==temp)
28                             break;
29                             }
30              if(g>=n)
31                  return false;
32                  }
33      return true;
34      }
35 int main(){ 
36     ofstream fout ("crypt1.out");
37     ifstream fin ("crypt1.in");
38     int mut1[3];
39     int mut2[2];
40         fin>>n;
41         int sum=0;
42         for(int i=0;i<n;i++){
43                 fin>>a[i];
44                 }
45         for(int j1=0;j1<n;j1++){
46                 mut1[0]=a[j1];
47                 for(int j2=0;j2<n;j2++){
48                         mut1[1]=a[j2];
49                         for(int j3=0;j3<n;j3++){
50                                 mut1[2]=a[j3];
51                                 for(int j4=0;j4<n;j4++){
52                                         mut2[0]=a[j4];
53                                         for(int j5=0;j5<n;j5++){
54                                                 mut2[1]=a[j5];
55                                                 int temp1=mut1[0]*100+mut1[1]*10+mut1[2];
56                                                 int temp2=mut2[0];
57                                                 int temp3=mut2[1];      
58                                                 int res1=temp1*temp2;
59                                                 int res2=temp1*temp3;
60                                                 int res3=res1*10+res2;
61                                                 if(res1/1000!=0||res2/1000!=0||res3/10000!=0)
62                                                       continue;
63     
64                                                 if(istrue(res1)&&istrue(res2)&&istrue(res3)){
65                                                          sum++;
66                                                     
67                                                          }
68                                                          }
69                                                          }
70                                                          }
71                                                          }
72                                                          }
73         fout<<sum<<endl;
74     return 0;
75 }
76                                  

 

posted on 2012-12-07 23:03  yumao  阅读(164)  评论(0编辑  收藏  举报

导航