superman

聚精会神搞建设 一心一意谋发展
posts - 190, comments - 17, trackbacks - 0, articles - 0
   :: 首页 :: 新随笔 :: 联系 :: 聚合  :: 管理

ZOJ 1136 - Multiple

Posted on 2008-05-01 11:14 superman 阅读(362) 评论(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 

只有注册用户登录后才能发表评论。
网站导航: 博客园   IT新闻   BlogJava   知识库   博问   管理