USACO 1.3 Prime Cryptarithm
1Y,没写DFS,直接5个for。。
1 /* 2 ID: cuizhe 3 LANG: C++ 4 TASK: crypt1 5 */ 6 #include <iostream> 7 #include <cstdio> 8 #include <cstring> 9 #include <cstdlib> 10 #include <map> 11 #include <algorithm> 12 using namespace std; 13 int p[11],o[11]; 14 int judge(int x) 15 { 16 while(x > 0) 17 { 18 if(!o[x%10]) 19 return 0; 20 x = x/10; 21 } 22 return 1; 23 } 24 int main() 25 { 26 int i,j,n,k,u,q,ans; 27 int t1,t2,t3; 28 freopen("crypt1.in","r",stdin); 29 freopen("crypt1.out","w",stdout); 30 scanf("%d",&n); 31 for(i = 1;i <= n;i ++) 32 { 33 scanf("%d",&p[i]); 34 o[p[i]] = 1; 35 } 36 ans = 0; 37 for(i = 1;i <= n;i ++) 38 { 39 for(j = 1;j <= n;j ++) 40 { 41 for(k = 1;k <= n;k ++) 42 { 43 for(u = 1;u <= n;u ++) 44 { 45 for(q = 1;q <= n;q ++) 46 { 47 t1 = (p[i]*100+10*p[j]+p[k])*p[u]; 48 t2 = (p[i]*100+10*p[j]+p[k])*p[q]; 49 t3 = (p[i]*100+10*p[j]+p[k])*(p[q]*10+p[u]); 50 if(t1<1000&&t1>=100&&t2<1000&&t2>=100&&t3>=1000&&t3<10000&&judge(t1)&&judge(t2)&&judge(t3)) 51 { 52 ans ++; 53 } 54 } 55 } 56 } 57 } 58 } 59 printf("%d\n",ans); 60 return 0; 61 }