Different digits
Time Limit: 10000/4000 MS (Java/others) memory limit: 32768/32768 K (Java/Others)
Total submission (s): 382 accepted submission (s): 90 problem descriptiongiven a positive integer N, your task is to find a positive integer m, which is a multiple of N, and that M contains the least number of different digits when represented in decimal. for example, number 1334 contains three different
Digits 1, 3 and 4.
Inputthe input consists of no more than 50 test cases. each test case has only one line, which contains a positive integer N (1 <= n <65536 ). there are no blank lines between cases. a line with a single '0' terminates the input.
Outputfor each test case, You shocould output one line, which contains M. If there are several possible results, you shocould output the smallest one. Do not output blank lines between cases.
Sample Input
7 15 16 101 0
Sample output
7555161111
Source2004 Asia Regional Shanghai
PS: conclusion in a number theory: For any integer N, there must be a multiple composed of no more than two numbers. Because A, AA, AAA ...... If n + 1 is used, the remainder of two modulo n must be the same, and the offset is the multiple of n m. M is only composed of a and 0. With this conclusion, it is easy to start, so number theory is very important.
Solution: first use a number to search for the answer. In 9 cases, the answer is output after the answer is found. If not, use two numbers to search for the answer, and in 36 cases. Of course, you must note that the answer must be the least number of different numbers, and the answer should be the smallest.
Misunderstanding: search is performed at the same time from small to large. In this case, you cannot simply retrieve the remainder of the sentence. Example 17: if the result is from small to large, the result is 17, and the result is 1111111111111111. Because different numbers affect each other in the search, 111 won't be added to the stack because vis [6] has already been added to the stack.
Code:
# Include <iostream> # include <cstdio> # include <cstring> # include <string> # define maxn 65540 using namespace STD; int n, m, mcnt, tcnt, h; int A [5]; bool vis [maxn]; // obtain the remainder of the sentence, which is string ans, Tans; struct node {int D, Val, pre; // Val-valid value int CNT;} cur, now, Q [maxn]; void getans (int K) // Recursion to get ans avoid adding a string to the struct. Each operation of string is very long {char C; If (k =-1) return; else {getans (Q [K]. PRE); C = Q [K]. d + '0'; tans + = C ;}} bool BFS (int K) {int I, j, nval, ncnt, tval; int head = 0, tail =-1; memset (VIS, 0, sizeof (VIS); for (I = 1; I <= K; I ++) {if (a [I]) {cur. CNT = 0; cur. D = A [I]; cur. pre =-1; cur. val = A [I] % N; vis [cur. val] = 1; Q [++ tail] = cur ;}} while (Head <= tail) {now = Q [head]; nval = now. val; ncnt = now. CNT; If (ncnt> mcnt) break; // pruning if (! Nval) {H = head; tcnt = ncnt; return true ;}for (I = 1; I <= K; I ++) {tval = (nval * 10 + A [I]) % N; If (! Vis [tval]) {vis [tval] = 1; cur. CNT = ncnt + 1; cur. D = A [I]; cur. pre = head; cur. val = tval; Q [++ tail] = cur;} head ++;} return false;} int main () {int I, j, flag; while (scanf ("% d", & N), n) {flag = 0; ans = "XX"; mcnt = 1000000000; for (I = 1; I <= 9; I ++) // You can first search for a number to make up a multiple of N {A [1] = I; If (BFS (1) {tans = ""; getans (h); If (mcnt> tcnt | mcnt = tcnt & Ans> TANS) // compare the size of ANS {ans = Tans; mcnt = tcnt ;}}if (ANS! = "XX") {flag = 1; cout <ans <Endl;} If (FLAG) continue; for (I = 0; I <= 9; I ++) // search two more numbers {A [1] = I; for (j = I + 1; j <= 9; j ++) {A [2] = J; if (BFS (2) {tans = ""; getans (h); If (mcnt> tcnt | mcnt = tcnt & Ans> TANS) {ans = Tans; mcnt = tcnt ;}}}cout <ans <Endl ;}return 0 ;}/ * 17343818234 */