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): 2370 Accepted Submission(s):
545
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?
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.
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
Recommend
1 #pragma comment(linker, "/STACK:1024000000,1024000000") 2 #include <map> 3 #include <queue> 4 #include <vector> 5 #include <string> 6 #include <cmath> 7 #include <cstdio> 8 #include <cstring> 9 #include <cstdlib> 10 #include <iostream> 11 #include <algorithm> 12 using namespace std; 13 #define maxn 200005 14 #define ll long long 15 #define INF 0x7fffffff 16 #define eps 1e-8 17 int n, m; 18 queue<int>q; 19 int pre[maxn]; 20 int rear[maxn]; 21 int a[maxn]; 22 void dfs(int u){ 23 if (pre[u] != -1)dfs(pre[u]); 24 printf("%d", rear[u]); 25 } 26 void bfs(){ 27 int u; 28 while (!q.empty())q.pop(); 29 for (int i = 1; i < 10; i++)if (!a[i]){ 30 if ( (i % n == 0)){ printf("%d", i); return; } 31 else if(!pre[i%n]){ pre[i%n] = -1; rear[i%n] = i; q.push(i%n); } 32 } 33 int t = 0; 34 while (!q.empty()&&t<=n){ 35 t++; 36 u = q.front(); q.pop(); 37 for (int i = 0; i < 10; i++)if (!a[i]){ 38 int v; 39 v = (u * 10 + i) % n; 40 if (!pre[v]){ 41 pre[v] = u; rear[v] = i; 42 if (v == 0){ dfs(v); return; } 43 q.push(v); 44 } 45 } 46 } 47 printf("-1"); 48 } 49 int main(){ 50 int cas = 1; 51 int t; 52 while (~scanf("%d%d",&n,&m)){ 53 memset(a, 0, sizeof a); 54 memset(pre, 0, sizeof pre); 55 for (int i = 0; i < m; i++){ scanf("%d", &t); a[t] = 1; } 56 printf("Case %d: ", cas++); 57 bfs(); 58 puts(""); 59 } 60 return 0; 61 }
浙公网安备 33010602011771号