HDU 4474 Yet Another Multiple Problem

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

2012 Asia Chengdu Regional Contest

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

因为两者同样都有可能成为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 }

时间: 2025-01-02 19:34:51

HDU 4474 Yet Another Multiple Problem的相关文章

HDU 4474 Yet Another Multiple Problem【2012成都regional K题】 【BFS+一个判断技巧】

Yet Another Multiple Problem Time Limit: 40000/20000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others) Total Submission(s): 3407    Accepted Submission(s): 825 Problem Description There are tons of problems about integer multiples. Despit

【暴力+BFS】HDU 4474 Yet Another Multiple Problem

通道:http://acm.hdu.edu.cn/showproblem.php?pid=4474 题意:给出n和m个数位,求一个数X,这个数是n的最小倍数且他的每一位都不含有m个数位中的任意一个. 思路:反过来想,其实就是有非M的元素组成一个数,这个数是N的倍数.如果存在解,那么他的第一位便是非M组合中的任意一位,然后除N后,他的余数便是X-a*n,那么下一位除N的就是(X-a*n)*10+(枚举的下一个非M元素),要求最小这个数,那么BFS跑最短就可以了. 代码:https://github

[bfs+余数判重+路径记录] hdu 4474 Yet Another Multiple Problem

题意: 给一个n和m个数字(一位数) 求最小的n的倍数不含有这m个数字,不存在输出-1 思路: 首先有可能这个数超long long 所以无法暴力解决 所以这题应该是一个bfs 为什么能用余数判重呢 对于当前的余数进到队列里,一定是这个余数对应数的最小值 接下来再怎么添加到满足条件的后续东西应该是一样的 所以就可以余数判重了,类似数位dp的记录方式 然后再加上一个路径记录就好了 代码: #include"cstdlib" #include"cstdio" #incl

HDOJ 4474 Yet Another Multiple Problem

BFS..... Yet Another Multiple Problem Time Limit: 40000/20000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others) Total Submission(s): 3307    Accepted Submission(s): 806 Problem Description There are tons of problems about integer multiple

HDU 4474(神奇的BFS+强剪枝)

 HDU - 4474 Yet Another Multiple Problem Time Limit: 20000MS   Memory Limit: 65536KB   64bit IO Format: %I64d & %I64u Description There are tons of problems about integer multiples. Despite the fact that the topic is not original, the content is hi

2012Chhengdu K - Yet Another Multiple Problem

K - Yet Another Multiple Problem Time Limit:20000MS     Memory Limit:65536KB     64bit IO Format:%I64d & %I64u Submit Status Practice HDU 4474 Appoint description:  System Crawler  (2014-10-16) Description There are tons of problems about integer mul

hdu 5349 MZL&#39;s simple problem

Problem Description A simple problem Problem Description You have a multiple set,and now there are three kinds of operations: 1 x : add number x to set 2 : delete the minimum number (if the set is empty now,then ignore it) 3 : query the maximum numbe

HDU 1019 Least Common Multiple (最小公倍数)

Least Common Multiple Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 30285    Accepted Submission(s): 11455 Problem Description The least common multiple (LCM) of a set of positive integers is

hdu - 5349 MZL&#39;s simple problem(解题报告)

A - MZL's simple problem Time Limit:1500MS     Memory Limit:65536KB     64bit IO Format:%I64d & %I64u Submit Status Description A simple problem Problem Description You have a multiple set,and now there are three kinds of operations: 1 x : add number