HDU4474

Yet Another Multiple Problem

Time Limit: 40000/20000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)
Total Submission(s): 5496    Accepted Submission(s): 1257


Problem Description

There are tons of problems about integer multiples. Despite the fact that the topic is not original, the content is highly challenging. That’s why we call it “Yet Another Multiple Problem”.
In this problem, you’re asked to solve the following question: Given a positive integer n and m decimal digits, what is the minimal positive multiple of n whose decimal notation does not contain any of the given digits?
 

Input

There are several test cases.
For each test case, there are two lines. The first line contains two integers n and m (1 ≤ n ≤ 104). The second line contains m decimal digits separated by spaces.
Input is terminated by EOF.
 

Output

For each test case, output one line “Case X: Y” where X is the test case number (starting from 1) while Y is the minimal multiple satisfying the above-mentioned conditions or “-1” (without quotation marks) in case there does not exist such a multiple.
 

Sample Input

2345 3
7 8 9
100 1
0

Sample Output

Case 1: 2345
Case 2: -1
 
题意:
求n的最小倍数x,不包含m个特定的数字。
思路:
按数字位进行搜索,状态数最多只有10000种。
 1 //2016.9.5
 2 #include <iostream>
 3 #include <cstdio>
 4 #include <queue>
 5 #include <cstring>
 6 #include <algorithm>
 7 #define N 10005
 8 
 9 using namespace std;
10 
11 bool vis[N], del[10];//vis表示是否访问过,del表示不能出现的数字
12 int n, m, pre[N];
13 char text[N];//最后输出的数组
14 
15 bool bfs()
16 {
17     queue<int> q;
18     q.push(0);
19     int cur;
20     while(!q.empty())
21     {
22         cur = q.front();
23         q.pop();
24         for(int i = 0; i < 10; i++)
25         {
26             if(del[i]==true||cur==0&&i==0)continue;//不符合要求
27             int mod = (cur*10+i)%n;
28             if(vis[mod])continue;//剪枝
29             text[mod] = '0'+i;
30             vis[mod] = true;
31             pre[mod] = cur;//记录上一个节点
32             q.push(mod);
33             if(mod == 0)return true;
34         }
35     }
36     return false;
37 }
38 
39 void print()//打印路径
40 {
41     string ans;
42     int pos = 0;
43     while(pos!=0 || ans.empty())
44     {
45         ans += text[pos];
46         pos = pre[pos];
47     }
48     reverse(ans.begin(), ans.end());//翻转,输出
49     puts(ans.c_str());
50 }
51 
52 int main()
53 {
54     int kase = 0, x;
55     while(scanf("%d%d", &n, &m)!=EOF)
56     {
57         memset(vis, 0, sizeof(vis));
58         memset(del, 0, sizeof(del));
59         for(int i = 0; i < m; i++)
60         {
61             scanf("%d", &x);
62             del[x] = true;
63         }
64         printf("Case %d: ", ++kase);
65         if(!bfs())printf("-1
");
66         else print();
67     }
68 
69     return 0;
70 }
原文地址:https://www.cnblogs.com/Penn000/p/5852153.html