USACO1.3.4--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
题解:枚举+剪枝。第一个数肯定是三位数,第二个位数肯定是两位数。其实不剪枝都不会超时的。。。不过还是能剪枝还是剪一下吧。先判断一下两个数的乘积是否是四位数,然后判断第二个数的个位和十位分别乘第一个数是否是三位数,如果符合的话,继续进行处理,看是否符合要求。其中如果第一个数乘以第二个数的十位或者个位大于等于10,那么可以剪枝。
我的代码:
1 /* 2 ID:spcjv51 3 PROG:crypt1 4 LANG:C 5 */ 6 #include<stdio.h> 7 #define maxn 10 8 int ans; 9 int main(void) 10 { 11 freopen("crypt1.in","r",stdin); 12 freopen("crypt1.out","w",stdout); 13 int num[maxn]; 14 int i,j,k,n,a1,a2,a3,b1,b2; 15 scanf("%d",&n); 16 memset(num,0,sizeof(num)); 17 for(i=0; i<n; i++) 18 { 19 scanf("%d",&k); 20 num[k]++; 21 } 22 ans=0; 23 for(i=100; i<1000; i++) 24 for(j=10; j<100; j++) 25 { 26 int total; 27 if((i*j)>9999||(i/100)*(j%10)>9||(i/100)*(j/10)>9) continue; 28 a1=i/100; 29 a2=(i%100)/10; 30 a3=i%10; 31 b1=j/10; 32 b2=j%10; 33 total=0; 34 total=num[a1]+num[a2]+num[a3]+num[b1]+num[b2]; 35 if(total==5) 36 { 37 k=i*(j%10); 38 a1=k/100; 39 a2=(k%100)/10; 40 a3=k%10; 41 total=0; 42 total=num[a1]+num[a2]+num[a3]; 43 if(total==3) 44 { 45 k=i*(j/10); 46 a1=k/100; 47 a2=(k%100)/10; 48 a3=k%10; 49 total=0; 50 total=num[a1]+num[a2]+num[a3]; 51 if(total==3) 52 { 53 k=i*j; 54 total=0; 55 a1=k/1000; 56 a2=(k%1000)/100; 57 a3=(k%100)/10; 58 b1=k%10; 59 total=num[a1]+num[a2]+num[a3]+num[b1]; 60 if(total==4) ans++; 61 } 62 } 63 } 64 } 65 printf("%d\n",ans); 66 return 0; 67 }
我的代码不够简洁,在判断一个数的每一位是否符合要求时重复太多,效率不够高,以后尽量把代码模块化。官方题解就非常的棒,简洁高效。
以下是官网题解:
1 #include <stdio.h> 2 #include <stdlib.h> 3 #include <string.h> 4 #include <assert.h> 5 6 int isgooddigit[10]; /* isgooddigit[d] is set if d is an acceptable digit 7 */ 8 9 /* check that every decimal digit in "n" is a good digit, 10 and that it has the right number "d" of digits. */ 11 int 12 isgood(int n, int d) 13 { 14 if(n == 0) 15 return 0; 16 17 while(n) { 18 if(!isgooddigit[n%10]) 19 return 0; 20 n /= 10; 21 d--; 22 } 23 24 if( d == 0 ) 25 return 1; 26 else 27 return 0; 28 } 29 30 /* check that every product line in n * m is an okay number */ 31 int 32 isgoodprod(int n, int m) 33 { 34 if(!isgood(n,3) || !isgood(m,2) || !isgood(n*m,4)) 35 return 0; 36 37 while(m) { 38 if(!isgood(n*(m%10),3)) 39 return 0; 40 m /= 10; 41 } 42 return 1; 43 } 44 45 void 46 main(void) 47 { 48 int i, j, n, nfound; 49 FILE *fin, *fout; 50 51 fin = fopen("crypt1.in", "r"); 52 fout = fopen("crypt1.out", "w"); 53 assert(fin != NULL && fout != NULL); 54 55 for(i=0; i<10; i++) { 56 isgooddigit[i] = 0; 57 } 58 fscanf(fin, "%d", &n); 59 for(i=0; i<n; i++) { 60 fscanf(fin, "%d", &j); 61 isgooddigit[j] = 1; 62 } 63 64 nfound = 0; 65 for(i=100; i<1000; i++) 66 for(j=10; j<100; j++) 67 if(isgoodprod(i, j)) 68 nfound++; 69 70 fprintf(fout, "%d\n", nfound); 71 exit(0); 72 }