Question: Give a number N, N> = 0, n <5000, then give the number M, and obtain the minimum multiple of n. the ratio must consist of the number of M, for example, if n = 12 and M contains 1 or 2, 12 is the smallest multiple of N and is synthesized by 1 and 2 of M numbers. Note that each number in M can be reused.
Resolution:
First, the analysis shows that because it is the number of digits plus, rather than the number, it is impossible to blindly append the number of digits because it must be processed!
Second, the M number can contain an infinite number based on the meaning of the question.
So here we need a solution, that is, the remainder. In fact, there can be only N results (if 0) for any number-to-N remainder operation ). And there is such a formula. If X % N = Y % N, then X % N * 10 + c = Y % N * 10 + C, we only need to judge the remainder, and combine the remainder and the remaining M arrays. If the obtained number is set and passed before the remainder of N, you do not need to make any further judgments, because you have already done so. That is to say, the number of M here is not important. What is important is that the remainder, once the M array and the number of M.
Next, let's take a rough look.Algorithm:
First, sort the number of M from bottom to big. In this way, the BFS process also traverses from small to large to minimize the result.
Then, start BFS and create a vis array to record the remainder that has been traversed by N. The remainder stored in the queue is then traversed from small to large. It is important to remember to create a parent node array for output.
Finally, it should be noted that for the parent node array, the node to be pointed to is newly created by itself, rather than the M number, which is easy to think.
ExplainCode: In the struct node, store the vertex ID, remainder, parent node, and corresponding number. BFS determines whether there is a multiple, outprintf recursive output. In BFs, the first for loop is used for initialization. Note that 0 cannot be added to the queue because it is the smallest positive integer. Then the while at the end will work normally.
Code:
# Include <cstdio> # include <cstring> # include <queue> # include <cmath> # include <algorithm> using namespace STD; const int n = 6000; const int M = 100001; int n, m, end, CNT, num [m], wnum [m]; bool vis [N]; struct node {int IDN, X, p, ID;} e [N]; int solve (int x) {If (x = 0) return 1; int ans = 0; while (x> 0) {ans + = 1; X/= 10;} return ans;} int fun (int x) {int ans = 1; for (INT I = 0; I <X; ++ I) ans * = 10; return ans;} bool CMP (int A, int B) {return a <B;} bool BFS () {queue <node> q; for (INT I = 0, x; I <m; ++ I) {x = num [I] % N; If (! Vis [x] & num [I]) {vis [x] = true; E [CNT]. X = x, E [CNT]. IDN = num [I], E [CNT]. P =-1, E [CNT]. id = CNT; q. push (E [CNT]); If (x = 0) {end = CNT; return true;} CNT ++ ;}} while (! Q. empty () {node u = Q. front (); q. pop (); For (INT I = 0; I <m; ++ I) {int x = (U. x * Fun (wnum [I]) + num [I]) % N; // printf ("% d \ n", x); If (! Vis [x]) {vis [x] = true; E [CNT]. X = x, E [CNT]. P = u. ID, E [CNT]. IDN = num [I], E [CNT]. id = CNT; If (x = 0) {end = CNT; return true;} Q. push (E [CNT]); CNT ++; }}return false;} void outprintf (int x) {If (x =-1) return; outprintf (E [X]. p); printf ("% d", E [X]. IDN);} int main () {While (scanf ("% d", & N, & M) = 2) {CNT = 0; for (INT I = 0; I <m; ++ I) scanf ("% d", & num [I]); If (n = 0) {printf ("0 \ n"); continue;} Sort (Num, num + M, CMP); For (INT I = 0; I <m; ++ I) wnum [I] = solve (Num [I]); memset (VIS, 0, sizeof (VIS); If (BFS () outprintf (end ); else printf ("0"); printf ("\ n ");}}