[USACO]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

本題還是比較容易理解的,只是乍一看題目不知道從何入手。本章節提示說使用Greed,但是並不是很好看出怎麼“貪婪”入手。題目和編程之美中《4.10數字啞謎和迴文》看起來有些相似,但是卻不盡然。
爲了簡化本題,首先是試探$$$*$=$$$的可取值,然後再做進一步處理。
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashSet;
import java.util.Iterator;
public class crypt1 {
	private static int[] res_digits = new int[10];
	/**
	 * @param args
	 * @throws IOException 
	 */
	public static void main(String[] args) throws IOException {
		// TODO Auto-generated method stub
		BufferedReader br = new BufferedReader(new FileReader("crypt1.in"));
		FileWriter fout = new FileWriter("crypt1.out");
		int dig_number = Integer.parseInt(br.readLine());
		ArrayList<Integer> list = new ArrayList<Integer>();
		int tmp = 0 ;
		String[] source = br.readLine().split(" ");
		for(int i = 0;i<dig_number;i++){
			tmp = Integer.parseInt(source[i]);
			list.add(tmp);
			res_digits[tmp] = 1;
		}
		Collections.sort(list);
		
		tmp = 0 ;
		int counter = 0;
		int result = 0;
		for(int h = 0;h<dig_number;h++){
			for(int t = 0;t<dig_number;t++){
				for(int s = 0;s<dig_number;s++){
					HashSet<Integer> set = new HashSet<Integer>();
					for(int n = 0;n<dig_number&&(list.get(h)*list.get(n)<10);n++){
						tmp = list.get(h)*100+list.get(t)*10+list.get(s);
						tmp = tmp * list.get(n);
						if(check(tmp,3)){
							set.add(tmp);
							Iterator<Integer> iterator = set.iterator();
							while(iterator.hasNext()){
								result = (Integer)iterator.next();
								//this product is the result of head partial of second plus fisrt
								if(check(tmp*10 +result,4)){
									counter++;
									System.out.println(list.get(h)+" "+list.get(t)+" "+list.get(s)+" "+(tmp*10 +result));
								}
								//this product is the result of tail partial of second plus fisrt
								if((tmp*10 +result!=tmp +result*10)&&check(tmp +result*10,4)){
									counter++;
									System.out.println(list.get(h)+" "+list.get(t)+" "+list.get(s)+" "+(tmp +result*10));
								}	
							}
						}
					}
				}
			}
		}
		fout.write(counter+"\n");
		fout.flush();
		fout.close();
		br.close();
		System.exit(0);
	}

	private static boolean check(int tmp, int i) {
		int ten_i = (int)Math.pow(10, i);
		int ten_i_1 = (int)Math.pow(10, i-1);
		if(tmp/ten_i>0||tmp/ten_i_1<0)
			return false;
		while(i>0){
			if(res_digits[tmp%10]!=1){
				return false;
			}
			tmp = tmp/10;
			i--;
		}
		return true;
	}

}


發佈了29 篇原創文章 · 獲贊 0 · 訪問量 3萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章