Yet Another Multiple Problem

Time Limit : 40000/20000ms (Java/Other)   Memory Limit : 65536/65536K (Java/Other)
Total Submission(s) : 2   Accepted Submission(s) : 1
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 ≤ 10[sup]4[/sup]). 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,让找出不含接下来m个数字能组成的n的倍数的最小解;参考了大神的写法;
/***************/

按照数的位数BFS,从小向大枚举就可以保证构造出来的数是递增的,如果不加判断就直接搜索的话,复杂度非常高。因此需要剪枝。

优化方法:如果一个数%N==0,那么这个数就是N的倍数。在没有找到的前提下,如果A%N==B%N,而且A<B,那么其实我们就可以取A而不取B,因为如果在A末尾增加C可以使得AC%N==0,那么BC%N也等于0,易得:如果A和B追加数之后%N==0,那么最优条件下追加的数肯定相同。

因此我们只需要维护组合出来的数%N的值即可,如果在搜索的途中出现了相同的%N值,就可以直接忽略了,因为肯定没有前面的优秀。

/***************/

代码:
 #include<stdio.h>
#include<string>
#include<queue>
#include<string.h>
#include<algorithm>
#define mem(a) memset(a,0,sizeof(a))
using namespace std;
const int MAXN=1e4+;
int vis[MAXN],del[MAXN],pre[MAXN];
char al[MAXN];
int n;
void print_ans(){
int r=;
string ans;
while(ans.empty()||r!=){
ans+=al[r];
r=pre[r];//由于anser的下属是0,刚开始的数字的上司又是0,所以最后找到0结束循环;
}
reverse(ans.begin(),ans.end());
puts(ans.c_str());
}
bool bfs(){
queue<int>dl;
dl.push();
while(!dl.empty()){
int f1,f2;
f1=dl.front();//**
dl.pop();
for(int i=;i<=;i++){
if(del[i]||i==&&f1==)continue;
f2=(f1*+i)%n;
if(vis[f2])continue;//应该放上面
pre[f2]=f1;//**
al[f2]=i+'';
if(f2==){
print_ans();
return true;
}
vis[f2]=;
dl.push(f2);
}
}
puts("-1");
return false;
}
int main(){
int m,flot=;
while(~scanf("%d%d",&n,&m)){
mem(vis);mem(del);mem(pre);mem(al);
while(m--){
int temp;
scanf("%d",&temp);
del[temp]=true;
}
printf("Case %d: ",++flot);
bfs();
}
return ;
}

第二种方法wa:

 #include<stdio.h>
#include<string.h>
#include<queue>
#include<algorithm>
using namespace std;
const int MAXN=1e4+;
int num[];
struct Node{
int s[];
int len;
};
int M,C,N;
int vis[MAXN];
int mod(Node a){
int x=;
for(int i=;i<a.len;i++){
x=(x*C+a.s[i])%N;
}
return x;
}
void print_ans(Node a){
for(int i=;i<a.len;i++){
printf("%d",a.s[i]);
}
puts("");
}
void bfs(){
memset(vis,,sizeof(vis));
queue<Node>dl;
Node a;
a.len=;
int md;
for(int i=;i<M;i++){
a.s[]=num[i];
md=mod(a);
if(vis[md]||num[i]==)continue;
if(md==&&num[i]){
printf("%d\n",num[i]);
return;
}
vis[md]=;
dl.push(a);
}
while(!dl.empty()){
a=dl.front();
dl.pop();
for(int i=;i<M;i++){
if(a.len==&&a.s[]==)continue;
a.s[a.len]=num[i];
a.len++;
md=mod(a);
if(vis[md]){
a.len--;
continue;
}
if(md==){ print_ans(a);
return;
}
vis[md]=;
dl.push(a);
a.len--;
}
}
puts("-1");
}
int main(){
int del[],flot=;
while(~scanf("%d%d",&N,&M)){
memset(del,,sizeof(del));
for(int i=;i<M;i++){
int temp;
scanf("%d",&temp);
del[temp]=;
}
int i,j;
for(i=,j=;i<=;i++){
if(!del[i])num[j++]=i;
}
M=j;
printf("Case %d: ",++flot);
C=;
bfs();
}
return ;
}

