Yet Another Multiple Problem(bfs好题)
Yet Another Multiple Problem
Time Limit : 40000/20000ms (Java/Other) Memory Limit : 65536/65536K (Java/Other)
Total Submission(s) : 2 Accepted Submission(s) : 1
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?
按照数的位数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好题)的更多相关文章
- POJ1426:Find The Multiple(算是bfs水题吧,投机取巧过的)
http://poj.org/problem?id=1426 Description Given a positive integer n, write a program to find out a ...
- HDU 4474 Yet Another Multiple Problem ( BFS + 同余剪枝 )
没什么巧办法,直接搜就行. 用余数作为每个节点的哈希值. #include <cstdio> #include <cstring> #include <cstdlib&g ...
- HDU 4474 Yet Another Multiple Problem BFS
题意:求m的倍数中不包含一些数码的最小倍数数码是多少.比如15 ,不包含0 1 3,答案是45. BFS过程:用b[]记录可用的数码.设一棵树,树根为-1.树根的孩子是所有可用的数码,孩子的孩子也是 ...
- 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 ...
- HDU-4471 Yet Another Multiple Problem (BFS+路径还原)
Problem Description There are tons of problems about integer multiples. Despite the fact that the to ...
- hdu4474 Yet Another Multiple Problem
Yet Another Multiple Problem Description There are tons of problems about integer multiples. Despite ...
- 2012Chhengdu K - Yet Another Multiple Problem
K - Yet Another Multiple Problem Time Limit:20000MS Memory Limit:65536KB 64bit IO Format:%I6 ...
- POJ 3984 - 迷宫问题 - [BFS水题]
题目链接:http://poj.org/problem?id=3984 Description 定义一个二维数组: int maze[5][5] = { 0, 1, 0, 0, 0, 0, 1, 0, ...
- fzuoj Problem 2182 水题
http://acm.fzu.edu.cn/problem.php?pid=2182 Problem 2182 水题 Accept: 188 Submit: 277Time Limit: 100 ...
随机推荐
- 关于jQuery中toggle()函数的使用
今天遇到一个有趣的例子,将它记录下来. 一个一级菜单,里边有一个二级菜单,二级菜单是通过锚点来链接页面元素的.想要实现的效果是当点击锚点时,页面链接到相应锚点,同时二级菜单隐藏,再点击一级菜单时,继续 ...
- asp.net mvc 注册中的邮箱激活功能实现(二)
邮件发送功能封装 /// <summary> /// 发送注册邮件 /// </summary> /// <param name ...
- 一个由proguard与fastJson引起的血案
更多内容在这里查看 https://ahangchen.gitbooks.io/windy-afternoon/content/ 更新微信sdk导致ComposeData中的内部类ComposeDat ...
- EPiServer网文
ListItemCollection Rending: http://joelabrahamsson.com/episerver-7-and-mvc-how-to-customize-renderin ...
- 单机Oracle+asm(11.2.0.3.0) Patch Set Update(11.2.0.3.7 )
之前写过一篇关于PSU升级的案例,参考如下: http://blog.csdn.net/jyjxs/article/details/8983880 但是,感觉有些地方理解的不是很透彻明白,照猫画虎的比 ...
- Android中SharedPreferences函数具体解释
Android平台提供了一个SharedPreferences类,它是一个轻量级应用程序内部轻量级的存储方案,特别适合用于保存软件配置參数,比方boolean,int,float,long,Strin ...
- Ext.Net 使用总结之查询条件中的起始日期
2.关于查询条件中起始日期的布局方式 首先上一张图,来展示一下我的查询条件的布局,如下: 大多数时候,我们的查询条件都是一个条件占一个格子,但也有不同的时候,如:查询条件是起始日期,则需要将这两个条件 ...
- CocoaPods 出现 OTHER_LDFLAGS 错误的解决方法
CocoaPods 出现 OTHER_LDFLAGS 错误的解决方法 在一些项目中运行 pod install 后经常会出现如下错误 [!] The target `项目名 [Debug]` over ...
- 第一个输出程序 Console.WriteLine
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.T ...
- JavaSE复习日记 : 继承关系和super关键字以及继承关系中方法的覆写
/* * 类的继承和super关键字 * * 软件开发的三大目的: * 可拓展性; * 可维护性; * 可重用性; * * 这里单说下可重用性这一项: * 为了代码复用,复用方式有: * 函数的调用复 ...