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 }
  • 相关阅读:
    SharePoint每日小贴士Web部件
    韦东山设备树课程-环境搭建【学习笔记】
    韦东山视频第3课第2节_JNI_C调用JAVA_P【学习笔记】
    韦东山视频第3课第1节_JNI_P【学习笔记】
    高通qxdm抓取sensor的log【学习笔记】
    sensor【学习笔记】
    linux驱动由浅入深系列:高通sensor架构实例分析之二(驱动代码结构)【转】
    linux驱动由浅入深系列:高通sensor架构实例分析之三(adsp上报数据详解、校准流程详解)【转】
    Android Sensor 架构深入剖析【转】
    Android Sensor详解(1)简介与架构【转】
  • 原文地址:https://www.cnblogs.com/20143605--pcx/p/4727861.html
Copyright © 2011-2022 走看看