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. ural1126 Magnetic Storms

    Magnetic Storms Time limit: 0.5 secondMemory limit: 64 MB The directory of our kindergarten decided ...

  2. Swift中自定义打印方法

    // 1.获取打印所在的文件 let file = ( #file as NSString).lastPathComponent // 2.获取打印所在的方法 let funcName = #func ...

  3. sql语句优化之not in

    多表关联想查a表中除去b表的可用not exists 效率比not in 更高 优化后的语句用时0.421秒 select john.*, (case when round((case john.su ...

  4. php 注意点

    1.如果一个方法可静态化,就对它做静态声明.速率可提升至4倍. 2.echo 比 print 快. 3.使用echo的多重参数(译注:指用逗号而不是句点)代替字符串连接. 4.在执行for循环之前确定 ...

  5. ckeditor_3.6.6.2+CKFinder2.0.2配置

    一.首先工具的下载,找到相应的版本进行下载     ckeditor_3.6.6.2+CKFinder2.0.2 http://ckeditor.com/download      打开war文件,然 ...

  6. HDU 5677 ztr loves substring

    Manacher+二维费用多重背包 二进制优化 这题是一眼标算....先计算出每个长度的回文串有几种,然后用二维费用的多重背包判断是否有解. 多重背包做的时候需要二进制优化. #include< ...

  7. MyEclipse构建Web Service(Xfire框架)

    以下是本人原创,如若转载和使用请注明转载地址.本博客信息切勿用于商业,可以个人使用,若喜欢我的博客,请关注我,谢谢!博客地址 任务要求: 使用Xfire实现一个简单的CalculatorWebServ ...

  8. CodeForces 626C Block Towers

    构造AC的.左右两边都先不用6的倍数,然后哪边数字大那一边往回退一下,然后再比较哪边数字大.......直到结束 #include<cstdio> #include<cstring& ...

  9. csv和excel的区别

    excel 文件只能通过excel打开,里面包含公式或者计算. csv文件是一种通用数据格式,可以用很多方式打开,比如excel.csv 以分割数据,用行分割符号分割行级数据,直接上个例子一目了然. ...

  10. mysql----ERROR 1040 (HY000): Too many connections

    http://gwokae.mewggle.com/wordpress/index.php/archives/683 查看最大链接 mysql -h alg-db14 -u bfdroot -pqia ...