Zju1136 Multiple N的倍数

1289: Zju1136 Multiple N的倍数

Time Limit: 1 Sec  Memory Limit: 256 MB
Submit: 62  Solved: 46
[Submit][Status][Web Board]

Description

a program that, given a natural number N between 0 and 4999 (inclusively), and M distinct decimal digits X1,X2..XM (at least one), finds the smallest strictly positive multiple of N that has no other digits besides X1,X2..XM (if such a multiple exists).

Input

On the first line - the number N
On the second line - the number M
On the following M lines - the digits X1,X2..XM.

Output

For each data set, the program should write to standard output on a single line the multiple, if such a multiple exists, and 0 otherwise.

Sample Input

22
3
7
0
1

Sample Output

110

HINT

 

Source

 1 #include <iostream>
 2 using namespace std;
 3 int num[10000],y[10000],pp[10000];
 4 bool o[5000];
 5 int f[10];
 6 int m,n,i,j,k,temp,head,tail;
 7 
 8 void output(int head)
 9 {
10     if (pp[head]!=0) 
11     output (pp[head]);
12     cout<<num[head];
13 }
14 void bfs()
15 {
16     head=0; 
17     tail=0; 
18     k=0;
19     do
20     {
21         for (i=1;i<=m;i++)
22         {
23             if (head==0 && f[i]==0) continue;
24             if (o[(y[head]*10+f[i])%n]) continue;
25             tail++; 
26             y[tail]=(y[head]*10+f[i])%n;
27             pp[tail]=head; 
28             num[tail]=f[i]; 
29             o[y[tail]]=true;
30             if (y[tail]==0)
31             {
32                 output(tail); 
33                 k=1; 
34                 break;
35             }
36         }
37         if (k==1) break; 
38         head++;
39     } while(head<=tail);
40 }
41 int main()
42 {
43         cin>>n>>m;
44         for (i=1;i<=m;i++)
45         cin>>f[i];
46         if (n==0)
47         {
48             cout<<0<<endl;
49             return 0;
50         }
51         for (i=1;i<=m-1;i++)
52         for (j=i+1;j<=m;j++)
53         if (f[j]<f[i])
54         {
55             temp=f[j]; 
56             f[j]=f[i]; 
57             f[i]=temp;
58         }
59         bfs();
60         if (k==0)
61         cout<<0<<endl;
62         cout<<endl;
63     return 0;
64 }
multiple

 

posted @ 2017-05-06 18:20  LHR-LHR  阅读(180)  评论(0编辑  收藏  举报