码迷,mamicode.com
首页 > 其他好文 > 详细

HDU 4474 Yet Another Multiple Problem

时间:2015-09-01 21:34:01      阅读:321      评论:0      收藏:0      [点我收藏+]

标签:

Yet Another Multiple Problem

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

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
 


Source

 解题:余数相同的情况下保存数位值较小的

因为两者同样都有可能成为n的倍数,但是我们要求的正是最小的

技术分享
 1 #include <bits/stdc++.h>
 2 using namespace std;
 3 const int maxn = 10010;
 4 int n,m,pre[maxn],num[maxn];
 5 bool bad[maxn];
 6 void out(int u){
 7     if(pre[u] != -1) out(pre[u]);
 8     printf("%d",num[u]);
 9 }
10 void bfs() {
11     queue<int>q;
12     for(int i = 1; i < 10; ++i) {
13         if(bad[i]) continue;
14         int t = i%n;
15         if(!t) {
16             printf("%d",i);
17             return;
18         }
19         num[t] = i;
20         q.push(t);
21     }
22     while(!q.empty()) {
23         int u = q.front();
24         q.pop();
25         for(int i = 0; i < 10; ++i) {
26             if(bad[i]) continue;
27             int t = (u*10 + i)%n;
28             if(num[t] == -1) {
29                 num[t] = i;
30                 pre[t] = u;
31                 q.push(t);
32             }
33             if(!t) {
34                 out(t);
35                 return;
36             }
37         }
38     }
39     printf("-1");
40 }
41 int main() {
42     int kase = 1;
43     while(~scanf("%d%d",&n,&m)) {
44         memset(pre,-1,sizeof pre);
45         memset(bad,false,sizeof bad);
46         memset(num,-1,sizeof num);
47         while(m--) {
48             int tmp;
49             scanf("%d",&tmp);
50             bad[tmp] = true;
51         }
52         printf("Case %d: ",kase++);
53         bfs();
54         putchar(\n);
55     }
56     return 0;
57 }
View Code

 

HDU 4474 Yet Another Multiple Problem

标签:

原文地址:http://www.cnblogs.com/crackpotisback/p/4776844.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!