[USACO]Prime Cryptarithm【JAVA】


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


暴力破解,但在範圍選擇,樹上上加以判定縮短運行時間


































































































































































USER: Singles Xin [xl1993a1]
TASK: crypt1
LANG: JAVA

Compiling...
Compile: OK

Executing...
   Test 1: TEST OK [0.130 secs, 30540 KB]
   Test 2: TEST OK [0.108 secs, 30540 KB]
   Test 3: TEST OK [0.108 secs, 30540 KB]
   Test 4: TEST OK [0.130 secs, 30540 KB]
   Test 5: TEST OK [0.144 secs, 30540 KB]
   Test 6: TEST OK [0.137 secs, 30540 KB]
   Test 7: TEST OK [0.144 secs, 30540 KB]

All tests OK.

Your program ('crypt1') produced all correct answers! This is your submission #8 for this problem. Congratulations!

Here are the test data inputs:


------- test 1 ----
5
2 3 4 6 8
------- test 2 ----
4
2 3 5 7
------- test 3 ----
1
1
------- test 4 ----
7
4 1 2 5 6 7 3
------- test 5 ----
8
9 1 7 3 5 4 6 8
------- test 6 ----
6
1 2 3 5 7 9
------- test 7 ----
9
1 2 3 4 5 6 7 8 9
Keep up the good work!
Thanks for your submission!
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章