-
Notifications
You must be signed in to change notification settings - Fork 0
/
h4474.cpp
82 lines (71 loc) · 1.36 KB
/
h4474.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
#include <stdio.h>
#include <cstring>
#include <queue>
#include <algorithm>
#include <iostream>
using namespace std;
#define MAX 10004
int del[10];
int n = 0, m = 0;
int visited[MAX];
int pre[MAX];
char text[MAX];
int bfs()
{
int yu = 0, current = 0;
queue<int> thisQueue;
thisQueue.push(0);
while(!thisQueue.empty())
{
current = thisQueue.front();
thisQueue.pop();
for(int i = 0; i < 10; i ++)
{
if (del[i] == 1 || i == 0 && current == 0)
continue;
yu = (current * 10 + i) % n;
if (visited[yu])
continue;
text[yu] = '0' + i;
pre[yu] = current;
thisQueue.push(yu);
visited[yu] = 1;
if (yu == 0)
return 1;
}
}
return 0;
}
void printAns()
{
string ans;
int p = 0;
while (p != 0 || ans.empty())
{
ans += text[p];
p = pre[p];
}
reverse (ans.begin(), ans.end());
puts(ans.c_str());
}
int main()
{
int caseNo = 0;
while(scanf ("%d%d", &n, &m) != EOF)
{
memset(del,0,sizeof(del));
memset(visited,0,sizeof(visited));
while (m--)
{
int num = 0;
scanf ("%d", &num);
del[num] = 1;
}
printf("Case %d: ", ++caseNo);
if (bfs())
printAns();
else
printf("%d\n", -1);
}
return 0;
}