Yet Another Multiple Problem

Time Limit: 40000/20000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)
Total Submission(s): 5496    Accepted Submission(s): 1257

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
0

Sample Output

Case 1: 2345
Case 2: -1
 
题意:
求n的最小倍数x,不包含m个特定的数字。
思路:
按数字位进行搜索,状态数最多只有10000种。
 //2016.9.5
#include <iostream>
#include <cstdio>
#include <queue>
#include <cstring>
#include <algorithm>
#define N 10005 using namespace std; bool vis[N], del[];//vis表示是否访问过,del表示不能出现的数字
int n, m, pre[N];
char text[N];//最后输出的数组 bool bfs()
{
queue<int> q;
q.push();
int cur;
while(!q.empty())
{
cur = q.front();
q.pop();
for(int i = ; i < ; i++)
{
if(del[i]==true||cur==&&i==)continue;//不符合要求
int mod = (cur*+i)%n;
if(vis[mod])continue;//剪枝
text[mod] = ''+i;
vis[mod] = true;
pre[mod] = cur;//记录上一个节点
q.push(mod);
if(mod == )return true;
}
}
return false;
} void print()//打印路径
{
string ans;
int pos = ;
while(pos!= || ans.empty())
{
ans += text[pos];
pos = pre[pos];
}
reverse(ans.begin(), ans.end());//翻转,输出
puts(ans.c_str());
} int main()
{
int kase = , x;
while(scanf("%d%d", &n, &m)!=EOF)
{
memset(vis, , sizeof(vis));
memset(del, , sizeof(del));
for(int i = ; i < m; i++)
{
scanf("%d", &x);
del[x] = true;
}
printf("Case %d: ", ++kase);
if(!bfs())printf("-1\n");
else print();
} return ;
}

HDU4474的更多相关文章

  1. hdu4474 Yet Another Multiple Problem

    Yet Another Multiple Problem Description There are tons of problems about integer multiples. Despite ...

随机推荐

  1. Java中的数组越界问题

    Java中数组初始化和OC其实是一样的,分为动态初始化和静态初始化, 动态初始化:指定长度,由系统给出初始化值 静态初始化:给出初始化值,由系统给出长度 在我们使用数组时最容易出现的就是数组越界问题, ...

  2. ffmpeg ffplay ffprobe资料整理

    1. 官网地址:https://ffmpeg.org/ 官网文档地址:https://ffmpeg.org/documentation.html 官网下载地址:https://ffmpeg.org/d ...

  3. acm--统计错误数

    题目描述 题目地址:http://www.nowcoder.com/practice/67df1d7889cf4c529576383c2e647c48?tpId=49&tqId=29276&a ...

  4. MOS管常识

    http://anlx27.iteye.com/blog/1583089 学过模拟电路,但都忘得差不多了.重新学习MOS管相关知识,大多数是整理得来并非原创.如有错误还请多多指点! 先上一张图 一. ...

  5. Ubuntu上CUDA和CUDNN的安装

    Ubuntu上CUDA的安装 下载路径: CUDA下载 `sudo dpkg -i cuda-repo-ubuntu1504-7-5-local_7.5-18_amd64.deb` `sudo apt ...

  6. Tyvj P3276

    题目链接:http://www.tyvj.cn/p/3276 这题是一个动归题,一直没有想出动归的做法,后来求教别人之后写了一个记忆化搜索,只有出题者又给我提供了DP的解法,下面我来写写DP的写法 设 ...

  7. STM32硬件IIC操作

    Stm32具有IIC接口,接口有以下主要特性 多主机功能:该模块既可做主设备也可做从设备 主设备功能 C地址检测 位/10位地址和广播呼叫 支持不同的通讯速度 状态标志: 发送器/接收器模式标志 字节 ...

  8. md5-linux_shell

    linux shell提供md5sum用于计算校验MD5值. md5sum - compute and check MD5 message digest md5sum [OPTION]... [FIL ...

  9. 创建sqlserver函数

    CREATE FUNCTION getsort (@first varchar(100),@second varchar(100))RETURNS  intASBEGIN declare @rv in ...

  10. 如何备份、还原或迁移 WhatsApp 的信息和资料?

    WhatsApp 会在每天清晨 2 点钟自动创建本地备份文件并将备份文件存储在手机系统设置的文件夹中. Google 云端硬盘 您可以备份对话记录和媒体到 Google 云端硬盘以便日后更换手机时仍可 ...