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

YetAnotherMultipleProblem解析
本文介绍了一道关于寻找特定正整数倍数的问题,并提供了一种使用广度优先搜索解决问题的方法。具体而言,给定一个正整数n及一些禁止出现的十进制数字,求最小的n的正倍数,该倍数的十进制表示中不包含这些禁止出现的数字。

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 ≤ 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
 
思路:就是数位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");
  }
}




评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值