Yet Another Multiple Problem(bfs好题)的更多相关文章

  1. POJ1426:Find The Multiple(算是bfs水题吧,投机取巧过的)

    http://poj.org/problem?id=1426 Description Given a positive integer n, write a program to find out a ...

  2. HDU 4474 Yet Another Multiple Problem ( BFS + 同余剪枝 )

    没什么巧办法,直接搜就行. 用余数作为每个节点的哈希值. #include <cstdio> #include <cstring> #include <cstdlib&g ...

  3. HDU 4474 Yet Another Multiple Problem BFS

    题意:求m的倍数中不包含一些数码的最小倍数数码是多少.比如15 ,不包含0  1 3,答案是45. BFS过程:用b[]记录可用的数码.设一棵树,树根为-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. HDU-4471 Yet Another Multiple Problem (BFS+路径还原)

    Problem Description There are tons of problems about integer multiples. Despite the fact that the to ...

  6. hdu4474 Yet Another Multiple Problem

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

  7. 2012Chhengdu K - Yet Another Multiple Problem

    K - Yet Another Multiple Problem Time Limit:20000MS     Memory Limit:65536KB     64bit IO Format:%I6 ...

  8. POJ 3984 - 迷宫问题 - [BFS水题]

    题目链接:http://poj.org/problem?id=3984 Description 定义一个二维数组: int maze[5][5] = { 0, 1, 0, 0, 0, 0, 1, 0, ...

  9. fzuoj Problem 2182 水题

    http://acm.fzu.edu.cn/problem.php?pid=2182 Problem 2182 水题 Accept: 188    Submit: 277Time Limit: 100 ...

随机推荐

  1. SharedPreferences最佳实践

    转:http://blog.csdn.net/xushuaic/article/details/24513599 笔记摘要:该文章是我在Android Weekly中看到的,以前也一直用SharedP ...

  2. 正选反选JS

    JS <script> window.onload=function(){ var oTher=document.getElementById('other'); var oCheck=d ...

  3. Linux 网络编程基础(4) -- Ping 的C代码实现

    1.背景 在进行网络编程的时候,通常使用的协议有TCP协议,UDP协议.这些协议在简历套接字之初需要制定套接字的类型,比如TCP应当设置为 SOCK_STREAM, UDP对应的套接字应当设置为SOC ...

  4. WIX 学习笔记- 1 简介

    一个项目 Code Complete 后,程序员们欢欣鼓舞,以为事情到此结束,可以 Happy 了.其实 Code Complete 五十之于百里.一个没有运行在设备上,为人们创造价值的项目是注定失败 ...

  5. 权威验证:MSDN会明确告诉你下载的光盘镜像是否正宗微软原版

    MSDN是微软官方网站.这个网站的职能之一,就是向MSDN订户(付费相当高昂)提供Microsoft Windows资源,即大家通常说的操作系统光盘镜像.相信大家手头都有不少这类下载,但究竟是否微软的 ...

  6. 04737_C++程序设计_第7章_类模板与向量

    例7.1 使用类模板的实例. 例7.2 求4个数中最大值的类模板程序. #include <iostream> using namespace std; template <clas ...

  7. 值得赞扬的尝试与进步——CSDN开源夏令营第一印象

    注:写这篇文章时我并未參加CSDN开源夏令营,也不确定是否会參加以及是否能參加上. 欣闻CSDN举办了"CSDN开源夏令营"活动.第一感觉是CSDN作为活动的组织者是很值得称赞的. ...

  8. js 使用for循环遍历数组

    今天写个无聊的东西!for循环的使用! 例如以下:定义a数组,b为伪数组! var a = [1,2,3,0,5,4]; var b = document.getElementsByTagName(' ...

  9. C#反射调用程序集类中方法

    建立类 class OperatorClass { /// <summary> /// 加法 /// </summary> /// <param name="x ...

  10. English - 英语学习小笔记

    1.It is...to do sth:做某事是.... 解析:It 是形式主语,后面一半接形容词做表语,to do sth是不定式短语作真正主语. 2.make do和make doing是两种表达 ...