hdu 4474 搜索bfs
来源:互联网 发布:怎样查网卡mac地址 编辑:程序博客网 时间:2024/05/22 10:25
Yet Another Multiple Problem
Time Limit: 40000/20000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)Total Submission(s): 3740 Accepted Submission(s): 907
Problem Description
There are tons of problems about integer multiples. Despite the fact that the topic is not original, the content is highly challenging. That’s why we call it “Yet Another Multiple Problem”.
In this problem, you’re asked to solve the following question: Given a positive integer n and m decimal digits, what is the minimal positive multiple of n whose decimal notation does not contain any of the given digits?
In this problem, you’re asked to solve the following question: Given a positive integer n and m decimal digits, what is the minimal positive multiple of n whose decimal notation does not contain any of the given digits?
Input
There are several test cases.
For each test case, there are two lines. The first line contains two integers n and m (1 ≤ n ≤ 104). The second line contains m decimal digits separated by spaces.
Input is terminated by EOF.
For each test case, there are two lines. The first line contains two integers n and m (1 ≤ n ≤ 104). The second line contains m decimal digits separated by spaces.
Input is terminated by EOF.
Output
For each test case, output one line “Case X: Y” where X is the test case number (starting from 1) while Y is the minimal multiple satisfying the above-mentioned conditions or “-1” (without quotation marks) in case there does not exist such a multiple.
Sample Input
2345 37 8 9100 10
Sample Output
Case 1: 2345Case 2: -1
Source
2012 Asia Chengdu Regional Contest
/*题意: 给一个n(n<=10000), m个数字(长度为1),问最小的数x(x%n=0) 不包含这m个数。思路: 直接求,没想出解法. 对于一个数 x%n = m, 则 x` = x*10+i , 有 m` = (m*10+i)%n 我们可以利用 除了m个数字外的数来构造这个 X. 因为需要最小的, 则其长度与字典序排列皆最小. 通过BFS进行搜索. 每一个模n的余数只取第一次出现的,因为之后再出现的. 只可能更长或者更大. 必定不是最优解. 搜索节点,保存三个信息: 当前x的最后一位, 当前x%n的余数, 当前节点的父亲节点. 结果输出利用记忆父亲节点 ,然后递归输出即可.*/#include<stdio.h>#include<queue>#include<string.h>#define N 10005using namespace std;int b[N],pre[N],mark[N],n;void print(int k){ if(pre[k]) print(pre[k]); printf("%d",b[k]);}void bfs(){ queue<int>q; int i,r,x,y; for(i=1; i<10; i++) { if(!mark[i]) { r=i%n; if(r==0) { printf("%d\n",i); return; } else if(pre[r]==-1) { pre[r]=0;//pre指向前驱,此时无前驱 q.push(r);//根节点保存的mod值 b[r]=i;//保存i,用于输出 } } } while(!q.empty()) { x=q.front(); q.pop(); for(i=0; i<10; i++) { if(!mark[i]) { y=x*10+i; r=y%n; if(r==0) { print(x); printf("%d\n",i); return; }//表示没有r这个mod没有访问过,则把它加入队列 if(pre[r]==-1) { q.push(r); b[r]=i; pre[r]=x; } } } } printf("-1\n");}int main(){ int m,i,a,cnt=1; while(scanf("%d%d",&n,&m)!=EOF) { memset(mark,0,sizeof(mark)); memset(pre,-1,sizeof(pre)); for(i=1; i<=m; i++) { scanf("%d",&a); mark[a]=1; } printf("Case %d: ",cnt++); bfs(); } return 0;}
0 0
- hdu 4474 搜索bfs
- hdu 4474 数位搜索bfs
- HDU-1548(BFS搜索)
- 搜索 BFS HDU
- HDU 1429 BFS+状态搜索
- HDU 3220 IDA*搜索 || BFS
- 广度搜索BFS hdu-1312
- hdu 1072 Nightmare 搜索,bfs
- HDU-4460(bfs搜索)
- hdu 4474 Yet Another Multiple Problem 模型转换 BFS搜索
- HDU 4474 Yet Another Multiple Problem 搜索——BFS
- HDU 4474 Yet Another Multiple Problem(搜索 BFS)
- HDU 1175(搜索题,BFS)
- HDU 2102(搜索题,BFS)
- HDU 1180(搜索题,BFS)
- HDU 2612(搜索题,BFS)
- HDU 1312 广度优先搜索(BFS)
- HDU 1429 广度优先搜索(BFS)
- NSLog的使用
- Myeclipse 快捷键大全(绝对全)
- java Observer类使用实例
- Android--Fragment(2)——Fragment与Activity通信
- 经验风险与结构风险
- hdu 4474 搜索bfs
- 笔试面试2 用C实现C库的strcpy函数
- Fragment生命周期
- Zend_Log使用
- centos的VI编辑器,退出后。保存不了。
- DedeCMS 基本操作说明:栏目管理
- SAP MM移动类型概念详述
- Mondrian>>MDX语法学习--filter与iif的使用
- Java初学习 - Set的使用