Question:
Give N and M numbers (one digit)
The multiples of the minimum N value do not include the M number and output-1 does not exist.
Ideas:
First, it is possible that this number is too long, so it cannot be solved by brute force.
Therefore, this question should be a BFS
Why can I use the remainder?
For the current remainder into the queue, it must be the minimum value of the remainder
Next, how should we add things that meet the conditions?
Therefore, the remainder can be determined again, similar to the digital DP record method.
Then add a path record.
Code:
# Include "cstdlib" # include "cstdio" # include "cstring" # include "cmath" # include "queue" # include "algorithm" # include "map" # include "iostream" using namespace STD; int used [12]; struct node {int F, pre, now; // F indicates whether or not, pre precursor, current value of now} mark [12345]; int N, m; void DFS (int x) // print path {If (MARK [X]. PRE) DFS (MARK [X]. PRE); printf ("% d", Mark [X]. now);} void BFS () {int cur, next; queue <int> q; For (INT I = 1; I <= 9; I ++) // if there is no leading 0, put one digit first. {If (used [I]) continue; q. push (I); Mark [I]. F = 1; Mark [I]. pre = 0; Mark [I]. now = I;} while (! Q. empty () {cur = Q. front (); q. pop (); For (INT I = 0; I <10; I ++) {If (used [I]) continue; next = (cur * 10 + I) % N; If (MARK [next]. f) continue; Mark [next]. F = 1; Mark [next]. now = I; Mark [next]. pre = cur; If (next = 0) // satisfies the multiple {DFS (next); return;} Q. push (next) ;}} printf ("% d",-1) ;}int main () {int CAS = 1; while (scanf ("% d ", & N, & M )! =-1) {memset (used, 0, sizeof (used); For (INT I = 0; I <m; I ++) {int X; scanf ("% d", & X); used [x] = 1;} printf ("case % d:", CAS ++ ); if (n <10 &&! Used [N]) // special sentence {printf ("% d \ n", n); continue;} memset (mark, 0, sizeof (Mark )); BFS (); puts ("");} return 0 ;}
[BFS + remainder judgment + path Record] HDU 4474 yet another multiple problem