Yet another multi ple problem (HDU 4474)

Source: Internet
Author: User
There are tons of problems about integer multiples. Despite the fact that the topic is not original, the content is highly challenging. That’s why we call it “Yet Another Multiple Problem”.

In this problem, you're asked to solve the following question: given a positive integer N and M decimal digits, what is the minimal positive multiple of N whose decimal notation does not contain any of the given digits?
 

Input
There are several test cases.
For each test case, there are two lines. The first line contains two integers n and M (1 ≤ n ≤104). The second line contains M decimal digits separated by spaces.
Input is terminated by EOF.
 

Output
For each test case, output one line "case X: Y" where X is the test case number (starting from 1) while Y is the minimal multiple satisfying the above-mentioned conditions or "-1" (without quotation marks) in case there does not exist such a multiple.
 

Sample Input
2345 3
7 8 9
100 1
0
 

Sample output
Case 1: 2345
Case 2:-1
 

Source
2012 Asia Chengdu Regional Contest
 

Recommend

Liuyiding

Question Analysis: When I got into touch with this question, I added the Brute Force that my teammates thought twice. When I added a card, the results were timed out over and over again, wrong answer, now, I think it was silly and irrational.
The main idea of this question is BFs, which sorts the numbers that can be used in the smallest lexicographically ordered BFS until the number that can be divisible by N is found. Bare BFS will undoubtedly time out. We found that N is less than 10000,
That is to say, the number after N is obtained is less than 10000. Can I write some articles on this? In fact, if the two numbers have the same remainder for N, if the larger number is unnecessary, you can use a bool array to record the remainder. In this way, the extended node is about 10000, which greatly improves the efficiency by 174 Ms AC, 20 ms is too wasteful.

# Include <iostream> # include <cstdio> # include <cstring> # include <cstdlib> struct {int R; // the remainder of the current node, int F; // The parent pointer of the current node char C; // the last digit of the remainder of the current node} Q [10005], TMP; void print (int K) {If (Q [K]. f! =-1) {print (Q [K]. f);} printf ("% C", Q [K]. c);} int main () {int n, m, X, L, R, res, I; bool vis [10], p [10005]; int case = 1; while (~ Scanf ("% d", & N, & M) {memset (VIS, true, sizeof (VIS); While (M --) {scanf ("% d", & X); vis [x] = false;} l = 1; r = 0; Res = 0; memset (p, false, sizeof (p); for (I = 1; I <10; I ++) if (vis [I] &! P [I % N]) {r ++; P [I % N] = true; Q [R]. C = I + '0'; Q [R]. R = I % N; Q [R]. F =-1; if (I % N = 0) {res = r; break ;}} while (L <= R &&! Res) {for (I = 0; I <10; I ++) if (vis [I]) {TMP. C = I + '0'; TMP. R = (Q [l]. R * 10 + I) % N; TMP. F = L; If (! P [TMP. r]) {r ++; Q [R] = TMP; P [TMP. r] = true; If (TMP. R = 0) {res = r; break ;}} l ++;} If (! Res) printf ("case % d:-1 \ n", Case ++); else {printf ("case % d:", Case ++ ); print (RES); printf ("\ n") ;}} return 0 ;}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.