题意:求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. 安装hiredis后swoole扩展消失

    php -m报错: PHP Warning: PHP Startup: Unable to load dynamic library 'swoole' (tried: /home/work/study ...

  2. 蓝桥-区间K大数查询

    问题描述: 给定一个序列,每次询问序列中第l个数到第r个数中第K大的数是哪个. 输入格式 第一行包含一个数n,表示序列长度. 第二行包含n个正整数,表示给定的序列. 第三个包含一个正整数m,表示询问个 ...

  3. c发邮件

    /* base64编码 */ static const char* base64_enc_map = \ "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmno ...

  4. 《ABCD组》第八次作业:ALPHA冲刺

    <ABCD组>第八次作业:ALPHA冲刺 项目 内容 这个作业属于哪个课程 http://www.cnblogs.com/nwnu-daizh/ 这个作业的要求在哪里 https://ww ...

  5. SQLServer中的Cross Apply、Outer Apply

    https://www.2cto.com/database/201304/206330.html

  6. 编译器错误消息: CS0016: 未能写入输出文件“c:\Windows\Microsoft.NET\Framework64\v4.0.30319\Temporary ASP.NET Files\root\64b7b75e\4dfbfca6\App_Web_company.master.4611739e.l3t-kpke.dll”--“拒绝访问。 ”

    报错 原因:IIS没有权限操作TEMP文件夹 最后重新启动站点

  7. jQuery学习----简单介绍,基本使用,操作样式,动画

    jQuery简单介绍 jq是js的插件库,说白了,jq就是一个js文件 凡事能用jq实现的,js都能实现.但是js能实现的,jq不一定能够实现 jq的引入 http://www.bootcdn.cn ...

  8. 【codeforces 765E】Tree Folding

    [题目链接]:http://codeforces.com/problemset/problem/765/E [题意] 给你一棵树; 可以把一个节点的两条相同长度的链合并成一条链; 且这两条相同长度的链 ...

  9. Android传递Bitmap的两种简单方式及其缺陷

    Android传递Bitmap的几种简单方式 一,通过Intent的Bundle. 比如有两个activity,A,B,从A进入B.先在A中将Bitmap写进去: Resources res=getR ...

  10. Caused by: android.os.TransactionTooLargeException总结

    错误信息 Error: android.os.TransactionTooLargeException W/ActivityManager(344): android.os.TransactionTo ...