HDOJ 4474 Yet Another Multiple Problem(BFS + 剪枝)
来源:互联网 发布:linux 播放器剪辑 编辑:程序博客网 时间:2024/05/01 10:12
给你一个数n,在不能用某些数字的情况下找到能表示成n的倍数的最小值,如果不能表示输出-1。
首先模运算的规则:
A % N == B % N → A*K % N == B*K % N → (A + C) % N == (B + C) % N → (A*K + C) % N == (B*K + C) % N
→ (A*10 + C) % N == (B*10 + C) % N
所以可以用bfs搜索,从数字最小的情况搜起。每次只需保存余数,和最先出现这个余数时,记录添加在末尾的那位数字,一旦遇到余数为0,则将记录中的数字逆序输出。
#include <iostream>#include <stdlib.h>#include <stdio.h>#include <string.h>#include <math.h>#include <queue>#include <algorithm>using namespace std;const int maxn = 10010;int n, m, x;bool dig[10];int pre[maxn], save[maxn];queue<int> q;void put(int u){ if(pre[u] != -1) put(pre[u]); printf("%d", save[u]);}void bfs(){ while(!q.empty()) q.pop(); int rema, now; for(int i = 1; i <= 9; i++) { if(!dig[i]) { rema = i % n; if(rema == 0) { printf("%d", i); return ; } q.push(rema); save[rema] = i; } } while(!q.empty()) { now = q.front(); q.pop(); for(int i = 0; i <= 9; i++) { if(!dig[i]) { rema = (now * 10 + i) % n; if(save[rema] == -1) { q.push(rema); pre[rema] = now; save[rema] = i; } if(rema == 0) { put(rema); return ; } } } } printf("-1");}int main(){ int cas = 0; while(~scanf("%d%d", &n, &m)) { printf("Case %d: ", ++cas); memset(dig, false, sizeof(dig)); memset(pre, -1, sizeof(pre)); memset(save, -1, sizeof(save)); for(int i = 0; i < m; i++) { scanf("%d", &x); dig[x] = true; } bfs(); printf("\n"); } return 0;}
0 0
- HDOJ 4474 Yet Another Multiple Problem(BFS + 剪枝)
- HDOJ 4474 Yet Another Multiple Problem(BFS)
- HDOJ 题目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
- 【搜索】 HDOJ 4474 Yet Another Multiple Problem
- HDOJ 4474 Yet Another Multiple Problem
- HDU 4474 Yet Another Multiple Problem(搜索 BFS)
- hdu 4474 Yet Another Multiple Problem(BFS)
- HDU-#4474 Yet Another Multiple Problem(BFS)
- Yet Another Multiple Problem-----BFS
- HDU 4474 Yet Another Multiple Problem(bfs+同余模定理剪枝)
- HDU 4474 Yet Another Multiple Problem(搜索剪枝)
- hdu 4474 Yet Another Multiple Problem 模型转换 BFS搜索
- HDU 4474 Yet Another Multiple Problem 搜索——BFS
- HDU 4474 Yet Another Multiple Problem (bfs+mod运算)
- 第三次自考总结——时间管理和学习知识
- java集合练习笔记
- 真实网络环境下使用易邮搭建网络邮件服务器并测试通过
- 关于点击电源按钮的时候出现了这情况Transport (VMDB) error -44: Message.
- Struts2文件上传
- HDOJ 4474 Yet Another Multiple Problem(BFS + 剪枝)
- C# 委托的发展 delegate, anonymous method and lambda
- CF#272 (Div. 2) B.(dfs)
- 关于struts2的注解
- echoprint
- 常用url
- 堆排序(C语言版)
- 使用zTree控件制作的表格形式的树形+数据菜单
- 6-1 多态性与虚函数