2012ICPC长春站 K Yet Another Multiple Problem 【BFS+同余模定理】

来源:互联网 发布:淘宝网刷销量 编辑:程序博客网 时间:2024/06/05 18:11

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

#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 0x3f3f3f3fconst 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;}
阅读全文
0 0