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): 3041    Accepted Submission(s): 735


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 ≤ 10 4). 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 ~ 10000) 和 M,后给出 M 个数字,输出 N 的最小倍数,这个数不包含给出的 M 个数字。

 

       思路:

       BFS。枚举 N 的倍数的话无疑会 T,所以从 0 ~ 9 构造数,判断这个数能否出现且维护这个数 % N 的余数,以第一个出现的余数为基准,因为以后出现相同余数的一定会比之前的大,所以第一次出现的余数就放进队列中,知道找到 % N == 0 的为止。下次更新新的数的时候,mod = (mod X 10 + i )% n。注意插入第一个数的时候也要判断 % N 后是不是等于 0。

 

        AC:

#include <cstdio>
#include <cstring>
#include <algorithm>
#include <queue>
#include <string>
#include <iostream>

using namespace std;

typedef struct {
        int mod;
        string num;
} node;

bool vis[15], p[10005];
int num;

bool bfs () {
        queue<node> q;

        for (int i = 1; i <= 9; ++i) {
                if (!vis[i]) {
                        node a;
                        a.mod = i % num;
                        a.num = i + '0';
                        if (!a.mod) {
                                cout << a.num << endl;
                                return true;
                        }
                        q.push(a);
                }
        }

        while (!q.empty()) {
                node now = q.front(); q.pop();

                for (int i = 0; i <= 9; ++i) {
                        node New;

                        if (!vis[i]) {
                                string a;
                                a = i + '0';

                                New.num = now.num + a;
                                New.mod = (now.mod * 10 + i) % num;
                                if (!New.mod) {
                                        cout << New.num << endl;
                                        return true;
                                } else if (!p[New.mod]) {
                                        q.push(New);
                                        p[New.mod] = 1;
                                }
                        }
                }
        }

        return false;
}

int main() {
        int m, t = 0;

        while (~scanf("%d%d", &num, &m)) {
                memset(vis, 0, sizeof(vis));
                memset(p, 0, sizeof(p));

                while (m--) {
                        int ans;
                        scanf("%d", &ans);
                        vis[ans] = 1;
                }

                printf("Case %d: ", ++t);
                if(!bfs()) printf("-1\n");
        }

        return 0;
}

 

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值