题意:求m的倍数中不包含一些数码的最小倍数数码是多少。比如15 ,不包含0  1 3,答案是45.

BFS过程:用b[]记录可用的数码。设一棵树,树根为-1.树根的孩子是所有可用的数码,孩子的孩子也是所有可用的数码。这样从根到叶子节点这条路径所组成的数表示一个可行的数。

__                 __

剪枝:(A % m ==  B % m)  =>  (AX % m ==  BX % m)   即如果搜索到一个数X, X%m == a (a !=0) , 则以后如果搜索到Y , Y%m == a,则不必继续搜索。 这样一棵树是有尽头的。

所有这棵树的节点最多才m-1个。

这个剪枝在逻辑电路课上突然灵光突现,昨天模拟赛就怎么也想不来了,额,熬夜要变傻子了。 还有碰到两个陷阱,m==0的时候要特判。不能用节点来直接保存答案,long long 不够用,递归输出答案吧。

#include <stdio.h>
#include <string.h>
//#include <queue>
using namespace std;
#define MAXN 100005
int N,m;
bool vis[MAXN];
int pre[MAXN];
int a[];
int b[];
int num[MAXN];
int que[MAXN];
int front,rear;
void print(int x) //递归从前往后输出答案
{
if (pre[x] != - )
print(pre[x]);
printf("%d",num[x]);
return;
}
int main()
{
int i,j,k,x,cas=;
int casb,ans;
while (scanf("%d%d",&N,&m)!=EOF)
{
printf("Case %d: ",++cas);
int x;
int casb=;
memset(a, , sizeof(a));
memset(b, , sizeof(b));
memset(vis, , sizeof(vis));
for (i=; i<m; i++)
{
scanf("%d",&x);
a[x]=;
}
for (i = ; i <= ; i++)
if (a[i] == )
b[casb++]=i;
if (N == ) //特判
{
if (b[] == )
printf("0\n");
else
printf("-1\n");
continue;
} int flag=;
int ans;
front=;rear=;
for (i = ; i < casb; i++) //最高位
if (b[i]!=)
{
que[rear++]=b[i]%N;
vis[b[i]%N]=true;
num[rear-]=b[i];
pre[rear-]=-;
if (b[i] % N == )
{
ans=b[i];
flag=;
break;
}
}
if (flag == )
{
printf("%d\n",ans);
continue;
}
int tmp;
ans=-;
flag=;
while (front != rear) //bfs
{
tmp=que[front]; for (i = ; i < casb; i++)
if (!vis[(tmp*+b[i]) % N]) //剪枝
{
que[rear]=(tmp*+b[i])% N;
vis[(tmp*+b[i])% N ]=true;
pre[rear] = front;//保存父节点
num[rear] = b[i]; //保存本节点这个位数的数字
if ((tmp*+b[i]) % N == )
{
print(rear);
printf("\n");
flag=;
break;
}
rear++;
}
if (flag)
break;
front++;
}
if (flag == )
printf("-1\n"); }
return ;
}

HDU 4474 Yet Another Multiple Problem BFS的更多相关文章

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

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

  2. 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 ...

  3. hdu 4474 Yet Another Multiple Problem

    题意: 找到一个n的倍数,这个数不能含有m个后续数字中的任何一个 题解: #include<stdio.h> #include<string.h> #include<qu ...

  4. Yet Another Multiple Problem(bfs好题)

    Yet Another Multiple Problem Time Limit : 40000/20000ms (Java/Other)   Memory Limit : 65536/65536K ( ...

  5. hdu 4474 大整数取模+bfs

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4474 (a*10+b)%c = ((a%c)*10+b%c)%c; 然后从高位开始枚举能填的数字填充, ...

  6. HDU-4471 Yet Another Multiple Problem (BFS+路径还原)

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

  7. 2012Chhengdu K - Yet Another Multiple Problem

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

  8. hdu4474 Yet Another Multiple Problem

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

  9. HDU 1019 Least Common Multiple【gcd+lcm+水+多个数的lcm】

    Least Common Multiple Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Ot ...

随机推荐

  1. ESLint =》tslint.json

    结论:将ESLint提示注意()里面的规则属性在tslint.json中"rules": { } 里设置为false 1.ES6: ESLint提示"Require Ob ...

  2. css中有些属性的前面会加上“*”或“_(兼容IE浏览器)

    给不同浏览器识别: color{ background-color: #CC00FF; /*所有浏览器都会显示为紫色*/ background-color: #FF0000\9; /*IE6.IE7. ...

  3. python学习笔记--关于函数的那点事1

    函数参数 1.位置参数 类似于java函数的基本参数,按照顺序和结构定义参数 2.默认参数 def method(param,defaultParam=defaultValue) 调用时,可以调用me ...

  4. 爬虫文件存储-3:Redis

    前提条件: 安装并运行redis服务端程序,安装RedisPy库 说明:Redis 是 StrictRedis 的子类,它的主要功能是用于向后兼容旧版本库里的几个方法,官方推荐使用 StrictRed ...

  5. CentOS7下安装ELK三件套

    ELK用于分布式收集,然后elasticsearch用于分析数据,在Kibana中可以查看数据.报表. 目前公司日志数据量暂时不使用elasticsearch集群,只是用的elasticsearch单 ...

  6. 746. Min Cost Climbing Stairs(动态规划)

    On a staircase, the i-th step has some non-negative cost cost[i] assigned (0 indexed). Once you pay ...

  7. assertion: 18 { code: 18, ok: 0.0, errmsg: "auth fails" }

    MongoDB Version: 2.4.7 Mongodump: $ bin/mongodump -u admin -p admin -d test -o ./tmp/ connected to: ...

  8. Spring MVC REST 风格的 URL

    前言 本文主要内容为 REST 风格的 URL. REST REST(Representational State Transfer).(资源)表现层状态转化.它是一种架构风格,用 url 来访问网络 ...

  9. N天学习一个Linux命令之帮助命令:man

    前言 工作中每天都在使用常用的命令和非常用的命令,忘记了用法或者参数,都会bing一下,然后如此循环.一直没有真正的系统的深入的去了解命令的用法,我决定打破它.以前看到有人,每天学习一个linux命令 ...

  10. Android LaunchAnyWhere (Google Bug 7699048)漏洞具体解释及防御措施

    開始 近日,Google修复一个组件安全的漏洞LaunchAnyWhere(Google Bug 7699048). 这个漏洞属于Intend Based提取漏洞,攻击者利用这个漏洞,能够突破了应用间 ...