hdu 4474 Yet Another Multiple Problem(按位枚举,4级)

来源:互联网 发布:过目不忘 知乎 编辑:程序博客网 时间:2024/05/16 06:39

Yet Another Multiple Problem

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


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 37 8 9100 10
 

Sample Output
Case 1: 2345Case 2: -1
 

Source
2012 Asia Chengdu Regional Contest
 

Recommend
liuyiding
 
思路:就是数位DP常用的思路,按位枚举。只是这换成了广搜。

            模数最多10000 所以最多产生10000个数。


#include<cstdio>#include<cstring>#include<iostream>#define FOR(i,a,b) for(int i=a;i<=b;++i)#define clr(f,z) memset(f,z,sizeof(f))using namespace std;const int mm=5e5+9;class bit{  public:int c,sum,fa;}Q[mm];int l,r;bool ten[11],vis[mm],flag;int n,m;void out(int x){  if(Q[x].fa!=-1)    out(Q[x].fa);  printf("%d",Q[x].c);}void getans(){  int ret;     l=0,r=0;flag=0;    FOR(i,1,9)    if(!ten[i])    {      Q[r].c=i;Q[r].sum=i%n;Q[r].fa=-1;      ret=Q[r].sum;++r;      vis[ ret ]=1;      if(ret==0)      { flag=1;//puts("yes");cout<<i<<endl;        out(r-1);return;      }    }    while(l<r)    {      FOR(i,0,9)      if(!ten[i])      {        Q[r].c=i;Q[r].sum=(Q[l].sum*10+i)%n;Q[r].fa=l;        ret=Q[r].sum;        if(ret==0){out(r);flag=1;return;}        if(!vis[ ret ]){++r;vis[ret]=1;}      }      ++l;    }}int main(){ int a,cas=0;  while(~scanf("%d%d",&n,&m))  { clr(ten,0);    clr(vis,0);    printf("Case %d: ",++cas);    FOR(i,1,m)    {      scanf("%d",&a);      ten[a]=1;    }    getans();    if(flag)puts("");    else puts("-1");  }}