Yet Another Multiple Problem HDU 4474 BFS

Yet Another Multiple Problem

Time Limit: 40000/20000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)
Total Submission(s): 2004    Accepted Submission(s): 482


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
 

 

Recommend
liuyiding
 
#include<cstdio>
#include<cstring>
#include<cstdlib>
#include<cmath>
#include<algorithm>
#include<string>
#include<map>
#include<stack>
#include<set>
#include<iostream>
#include<vector>
#include<queue>
//ios_base::sync_with_stdio(false);
//#pragma comment(linker, "/STACK:1024000000,1024000000")

using namespace std;
#define sz(v) ((int)(v).size())
#define rep(i, a, b) for (int i = (a); i < (b); ++i)
#define repf(i, a, b) for (int i = (a); i <= (b); ++i)
#define repd(i, a, b) for (int i = (a); i >= (b); --i)
#define clr(x) memset(x,0,sizeof(x))
#define clrs( x , y ) memset(x,y,sizeof(x))
#define out(x) printf(#x" %d\n", x)
#define sqr(x) ((x) * (x))
typedef long long LL;

const int INF = 1000000000;
const double eps = 1e-8;
const int maxn = 30000;

int sgn(const double &x) {  return (x > eps) - (x < -eps); }

int n,m;
int cant[maxn];
int vis[maxn];
struct Node
{
    int mod;
    int pre;
    int val;
    int id;
}node[maxn];

void print_ans(int i)
{
    if(node[i].pre != -1)
        print_ans(node[i].pre);
    //cout<<i<<endl;
    printf("%d",node[i].val);
}
void bfs()
{
    clr(vis);
    queue<Node> q;
    int cnt = 0;
    rep(i,1,10)
    {
        if(!cant[i] && !vis[i%n])
        {
            vis[i%n] = 1;
            node[cnt].mod = i%n;
            node[cnt].pre = -1; 
            node[cnt].val = i;
            node[cnt].id = cnt;
            q.push(node[cnt]);
            cnt++;
        }
    }
    while(!q.empty())
    {
        Node temp = q.front();
        q.pop();
        if(temp.mod == 0)
        {
            print_ans(temp.id);
            return ;
        }
        
        int ret;
        rep(i,0,10)
        {
            if(cant[i])
                continue;
            ret  = temp.mod*10 + i;
            int a = ret%n;
            if(!vis[a])
            {
                vis[a] = 1;
                Node t;
                t.mod = a;
                t.val = i;
                t.pre = temp.id;
                t.id = cnt;
                node[cnt] = t;
                cnt++;
                q.push(t);
            }
        }
    }
    printf("-1");
}
int main() 
{
    //freopen("in.txt","r",stdin);
    int k = 1;
    while(scanf("%d%d",&n,&m) == 2)
    {
        clr(cant);
        rep(i,0,m)
        {
            int a;
            scanf("%d",&a);
            cant[a] = 1;
        }
        printf("Case %d: ",k++);
        bfs();
        cout<<endl;
    }
    return 0;
}

 

转载于:https://www.cnblogs.com/DreamHighWithMe/p/3367003.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值