2012ICPC长春站 K Yet Another Multiple Problem 【BFS+同余模定理】
2017-10-04 15:18
465 查看
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
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
#include<iostream> #include<cstdio> #include<cstring> #include<algorithm> #include<iomanip> #include<string> #include<vector> #include<stack> #include<queue> #include<cmath> #include<functional> using namespace std; #define LL long long int #define INF 0x3f3f3f3f const int maxn = 1e5 + 10; const int MOD = 1e9 + 7; struct point{ int m, f; char c; }ans[maxn], temp; int n, m, l, r; bool k[10], h[maxn]; void pout(int x)//递归输出 { if (x == -1) return; pout(ans[x].f); putchar(ans[x].c); } int main() { int tm = 0; int tmp; while (cin >> n >> m) { tm++; memset(h, true, sizeof(h)); memset(k, true, sizeof(k)); l = 0; r = 0; for (int i = 0; i < m; i++) { cin >> tmp; k[tmp] = 0; } int answer = -1; for (int i = 1; i < 10; i++) { if (k[i]) { temp.c = i + '0'; temp.m = i%n; h[temp.m] = 0; temp.f = -1; ans[r++] = temp; if (temp.m == 0) { answer = r - 1; break; } } } while (l < r&&answer == -1) { for (int i = 0; i < 10; i++) { if (k[i]) { temp.c = i + '0'; temp.m = (ans[l].m * 10 + i) % n; if (h[temp.m]) { h[temp.m] = 0; temp.f = l; ans[r++] = temp; if (temp.m == 0) { answer = r - 1; break; } } } } l++; } cout << "Case " << tm << ": "; if (answer == -1) cout << "-1" << endl; else { pout(answer); cout << endl; } } return 0; }
相关文章推荐
- HDU 4474 Yet Another Multiple Problem bfs枚举所有余数
- HDU 4474 Yet Another Multiple Problem 搜索
- hdu 4474 Yet Another Multiple Problem(按位枚举,4级)
- hdu 4474 Yet Another Multiple Problem(BFS)
- HDU 4474 Yet Another Multiple Problem(搜索 BFS)
- 【搜索】 HDOJ 4474 Yet Another Multiple Problem
- HDU 4474(Yet Another Multiple Problem)(BFS+同余定理)
- HDU 4474 Yet Another Multiple Problem ( BFS + 同余剪枝 )
- hdu 4474 Yet Another Multiple Problem 模型转换 BFS搜索
- HDU 4474 HDOJ Yet Another Multiple Problem 2012ACM亚洲赛成都赛区K题
- hdu 4474 Yet Another Multiple Problem
- Yet Another Multiple Problem (hdu 4474)
- HDU4474 Yet Another Multiple Problem
- HDU 4474 Yet Another Multiple Problem【2012成都regional K题】 【BFS+一个判断技巧】
- 【HDU4474 2012成都现场赛K】【搜索-BFS】Yet Another Multiple Problem 最小的要求字符集的数字倍数 余数哈希BFS
- HDU 4474 Yet Another Multiple Problem(搜索剪枝)
- HDU-4471 Yet Another Multiple Problem (BFS+路径还原)
- hdu 4474 Yet Another Multiple Problem 模型转换 BFS搜索 根据剩余类建图广搜
- HDU 4474 Yet Another Multiple Problem (bfs+mod运算)
- HDOJ 4474 Yet Another Multiple Problem