题意:求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. vue03 axios

    4. 通过axios实现数据请求 vue.js默认没有提供ajax功能的. 所以使用vue的时候,一般都会使用axios的插件来实现ajax与后端服务器的数据交互. 注意,axios本质上就是java ...

  2. css3的基础知识

    transfrom的应用: 1.旋转:transform: rotate(30deg): 2.阴影效果:box-shadow: 10px 10px 5px #888888: 3.鼠标移入放大:tran ...

  3. Linux基础篇(1)

    1.计算机硬件的五大单元 五大单元:输入单元,输出单元,CPU内部的控制单元,算术逻辑单元与内存等五大部分. (Ps:CPU实际要处理的数据完全来自于内存) 2.计算机上常用的计算单位 (1)容量单位 ...

  4. DNS详细解析过程【转】

    转自:http://blog.csdn.net/crazw/article/details/8986504 先说一下DNS的几个基本概念: 一. 根域 就是所谓的“.”,其实我们的网址www.baid ...

  5. SSH免密登录的错误

    为了避免每次在测试环境启动hadoop都需要输入密码, 使用免密操作 ssh-keygen,然后一直按enter键 接着需要输入root密码, 输入root密码后一直报错,密码不正确, permiss ...

  6. 仿探探卡片滑动vue封装并发布到npm

    项目初始化使用 webpack-simple 方式比较方便和容易配置,原来的方式各种坑慎入 vue init webpack-simple vue-card-slide cd vue-card-sli ...

  7. Problem 28

    Problem 28 https://projecteuler.net/problem=28 Starting with the number 1 and moving to the right in ...

  8. linux学习4-crontab定时任务

    crontab -e  在当前用户下创建定时任务 我们通过这样一张图来了解 crontab 的文档编辑的格式与参数 在了解命令格式之后,我们通过这样的一个例子来完成一个任务的添加,在文档的最后一排加上 ...

  9. ReatEasy+用户指南----第9章@MatrixParam

    转载说明出处:http://blog.csdn.net/nndtdx/article/details/6870391 原文地址 http://docs.jboss.org/resteasy/docs/ ...

  10. 【ACM】hdu_2007_平方和与立方和_201307261533

    平方和与立方和Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Subm ...