Prime Cryptarithm

来源:互联网 发布:网络教育本科怎么报考 编辑:程序博客网 时间:2024/05/21 23:34


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



哭煞笔一开始没有用标记法,错了都不知道哪里找。

/*ID: des_jas1PROG: crypt1LANG: C++*/#include <iostream>#include <fstream>#include <string.h>//#define fin cin//#define fout coutusing namespace std;int N,set[10];bool used[10];int main() {ofstream fout ("crypt1.out");    ifstream fin ("crypt1.in");int i,j,r,k,q,count=0;int a,b,c,d;fin>>N;memset(used,0,sizeof(used));for(i=0;i<N;i++){fin>>set[i];used[set[i]]=true;}for(i=0;i<N;i++)          for(r=0;r<N;r++)for(j=0;j<N;j++)for(k=0;k<N;k++)for(q=0;q<N;q++){a=set[i]*100+set[r]*10+set[j];b=a*set[k];c=a*set[q];d=b+c*10;if(b>1000 || c>1000 || d>10000)continue;if(!used[b%10] ||!used[c%10] || !used[d%10])continue;b/=10;c/=10;d/=10;if(!used[b%10] || !used[c%10] || !used[d%10])continue;d/=10;if(!used[b/10] || !used[c/10] || !used[d%10] || !used[d/10])continue;count++;}fout<<count<<endl;fout.close();fin.close();    return 0;}