hdu 4474 dfs+减枝

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
 
 
题意:0-9(题目中有限制,其中会给出m个数不能使用)这十个数字里面的若干个数字组合出一个数,使这个数是N的倍数,求最小的这个这样的数,不存在的话输出-1。

思路:首先第一想法肯定是基于数位的搜索,但是直接爆搜的话复杂度太高,一直也没找着好的减枝的方法,网上题解给出了一种巧妙的减枝的方法,对于余数相等的两个数a  b来说(a%n==b%n),如果a<b的话,那么我们直接将b舍弃,因为如果存在一个c使得bc%n==0的话,那么肯定的ac%n==0,并且明显此时的ac要比bc小,a是更优的解
#include <iostream>
#include <cstdio>
#include <cstring>
#include <cmath>
#include <queue>
using namespace std;
int a[20];
int num[10005];
int pre[10005];//因为需要将最优解输出,基于数位的运算,我们存前后关系,
//该pre数组存的就是前边存的就是前一位的数
int n;
void print(int k)//递归逆序输出
{
    if(pre[k]!=-1)
    print(pre[k]);
    printf("%d",num[k]);
}
void dfs()
{
   queue<int> que;//里边存的是余数的情况
   for(int i=1;i<=9;i++)
   {
       if(a[i]==0)
       {
           int k=i%n;
           if(k==0)
           {
               printf("%d",i);
               return ;
           }
            que.push(k);
            num[k]=i;//num[i]存的是余数为i的最小数的最后一位
       }
   }
   while(!que.empty())
   {
       int h=que.front();
       que.pop();
       for(int i=0;i<=9;i++)
       {
           if(a[i]==0)
           {
               int k=(h*10+i)%n;
               if(num[k]==-1)
               {
                   que.push(k);
                   pre[k]=h;
                   num[k]=i;
               }
               if(k==0)
               {
                   print(k);
                   return ;
               }
           }
       }
   }
   printf("-1");
}
int main()
{
    int m;
    int p=1;
    while(cin>>n>>m)
    {
        int k;
        memset(a,0,sizeof(a));
        memset(num,-1,sizeof(num));
        memset(pre,-1,sizeof(pre));
        for(int i=1;i<=m;i++)
        {
            scanf("%d",&k);
            a[k]=-1;
        }
        printf("Case %d: ",p++);
        dfs();
        cout<<endl;
    }
    return 0;
}







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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值