hdu 4474 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): 4947    Accepted Submission(s): 1082


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   |   We have carefully selected several similar problems for you:   5508  5507  5506  5505  5504 

#include<cstdio>
#include<string>
#include<cstring>
#include<iostream>
#include<cmath>
#include<algorithm>
#include<climits>
#include<queue>
#include<vector>
#include<map>
#include<sstream>
#include<set>
#include<stack>
#include<cctype>
#include<utility>
#pragma comment(linker, "/STACK:102400000,102400000")
#define PI 3.1415926535897932384626
#define eps 1e-10
#define sqr(x) ((x)*(x))
#define FOR0(i,n)  for(int i=0 ;i<(n) ;i++)
#define FOR1(i,n)  for(int i=1 ;i<=(n) ;i++)
#define FORD(i,n)  for(int i=(n) ;i>=0 ;i--)
#define  lson   num<<1,le,mid
#define rson    num<<1|1,mid+1,ri
#define MID   int mid=(le+ri)>>1
#define zero(x)((x>0? x:-x)<1e-15)
#define mk    make_pair
#define _f     first
#define _s     second
using namespace std;
//const int INF=    ;
typedef long long ll;
//const ll inf =1000000000000000;//1e15;
//ifstream fin("input.txt");
//ofstream fout("output.txt");
//fin.close();
//fout.close();
//freopen("a.in","r",stdin);
//freopen("a.out","w",stdout);
const int INF =0x3f3f3f3f;
//const int maxn=    ;
//const int maxm=    ;
 
 
 ll n,m;
 bool use[12];
 int num;
 
 int a[12];
 bool vis[10000+5];
 struct Node
 {
     ll yu;
     int pre;
     ll thi;
     Node(){}
     Node(ll yu,int pre,ll thi ):yu(yu),pre(pre),thi(thi){}
 } q[1000000];
 
 
 void output(int x)
 {
     if(x==-1) return;
 
     output(q[x].pre);
 
     printf("%lld",q[x].thi);
 
 }
bool  BFS()
 {
     int r=0,f=0;
     memset(vis,0,sizeof vis);
     for(int i=1;i<=num;i++)
     {
         ll x=a[i];
         if(x==0)  continue;
         if(x%n==0)  {printf("%lld\n",x);return true;}
         q[r++]= Node(x%n,-1,x );
         vis[x%n]=1;
     }
     while(f<r)
     {
//         Node now=q.front();q.pop();
       Node now=q[f++];
         ll tmp=now.yu;
         ll t;
         for(int i=1;i<=num;i++)
         {
             ll x=a[i];
             ll yu=(tmp*10+x)%n;
 
             if(vis[yu])  continue;
             q[r++]=Node(yu,f-1,x);
             if(yu==0)  {output(r-1);putchar('\n');return true;}
             vis[yu]=1;
 
         }
 
     }
     return false;
 
 }
int main()
{
    int x,kase=0;
    while(~scanf("%lld%d",&n,&m))
    {
        memset(use,0,sizeof use);
        for(int i=1;i<=m;i++)
        {
            scanf("%d",&x);
            use[x]=1;
        }
        num=0;
        for(int i=0;i<=9;i++)
        {
            if(use[i])  continue;
            num++;
            a[num]=i;
        }
 
        printf("Case %d: ",++kase);
        if(!BFS())  puts("-1");
 
 
    }
 
 
    return 0;
}


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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值