1 #include 2 #include 3 #include 4 #include 5 using namespace std; 6 int pre[10010],num[10010],a[10005],n; 7 void print(int x) 8 { 9 if (pre[x]!=-1) print(pre[x]);10 printf("%d",num[x]);11 }12 void bfs(int x)13 {14 queue q;15 while (!q.empty()) q.pop();16 for (int i=1;i<=9;i++)17 {18 if (i%n==0&&a[i]==0) {printf("%d",i); return; }19 if (a[i]==0) {num[i]=i; q.push(i); }20 }21 while (!q.empty())22 {23 int x=q.front(); q.pop();24 for (int i=0;i<=9;i++)25 if (!a[i])26 {27 int y=(x*10+i)%n;28 if (num[y]==-1) {q.push(y); pre[y]=x; num[y]=i; }29 if (y==0) {print(y); return; }30 }31 }32 printf("-1");33 }34 int main()35 {36 int cnt=0,m,i,x;37 while (~scanf("%d%d",&n,&m))38 {39 memset(a,0,sizeof(a));40 memset(num,-1,sizeof(num));41 memset(pre,-1,sizeof(pre));42 for (i=1;i<=m;i++)43 {44 scanf("%d",&x);45 a[x]=1;46 }47 printf("Case %d: ",++cnt);48 bfs(n);49 printf("\n");50 }51 }