HDU 4474 Yet Another Multiple Problem【BFS+一个判断技巧】
来源:互联网 发布:linux下web服务器配置 编辑:程序博客网 时间:2024/05/16 05:21
题意:0-9这十个数字里面的若干个数字组合出一个数,使这个数是N的倍数,求最小的这个这样的数,不存在的话输出-1。
按照数的位数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 <cstdio>#include <cstring>#include <algorithm>using namespace std;#define N 1000020struct node { int pre, mod; char c;} q[N];int n, m;bool v[10010];void print(int x) { if (x == 0) return ; print(q[x].pre); putchar(q[x].c);}int main() { int cas = 1, t, a[10]; bool vis[10]; while (scanf("%d%d", &n, &m) == 2) { memset(vis, false, sizeof(vis)); for (int i=0; i<m; i++) { scanf("%d", &t); vis[t] = true; } m = 0; for (int i=0; i<10; i++) if (!vis[i]) a[m++] = i; printf("Case %d: ", cas++); if (m == 0) { puts("-1"); continue; } memset(v, false, sizeof(v)); int l = 1, r = 1, ans = -1; for (int i=0; i<m; i++) if (a[i]) { q[r].pre = 0; v[q[r].mod = a[i] % n] = true; q[r].c = a[i] + '0'; if (q[r].mod == 0) { ans = r; break; } r++; } while (l < r && ans == -1) { for (int i=0; i<m; i++) { q[r].mod = (q[l].mod*10 + a[i])%n; if (!v[q[r].mod]) { v[q[r].mod] = true; q[r].pre = l; q[r].c = a[i] + '0'; if (q[r].mod == 0) { ans = r; break; } r++; } } l++; } if (ans == -1) puts("-1"); else { print(ans); printf("\n"); } } return 0;}
- HDU 4474 Yet Another Multiple Problem【BFS+一个判断技巧】
- hdu 4474 Yet Another Multiple Problem bfs
- hdu 4474 Yet Another Multiple Problem (bfs)
- hdu 4474 Yet Another Multiple Problem(BFS)
- HDU 4474 Yet Another Multiple Problem BFS
- hdu 4474 Yet Another Multiple Problem 模型转换 BFS搜索
- HDU 4474 Yet Another Multiple Problem 搜索——BFS
- HDU 4474 Yet Another Multiple Problem (bfs+mod运算)
- HDU 4474 Yet Another Multiple Problem(搜索 BFS)
- hdu 4474 Yet Another Multiple Problem (余数判重 + BFS)
- hdu 4474 Yet Another Multiple Problem ( bfs + math)
- hdu 4474 Yet Another Multiple Problem(BFS)
- HDU-#4474 Yet Another Multiple Problem(BFS)
- HDU 4474 Yet Another Multiple Problem bfs枚举所有余数
- HDU 4474(Yet Another Multiple Problem)(BFS+同余定理)
- HDOJ 题目4474 Yet Another Multiple Problem(bfs,技巧,找一个数的倍数)
- Yet Another Multiple Problem-----BFS
- Yet Another Multiple Problem (hdu 4474)
- 我的小金库(跬步千里)
- TCP和UDP的区别有哪些
- Oracle 10046跟踪事件操作步骤
- java匿名内部类
- Flex+BlazeDs+Java的教程及Demo
- HDU 4474 Yet Another Multiple Problem【BFS+一个判断技巧】
- tomcat server 相关 数字证书 的生成
- 多态的一些理解
- [转]Unity——Export/Import Package功能和项目管理的研究
- 配置寄存器值为什么是OX2102
- tomcat设置项目默认启动
- Tomcat 系统架构与设计模式,第 1 部分: 工作原理 第 2 部分: 设计模式分析
- iOS 笔试、面试题
- C语言预处理命令之条件编译