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. bzoj1056/1862 [Zjoi2006]GameZ游戏排名系统

    题目链接:1,2 treap恶心题,不多说 #include<algorithm> #include<iostream> #include<cstdlib> #in ...

  2. 我用了7年时间成长为阿里Java架构师,你呢?(附学习路线图)

      前言:我用了七年的时间,一步一步走到了现在,中途也有了解过其他的技术,也想过要转其他的语言,但是最后还是坚持下来走Java这条路,希望我的经历可以帮助到后来的人,要是觉得对你有帮助的话,可以点赞关 ...

  3. 05: MySQLdb 原生SQL语句操作数据库

    1.1 MySQLdb安装与简介 1.MySQLdb 模块的安装(python3中目前这个模块还不可用)参考博客 1. linux: yum install MySQL-python 2. windo ...

  4. 20145118 《Java程序设计》 实验报告二

    实验二 Java面向对象程序设计 实验内容 初步掌握单元测试和TDD 理解并掌握面向对象三要素:封装.继承.多态 初步掌握UML建模 熟悉S.O.L.I.D原则 了解设计模式 实验要求 1.没有Lin ...

  5. IPMB接口协议总结

    IPMB接口协议总结 IPMB,智能平台管理总线, 是ATCA(Advanced Telecom Computing Architecture)先进的电信计算平台的各FRU背板通讯的两组冗余I2C总线 ...

  6. stl string 使用(转载)

    出处:http://www.cnblogs.com/lzjsky/archive/2011/01/23/1942508.html 1. 查找字符 std::wstring strData = L&qu ...

  7. myeclipse中文名字项目运行报错

    今天由于项目进行分支,负责开发迭代的功能对应不同的分支项目,没想到的是分支项目名称加上了功能的中文名字.要使用了resin发布项目的时候,报了 java.io.CharConversionExcept ...

  8. git分支 远程协作

    创建文件mkdir ### cd ### git init 初始化 git config global user.name “username” git config global user.emia ...

  9. Linux环境下一些有用但不常见的命令

    Linux环境下一些有用但不常见的命令 1.获取显卡硬件信息 lspci -vnn | grep VGA -A 12 (若是n卡,则用glxinfo) 2.执行*.sh文件 常见的执行*.sh文件当属 ...

  10. ACM-ICPC 2018 徐州赛区网络预赛 J. Maze Designer 最大生成树 lca

    大概就是要每两个点 只能有一条路径,并且约束,最短的边用来砌墙,那么反之的意思就是最大的边用来穿过 故最大生成树 生成以后 再用lca计算树上两点间的距离 (当然防止生成树是一条链,可以用树的重心作为 ...