Chapter05-Multiple(POJ 1465)

Multiple

Time Limit: 1000MS

Memory Limit: 32768K

Total Submissions: 5911

Accepted: 1284

Description

a program that, given anatural number N between 0 and 4999 (inclusively), and M distinct decimaldigits X1,X2..XM (at least one), finds the smallest strictly positive multipleof N that has no other digits besides X1,X2..XM (if such a multiple exists).

Input

The input has several datasets separated by an empty line, each data set having the following format:

On the first line - the number N
On the second line - the number M
On the following M lines - the digits X1,X2..XM.

Output

For each data set, the programshould write to standard output on a single line the multiple, if such amultiple exists, and 0 otherwise.

An example of input and output:

Sample Input

22

3

7

0

1

 

2

1

1


Sample Output

110
0

Source

SoutheasternEurope 2000

 

題目大意:

       給定一個數N(0<=N<=4999),現在給定若干個十進制的數,能否用給定的十進制的數組成一個數,使其是N的倍數,如果存在,請求出倍數最小的數,如果不存在,請輸出0;

       十進制的數可以無限次重複使用,例如N=22;用7,0,1三個十進制的數可以組成110使其是22的最小的倍數;

 

該題必須解決兩個問題:

1, 怎麼判斷不存在該數的倍數,也就是說在什麼情況下可以停止遍歷;

2, 因爲數的可能大小非常大,我們不能用int來存儲,那麼需要怎麼解決這個問題;

3, 如何保證取出的數是最小的額

 

取餘數操作,從個位數開始,對每個數取餘,若餘數不爲零,那麼對每個餘數添加增加所有的可能性,依次無線循環;何時結束呢?就是取餘的時候,如果發現該餘數已經是第二次出現了,那麼可以丟棄,因此也就是說,最多需要遍歷4999次,把所有的餘數都取了一遍;

存數,我們可以用數組來存儲每一位數,不妨新建一個類,每一種組合新建一個類;類包含成員prev,指向上一個數,打個比方,首次遍歷 分別爲數1,7新建一個類,他們的prev指向空,數1又能衍生出3個類,數1,0,7,此時他們的prev分別指向數1;同時也要有成員modNum保存當前的餘數,也要有一個成員currentNum表示當前的數是多少。

爲了保證取出的數是最小的,我們給這些十進制的數排序,每次取數從小開始取數,因爲是fifo隊列,所以總是從最小的計算;

 

開始動手寫代碼吧。。。

Java代碼如下:

import java.io.BufferedInputStream;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.LinkedList;
import java.util.Scanner;

public class Main {
	// 用來標記所有的餘數,比如餘數32,可以表示allModNum[32]=1;表示已經存在
	private int[] allModNum;

	// 這是存放十進制數的數組;
	private int[] number;
	private int N;

	public Main(int N) {
		this.N = N;
		if (N != 0) {
			allModNum = new int[N];
			Arrays.fill(allModNum, 0);
		}
	}

	public static void main(String[] args) {
		// TODO Auto-generated method stub
		Scanner cin = new Scanner(new BufferedInputStream(System.in));
		
		int N ;
		while (cin.hasNext()) {
			N = cin.nextInt();
			Main ma = new Main(N);
			int countNum;
			countNum = cin.nextInt();
			ma.number = new int[countNum];

			for (int i = 0; i < countNum; i++) {
				ma.number[i] = cin.nextInt();
			}
			
			
			ma.processItBfsAndPrint();
			
			
		}
	}

	private void processItBfsAndPrint() {
		// TODO Auto-generated method stub

		// 輔助實現的隊列;
		LinkedList<Node> al = new LinkedList<Node>();

		Arrays.sort(number);
		

		// 這個事特例哈;
		if (N == 0) {
			System.out.println(0);
			return;
		}
		
		Arrays.fill(allModNum, 0);
		
		if (number[0] != 0) {
			if (number[0] % N == 0) {
				System.out.println(number[0]);
				return;
			}
			al.add(new Node(null, number[0] % N, number[0]));
			allModNum[number[0] % N] = 1;
		}

		for (int i = 1; i < number.length; i++) {

			if (number[i] % N == 0) {
				System.out.println(number[i]);
				return;
			}

			al.add(new Node(null, number[i] % N, number[i]));
			allModNum[number[i] % N] = 1;
		}

		Node nodetemp;

		int modTemp;

		while (al.size() > 0) {
			nodetemp = al.remove();
			for (int i = 0; i < number.length; i++) {

				modTemp = (nodetemp.modNum * 10 + number[i]) % N;

				// 這就是我們需要的值;
				if (modTemp == 0) {
					// 用來暫時保存所有的值
					ArrayList<Integer> ans = new ArrayList<Integer>();
					ans.add(number[i]);
					while (nodetemp != null) {
						ans.add(nodetemp.currentNum);
						nodetemp = nodetemp.prev;
					}

					for (int j = ans.size() - 1; j >= 0; j--) {
						System.out.print(ans.get(j));
					}
					System.out.println();
					return;
				}

				if (allModNum[modTemp] == 0) {
					allModNum[modTemp] = 1;
					al.add(new Node(nodetemp, modTemp, number[i]));
				}
			}
		}

		System.out.println(0);
	}

	static class Node {
		private Node prev;
		private int modNum;
		private int currentNum;

		public Node(Node prev, int modNum, int currentNum) {
			this.prev = prev;
			this.modNum = modNum;
			this.currentNum = currentNum;
		}
	}

}





發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章