解题报告:HDU4474 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): 5472    Accepted Submission(s): 1251


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
 

Statistic |  Submit |  Discuss | Note


题意:
给一个n(n<=1e4),m个小于10的数,问最小的符合要求的n的倍数,要求位上不含有给定的m个数。

这题有一个不常见的利用同余性的剪枝技巧,于是写下来记录一下。。

思路:
正确的做法是从高位到低位进行BFS,利用同余性进行剪枝,即记录出现的过余数,若出现重复,则剪枝,因为先BFS到的数一定是更小的数,这样第一个余数为0的情况就是答案,每个记录上一个状态,回溯到最高位再依次输出即可,或者BFS完所有的余数的情况也没有找到解,输出-1。

代码:
#include<bits/stdc++.h>

using namespace std;

int n,m;
bool used[10];
struct state {
    int last;
    int mod ;
    int f ;
}S[100005];

bool has[10005];


queue<int>Q;

inline void out(int x){
    if(~S[x].f)out(S[x].f);
    printf("%d",S[x].last);
}


int main()
{
    int t = 0;
    while(scanf("%d%d",&n,&m)==2){
        memset(used,false,sizeof(used));
        memset(has,false,sizeof(has));
        for(int i=0,x;i<m;i++){
            scanf("%d",&x);
            used[x] = true;
        }while(!Q.empty())Q.pop();
        int all = 0;int head = -1;
        for(int i=1;i<10;i++){
            if(!used[i]){
                S[all].last = i;
                S[all].mod = i % n;
                S[all].f = -1;
                has[S[all].mod] = true;
                if(S[all].mod==0){
                    head = all;
                    break;
                }
                Q.push(all++);
            }
        }
        while(!Q.empty()&&head==-1){
            int x = Q.front();Q.pop();
            for(int i=0;i<10;i++){
                if(!used[i]){
                    S[all].last = i;
                    S[all].mod = ( S[x].mod * 10 + i ) % n;
                    S[all].f = x;
                    if(!has[S[all].mod]){
                        has[S[all].mod] = true;
                        if(S[all].mod==0){
                            head = all;
                            break;
                        }Q.push(all++);
                    }
                }
            }
        }
        printf("Case %d: ",++t);
        if(head==-1){
            printf("-1\n");
        }else {
            out(head);
            printf("\n");
        }

    }return 0;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值