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. MySQL用sql复制表数据到新表的方法

    用sqlyog无法直接复制出一个不同表名的表来,只能copy到其他库上同名的表. 在MySQL数据库中,应该如何用sql将表数据复制到新表中呢? 本人通过试验测试成功了,而且相当简单易懂,速度也非常快 ...

  2. Maven(一)如何用Eclipse创建一个Maven项目

    1.什么是Maven Apache Maven 是一个项目管理和整合工具.基于工程对象模型(POM)的概念,通过一个中央信息管理模块,Maven 能够管理项目的构建.报告和文档. Maven工程结构和 ...

  3. ELK学习笔记之ElasticSearch的索引详解

    0x00 ElasticSearch的索引和MySQL的索引方式对比 Elasticsearch是通过Lucene的倒排索引技术实现比关系型数据库更快的过滤.特别是它对多条件的过滤支持非常好,比如年龄 ...

  4. SNMP学习笔记之Python的netsnmp和pysnmp的性能对比

    0x00 概览 用python获取snmp信息有多个现成的库可以使用,其中比较常用的是netsnmp和pysnmp两个库.网上有较多的关于两个库的例子. 本文重点在于如何并发的获取snmp的数据,即同 ...

  5. Python Web学习笔记之并发编程的孤儿进程与僵尸进程

    1.前言 之前在看<unix环境高级编程>第八章进程时候,提到孤儿进程和僵尸进程,一直对这两个概念比较模糊.今天被人问到什么是孤儿进程和僵尸进程,会带来什么问题,怎么解决,我只停留在概念上 ...

  6. P1661 扩散

    P1661 扩散 二分+最小生成树(kruskal使用并查集) 不清楚的题意导致我被坑了qwq,其实间接联通也是允许的.所以可以使用并查集+最小生成树维护 每次二分答案,然后跑一遍最小生成树判断是否联 ...

  7. java安全体系之JCA、JCE、JAAS、JSSE及其关系

    首先.如果是运行在internet上的系统,并且如果是个涉及到利益性的系统,不可避免的会遭受各种攻击(我们公司的很多系统从OS到DB到webapp就实时有收到攻击和破解),所以尽可能保证安全性将不再是 ...

  8. The equation (扩展欧几里得)题解

    There is an equation ax + by + c = 0. Given a,b,c,x1,x2,y1,y2 you must determine, how many integer r ...

  9. 配置caffe中出现的问题汇总

    1,运行下面代码时: sudo apt-get install libopencv 出错: E: 无法修正错误,因为您要求某些软件包保持现状,就是它们破坏了软件包间的依赖关系 原因: 源(source ...

  10. HDU 6128 Inverse of sum(同余)

    http://acm.hdu.edu.cn/showproblem.php?pid=6128 题意:有一个a数列,并且每个数都小于p,现在要求有多少对$(i,j)$满足$\frac{1}{a_i+a_ ...