Dream_Chaser队训练赛第一场 K题

题目来自2012成都区域赛

K - Yet Another Multiple Problem

Time Limit:20000MS     Memory Limit:65536KB     64bit IO Format:%I64d & %I64u

Submit Status Practice HDU 4474

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 ≤ 10 4). 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
0

Sample Output

Case 1: 2345
Case 2: -1
题意:找出不含给定数字的n的倍数。
思路:bfs+剪枝。利用同余模定理,如果A%n==B%n,则A和B只搜一个,就不用重复搜了,因为在A和B后添加尾数后的效果是一样的,比如添加尾数C,AC%n=(A*10+C)%n=(A%n)*(10%n)+C%n, BC%n=(B%n)*(10%n)+C%n,显然相等。所以从小到大不断对余数添加合法的尾数取余,利用余数判重即可,利用pre数组回溯路径来输出每一位。

#include<iostream>
#include<cstdio>
#include<cstring>
#include<cstdlib>
#include<algorithm>
#include<vector>
#include<stack>
#include<queue>
#include<set>
#include<map>
#include<string>
#include<math.h>
#include<cctype>using namespace std;typedef long long ll;
const int maxn=1000100;
const int INF=(1<<29);
const double EPS=0.0000000001;
const double Pi=acos(-1.0);ll n,m;
bool a[20];
int pre[maxn];
int num[maxn];
int tag=1;void bfs()
{memset(pre,-1,sizeof(pre));memset(num,-1,sizeof(num));queue<int> q;for(int i=1;i<=9;i++){if(!a[i]){if(i%n==0){printf("%d\n",i);return;}q.push(i);pre[i%n]=-1;num[i%n]=i;}}while(!q.empty()){int u=q.front();q.pop();if(u==0){stack<int> s;for(int i=u;i!=-1;i=pre[i]){s.push(num[i]);}while(!s.empty()){printf("%d",s.top());s.pop();}printf("\n");return;}for(int i=0;i<=9;i++){if(!a[i]){int v=(u*10+i)%n;if(num[v]==-1){q.push(v);pre[v]=u;num[v]=i;}}}}printf("-1\n");
}int main()
{while(cin>>n>>m){memset(a,0,sizeof(a));while(m--){int t;scanf("%d",&t);a[t]=1;}printf("Case %d: ",tag++);bfs();}return 0;
}

View Code

转载于:https://www.cnblogs.com/--560/p/4543051.html

Dream_Chaser队训练赛第一场 K题相关推荐

  1. Dream_Chaser队训练赛第一场 I题

    Dream_Chaser队训练赛第一场 I题 题目来自2012成都区域赛 I - Count Time Limit:1000MS     Memory Limit:32768KB     64bit ...

  2. 2021年度训练联盟热身训练赛第一场 H题On Average They‘re Purple(BFS)

    题意: 给你一些联通关系,问Bob先选择一些路径(1~n)联通,Alice在路径上染色,Bob的目的是选择一些路径使得染色变化最小,对于Alice来说,需要使得在Bob选择的(1−n1-n1−n)d的 ...

  3. 2021年度热身训练赛第一场

    2021年度热身训练赛第一场 A.Weird Flecks, But OK 题意 给定n个点以及n个点的三维坐标,用一根长度不限的圆柱以垂直x-y平面,x-z平面或y-z平面方式穿过所有点,求圆柱的最 ...

  4. HDU6578 2019HDU多校训练赛第一场 1001 (dp)

    HDU6578 2019HDU多校训练赛第一场 1001 (dp) 传送门:http://acm.hdu.edu.cn/showproblem.php?pid=6578 题意: 你有n个空需要去填,有 ...

  5. 突击蓝桥杯嵌入式(七)——第十三届省赛第一场真题

    突击蓝桥杯嵌入式(七)--第十三届省赛第一场真题 一.题目概览 二.思路梳理 我们直接在LCD例程的基础上,改需增加的外设如下: LED灯(配置锁存器PD2),串口(波特率9600,带中断),按键4个 ...

  6. 突击蓝桥杯嵌入式(十二)——第十二届省赛第一场真题 停车场

    突击蓝桥杯嵌入式(十二)--第十二届省赛第一场真题 停车场 一.题干 二.题目解析 所需: PA7 PWM LED(锁存器) 串口9600 LCD 按键4个,整体难在逻辑,我们先配置好硬件,进入工程 ...

  7. 备战省赛组队训练赛第一场

    目录 问题 A: 篮球队选拔 问题 B: 黑暗意志 问题 C: 调酒壶里的酸奶 问题 D: 过分的谜题 问题 E: 不存在的泳池 问题 F: fps游戏 问题 G: 流连人间的苏苏 问题 H: 路哥从 ...

  8. HDU 2019 Multi-University Training Contest 1 杭电2019多校联合训练赛 第一场 1001 Blank (6578)

    HDU 2019 Multi-University Training Contest 1 杭电2019暑期多校集训第一场 1001 Blank (6578) Problem Description T ...

  9. 中国石油大学 2018-2019赛季多校联合新生训练赛第一场 题解与补题

    这场比赛是在18年12月,因为当时完全不敢用C++,感觉很遥远的样子-代码都是拿C实现的,许多地方其实也可以优化的. 问题 A: 录取分数线 时间限制: 1 Sec 内存限制: 128 MB 题目描述 ...

最新文章

  1. 【硬件基础】振荡(时钟)周期、状态周期、机械周期、指令周期
  2. 零基础学习java软件开发攻略
  3. 十年沉浮,用Python看创业公司消亡史
  4. 浅析制造业物料编码在ERP系统中的实施
  5. IT风险的防控水平是一个“木桶”原理
  6. Tor真的匿名和安全吗?——如果是http数据,则在出口节点容易被嗅探明文流量,这就是根本问题...
  7. 【控制】《多智能体系统的动力学分析与设计》徐光辉老师-第8章-有输入时滞的二阶多智能体系统的多一致
  8. 浪里个浪 FZU - 2261
  9. 数据结构与算法--1.整型变量值互换
  10. 10月31日随笔 成人礼
  11. micropython按键控制流水灯_【MicroPython】LAB01 - 流水灯
  12. excel 案例素材_盘点 | 十分钟进阶Excel数据可视化
  13. win8更改计算机锁屏时间,win8如何设置电脑屏保时间设置方法
  14. 最新Axure谷歌浏览器Chrome扩展程序安装方法
  15. 如何设计高品质LoRa无线模块
  16. 阿里、华为和微软等多家国内外厂商组团搞OpenJDK,Oracle为啥不参加?
  17. 医疗APP有哪些功能
  18. Input框,禁止输入中文
  19. 什么是网关模块 工业物联网以太网网关WiFi模块的选型
  20. 京东怎么做《IOS系统APP耗电量检测分析和优化》?

热门文章

  1. 实现移动端查看控制台
  2. elasticsearch篇之mapping
  3. C#实现监控网络流量
  4. Opus:IETF低延迟音频编解码器:API和操作手册
  5. GBF ASIA亚太电池展
  6. 针对Could not find module libbanded5x.UGR6EUQPIWHQH7SL62IWIXB5545VDNQZ.gfortran-win_amd64.dll报错问题
  7. 微信公众号文字该如何排版?
  8. 在美国的电子工程师眼中的中国电子行业!
  9. Policy Evaluation之Doubly Robust论文讲解
  10. Tomcat开启为什么会秒退