HDU 4474 Yet Another Multiple Problem BFS
题意:求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的更多相关文章
- HDU 4474 Yet Another Multiple Problem ( BFS + 同余剪枝 )
没什么巧办法,直接搜就行. 用余数作为每个节点的哈希值. #include <cstdio> #include <cstring> #include <cstdlib&g ...
- 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 4474 Yet Another Multiple Problem
题意: 找到一个n的倍数,这个数不能含有m个后续数字中的任何一个 题解: #include<stdio.h> #include<string.h> #include<qu ...
- Yet Another Multiple Problem(bfs好题)
Yet Another Multiple Problem Time Limit : 40000/20000ms (Java/Other) Memory Limit : 65536/65536K ( ...
- hdu 4474 大整数取模+bfs
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4474 (a*10+b)%c = ((a%c)*10+b%c)%c; 然后从高位开始枚举能填的数字填充, ...
- HDU-4471 Yet Another Multiple Problem (BFS+路径还原)
Problem Description There are tons of problems about integer multiples. Despite the fact that the to ...
- 2012Chhengdu K - Yet Another Multiple Problem
K - Yet Another Multiple Problem Time Limit:20000MS Memory Limit:65536KB 64bit IO Format:%I6 ...
- hdu4474 Yet Another Multiple Problem
Yet Another Multiple Problem Description There are tons of problems about integer multiples. Despite ...
- 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 ...
随机推荐
- 一文详解Spring Cloud Feign重试机制
前言 Feign组件默认使用Ribbon的重试机制并增加了根据状态码判断重试机制,默认情况下是不启用的.Feign使用的是Spring Retry组件,需要引入依赖才能启用. 一.POM引入Sprin ...
- js基本类型的包装对象
var test = "test"; test.a = "hello"; console.log(test.a); 在JavaScript中,“一切皆对象”,数 ...
- form:input 标签使用
<form:input path="suplier" htmlEscape="false" maxlength="50" id=&qu ...
- Python----递归------Eight Queens 八皇后问题
递归思想是算法编程中的重要思想. 作为初学者,对递归编程表示很蒙逼,每次遇到需要递归的问题,心里就有一万头草泥马飞过~~~~~~(此处略去一万头草泥马) 在B站看数据结构与算法的视频时,视频中给了两个 ...
- Vector 二维数组 实现
1.C++实现动态二维数组 int **p; p = ]; //注意,int*[10]表示一个有10个元素的指针数组 ; i < ; ++i) { p[i] = ]; } 2.利用指针数组实现二 ...
- 通过git向github提交项目
按顺序学习 https://www.cnblogs.com/forget406/p/6045499.html#top https://blog.csdn.net/xiaoputao0903/artic ...
- CA认证相关
目录 CA认证相关 基本概念 CA认证相关 公钥私钥详解>> 基本概念 密钥对: 在非对称的加密技术中心, 有两种密钥, 分为私钥和公钥,私钥 --RSA算法-->公钥. 公钥: 公 ...
- linux 头文件和库文件的设置
GCC/G++会查找系统默认的include和link的路径,以及自己在编译命令中指定的路径.自己指定的路径就不说了,这里说明一下系统自动搜索的路径. [1]include头文件路径 除了默认的/us ...
- [Codeforces 872]比赛记录
强行打了$cf$上的第一场比赛,现在感觉自己的$rating$会炸飞= = A 这是练习输入输出吗QAQ,竟然$WA$了两遍QAQ,我$WA$的一声就哭了出来啊QAQ B 好像很水的乱扫就好了,m ...
- [Bzoj4195] [NOI2015] 程序自动分析 [并查集,哈希,map] 题解
用并查集+离散化,注意:并查集数组大小不是n而是n*2 #include <iostream> #include <algorithm> #include <cstdio ...