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作为状态,不光要记录到达当前状态的前一状态,还要记录在当前状态最后一个加进来的数字。这道题的坑就在这儿。
 
 
代码如下:
 # include<iostream>
# include<cstdio>
# include<queue>
# include<cstring>
# include<algorithm>
using namespace std;
# define ull unsigned long long
const int N=;
int mark[],pre[N],lst[N];
void print(int id)
{
if(pre[id]!=-)
print(pre[id]);
printf("%d",lst[id]);
}
void bfs(int n)
{
queue<int>q;
memset(lst,-,sizeof(lst));
memset(pre,-,sizeof(pre));
for(int i=;i<;++i){
if(!mark[i]){
if(i%n==){
printf("%d\n",i);
return ;
}
lst[i%n]=i;
q.push(i%n);
}
}
while(!q.empty())
{
int u=q.front();
q.pop();
for(int i=;i<;++i){
if(!mark[i]){
int nxt=(u*+i)%n;
if(lst[nxt]==-){
lst[nxt]=i;
pre[nxt]=u;
q.push(nxt);
}
if(nxt==){
print(nxt);
printf("\n");
return ;
}
}
}
}
printf("-1\n");
}
int main()
{
int n,m,cas=;
while(scanf("%d%d",&n,&m)!=EOF)
{
int num;
memset(mark,,sizeof(mark));
while(m--)
{
scanf("%d",&num);
mark[num]=;
}
printf("Case %d: ",++cas);
bfs(n);
}
return ;
}

HDU-4471 Yet Another Multiple Problem (BFS+路径还原)的更多相关文章

  1. HDU 4474 Yet Another Multiple Problem BFS

    题意:求m的倍数中不包含一些数码的最小倍数数码是多少.比如15 ,不包含0  1 3,答案是45. BFS过程:用b[]记录可用的数码.设一棵树,树根为-1.树根的孩子是所有可用的数码,孩子的孩子也是 ...

  2. HDU 4474 Yet Another Multiple Problem ( BFS + 同余剪枝 )

    没什么巧办法,直接搜就行. 用余数作为每个节点的哈希值. #include <cstdio> #include <cstring> #include <cstdlib&g ...

  3. POJ-3894 迷宫问题 (BFS+路径还原)

    定义一个二维数组: int maze[5][5] = { 0, 1, 0, 0, 0, 0, 1, 0, 1, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 0, 0, 0, 0, 1, ...

  4. HDU 4474 Yet Another Multiple Problem【2012成都regional K题】 【BFS+一个判断技巧】

    Yet Another Multiple Problem Time Limit: 40000/20000 MS (Java/Others)    Memory Limit: 65536/65536 K ...

  5. Yet Another Multiple Problem(bfs好题)

    Yet Another Multiple Problem Time Limit : 40000/20000ms (Java/Other)   Memory Limit : 65536/65536K ( ...

  6. HDU - 1160 FatMouse's Speed 动态规划LIS,路径还原与nlogn优化

    HDU - 1160 给一些老鼠的体重和速度 要求对老鼠进行重排列,并找出一个最长的子序列,体重严格递增,速度严格递减 并输出一种方案 原题等于定义一个偏序关系 $(a,b)<(c.d)$ 当且 ...

  7. Uva 816 Abbott的复仇(三元组BFS + 路径还原)

    题意: 有一个最多9*9个点的迷宫, 给定起点坐标(r0,c0)和终点坐标(rf,cf), 求出最短路径并输出. 分析: 因为多了朝向这个元素, 所以我们bfs的队列元素就是一个三元组(r,c,dir ...

  8. HDU 3861 The King’s Problem 最小路径覆盖(强连通分量缩点+二分图最大匹配)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=3861 最小路径覆盖的一篇博客:https://blog.csdn.net/qq_39627843/ar ...

  9. hdu 4474 Yet Another Multiple Problem

    题意: 找到一个n的倍数,这个数不能含有m个后续数字中的任何一个 题解: #include<stdio.h> #include<string.h> #include<qu ...

随机推荐

  1. python之路----模块调用

    如何使用模块? 1 import 示例文件:自定义模块my_module.py,文件名my_module.py,模块名my_module #my_module.py print('from the m ...

  2. Finalize和Dispose的区别

    https://www.cnblogs.com/Jessy/articles/2552839.html

  3. mp4v2 基本知识

    mp4v2 和mp4的一些基础知识 由于项目需要做mp4文件的合成(264+aac)和mp4文件的解析: MP4文件本身就是一个容器,对于视频来说就是把不同的内容放按照mp4的规则存放而已: 如果完全 ...

  4. mysql的级联复制和多源复制

    MySQL的复制:https://www.cnblogs.com/wxzhe/p/10051114.html 级联复制的结构如图 我们来设置基于filename和pos的级联复制,并且接受mysql- ...

  5. 记一次ping: unknown host错误

    虚拟机上一台主机,之前一直在用,可以通过xshell连接,但是忽然发现ping百度失败了! [root@mgt02 ~]# ping www.baidu.com ping: unknown host ...

  6. 在ubuntu英文系统下使用中文输入法

    How to install and use Chinese Input Method in the English Locale in Ubuntu ?(1) Check if there exis ...

  7. 20145109竺文君、20145106石晟荣 java实验三

    重构的概念:重构(Refactoring)就是通过调整程序代码改善软件的质量.性能,使其程序的设计模式和架构更趋合理,提高软件的扩展性和维护性. 首先由zwj在<git.oschina.net& ...

  8. CmsEasy 5.5 cut_image 代码执行漏洞

    3 CmsEasy 5.5 cut_image 代码执行漏洞 3.1 摘要 3.1.1 漏洞介绍 CmsEasy是一款基于PHP+MySQL架构的网站内容管理系统,可面向大中型站点提供重量级网站建设解 ...

  9. 使用volley来json解析

    我对网络请求get和post的理解: 1.get只是从某网址获得固定数据,如我访问百度,返回就是百度的html语句: 2.post是我在访问的时候加了某些参数,如我访问某个服务器,访问的时候加了一些语 ...

  10. 面向对象之(非)绑定方法,反射,isinstance与issubclass

    isinstance(obj,cls)和issubclass(sub,super) isinstance(obj,cls)检查obj是否是类 cls 的对象 class Foo(object): pa ...