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. Secure CRT 自动记录日志log配置

    SecureCRT8.0的下载地址下载地址: 链接:https://pan.baidu.com/s/1i5q09qH 密码:4pa2 配置自动log操作如下: 1.options ---> Se ...

  2. 安装Hue后的一些功能的问题解决干货总结(博主推荐)

    不多说,直接上干货! 我的集群机器情况是 bigdatamaster(192.168.80.10).bigdataslave1(192.168.80.11)和bigdataslave2(192.168 ...

  3. corejDay1

    1.内部类: 有什么用? 1.可以访问该类定义所在作用域中的数据,包括私有数据. 2.当想定义一个回调函数而不想编写大量代码时,使用匿名内部类比较便捷. 3.内部类可以对同一个包中的其他类隐藏起来. ...

  4. ELK学习笔记之ElasticSearch简介

    0x00 什么是Elasticsearch Elasticsearch (ES)是一个基于 Lucene 的开源搜索引擎,它不但稳定.可靠.快速,而且也具有良好的水平扩展能力,是专门为分布式环境设计的 ...

  5. uva 1658 Admiral - 费用流

    vjudge传送门[here] 题目大意:给一个有(3≤v≤1000)个点e(3≤e≤10000)条边的有向加权图,求1~v的两条不相交(除了起点和终点外没有公共点)的路径,使权值和最小. 正解是吧2 ...

  6. ubuntu查询某个库的相关情况

    环境:Ubuntu 14.04 64bit 1.如:查询libjpeg库的位置 ldconfig -p |grep libjpeg 2.如:查询libjpeg库的相关名称 dpkg -l '*jpeg ...

  7. HDU 4734 (数位DP)题解

    思路: dp[pos][pre]代表长度为pos的不大于pre的个数 #include<iostream> #include<cstdio> #include<cstri ...

  8. vim 操作快捷键 待更~

    shift + g 文件尾 ------ gdb p print start 从main函数开始 n next换行 s step 进入函数

  9. 提高Intellij创建Maven工程的速度

    按照默认的方式创建Maven工程的时候会发现Maven插件加载的很慢如下 解决方法:在创建的过程中,在Properties中添加一个参数archetypeCatalog=internal . 因为ar ...

  10. python input输入元素相加

    sum= number= while True: : break number=int(input('数字0为结束程序,请输入数字: ')) sum+=number print('目前累加的结果为: ...