Test instructions: N and m,m decimal Unit Number (0-9), the smallest number of ans, which causes ans to be a multiple of n (except 0), and ans does not include the input decimal number. There is no output-1.
Using BFS Search, small to large enumeration can ensure that the number of construction is incremental, if you do not judge the direct search, the complexity is very high. If a number%n==0, this number is a multiple of N. In the absence of the time, if a%n==b%n, and a<b, then actually can take a without taking B, because if the end of a C can make ac%n==0, then bc%n is equal to 0, if a and B after the addition of%n==0, the optimal conditions will be the same number of additions. So just maintain the combination of the number of%n value can, if the search on the way to the same%n value, it can be ignored, certainly not the previous better.
#include <iostream> #include <queue>using namespace std; #define N 10010int p[n]; PRE[V] Record reaches V of the previous node uint num[n]; NUM[V] records the last digit of the form v bool a[10]; Dropped data int n;void Init () {memset (p,-1,sizeof (P)); memset (num,-1,sizeof (num)); memset (A,false,sizeof (a));} void Show (int u) //Recursive print result {if (p[u]!=-1) Show (P[u]);p rintf ("%d", Num[u]);} void BFs () {int i,t,u;queue<int> q;for (i=1;i<10;i++) //Note 0 does not count, to remove if (!a[i]) {t=i%n;if (t==0) // The result is only a single digit case {printf ("%d", i); return;} Q.push (t); num[t]=i;} while (!q.empty ()) {U=q.front (), Q.pop (), for (i=0;i<10;i++) if (!a[i]) {t= (10*u+i)%n;if (num[t]==-1)//////not seen before {Q.push (t);p [T]=u;num[t]=i;} if (t==0) {Show (t); return;}}} printf ("-1");} int main () {int k=0,m,x;while (scanf ("%d%d", &n,&m) ==2) {Init (); while (m--) {scanf ("%d", &x); a[x]=true;} printf ("Case%d:", ++k), BFS ();p utchar (' \ n ');} return 0;}
HDU ACM 4474 Yet another multiple problem-> number theory (digital BFS)