zoukankan      html  css  js  c++  java
  • HDU-4471 Yet Another Multiple Problem (BFS+路径还原)

    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 3
    7 8 9
    100 1
     
    Sample Output
    Case 1: 2345
    Case 2: -1
     
     题目大意:要求不用给出的m个数字,组成n的最小倍数。
     题目分析:本题看似是简单的BFS,实际上按常规的BFS写会WA。因为答案有可能超过64位,应该按照路径来写。以模n作为状态,不光要记录到达当前状态的前一状态,还要记录在当前状态最后一个加进来的数字。这道题的坑就在这儿。
     
     
    代码如下:
     1 # include<iostream>
     2 # include<cstdio>
     3 # include<queue>
     4 # include<cstring>
     5 # include<algorithm>
     6 using namespace std;
     7 # define ull unsigned long long
     8 const int N=10005;
     9 int mark[10],pre[N],lst[N];
    10 void print(int id)
    11 {
    12     if(pre[id]!=-1)
    13         print(pre[id]);
    14     printf("%d",lst[id]);
    15 }
    16 void bfs(int n)
    17 {
    18     queue<int>q;
    19     memset(lst,-1,sizeof(lst));
    20     memset(pre,-1,sizeof(pre));
    21     for(int i=1;i<10;++i){
    22         if(!mark[i]){
    23             if(i%n==0){
    24                 printf("%d
    ",i);
    25                 return ;
    26             }
    27             lst[i%n]=i;
    28             q.push(i%n);
    29         }
    30     }
    31     while(!q.empty())
    32     {
    33         int u=q.front();
    34         q.pop();
    35         for(int i=0;i<10;++i){
    36             if(!mark[i]){
    37                 int nxt=(u*10+i)%n;
    38                 if(lst[nxt]==-1){
    39                     lst[nxt]=i;
    40                     pre[nxt]=u;
    41                     q.push(nxt);
    42                 }
    43                 if(nxt==0){
    44                     print(nxt);
    45                     printf("
    ");
    46                     return ;
    47                 }
    48             }
    49         }
    50     }
    51     printf("-1
    ");
    52 }
    53 int main()
    54 {
    55     int n,m,cas=0;
    56     while(scanf("%d%d",&n,&m)!=EOF)
    57     {
    58         int num;
    59         memset(mark,0,sizeof(mark));
    60         while(m--)
    61         {
    62             scanf("%d",&num);
    63             mark[num]=1;
    64         }
    65         printf("Case %d: ",++cas);
    66         bfs(n);
    67     }
    68     return 0;
    69 }
  • 相关阅读:
    烧写NAND Flash时出现错误:*** Warning bad CRC or NAND, using default environment
    在ubuntu下如何验证文件的MD5码
    条件编译#ifdef MACRO_A和#if defined(MACRO_A)的区别
    用nmap获取ip和mac地址
    rcS中启动udevd
    ubuntu下minicom不能接受键盘输入
    ios audioqueue 流播放接口
    ffmpeg 0.8.11 VC编译的SDK已经发布
    lua 字符串数学表达式运算
    ffmpeg 0.8.11 VC编译的SDK已经发布
  • 原文地址:https://www.cnblogs.com/20143605--pcx/p/4727861.html
Copyright © 2011-2022 走看看