Posted on 2008-05-01 11:14
superman 阅读(370)
评论(0) 编辑 收藏 引用 所属分类:
ZOJ
1 /* Accepted 1136 C++ 00:00.41 992K */
2 #include <queue>
3 #include <string>
4 #include <iostream>
5
6 using namespace std;
7
8 struct rec { int num; string strDigit; };
9
10 int main()
11 {
12 int n, m;
13 while(cin >> n)
14 {
15 cin >> m;
16 int * digit = new int[m];
17 for(int i = 0; i < m; i++)
18 cin >> digit[i];
19
20 if(n == 0)
21 {
22 cout << 0 << endl;
23 delete [] digit;
24 continue;
25 }
26
27 sort(digit, digit + m);
28
29 queue <rec> q;
30
31 rec cur;
32 cur.num = 0;
33 cur.strDigit = "";
34
35 q.push(cur);
36
37 bool r[5000] = {false};
38
39 while(q.empty() == false)
40 {
41 cur = q.front(); q.pop();
42
43 for(int i = 0; i < m; i++)
44 {
45 int x = cur.num * 10 + digit[i];
46
47 if(x == 0)
48 continue;
49
50 rec tmp;
51 if(r[x % n] == false)
52 {
53 r[x % n] = true;
54 tmp.num = x % n;
55 tmp.strDigit = cur.strDigit;
56 tmp.strDigit += char(digit[i] + '0');
57 q.push(tmp);
58 }
59 if(r[0])
60 {
61 cout << tmp.strDigit << endl; goto over;
62 }
63 }
64 }
65 cout << 0 << endl;
66
67 over:
68 delete [] digit;
69 }
70
71 return 0;
72 }
73
74