HDU_4474_数位bfs——经典搜索
2018-02-02 19:08
363 查看
Yet Another Multiple Problem
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?
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.
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 3
7 8 9
100 1
0
Sample Output
Case 1: 2345
Case 2: -1
Source
2012 Asia Chengdu Regional Contest
mean:
第一行给你n,m;
第二行给你m个>=0 &&<10的整数;
求多少倍的n使每一位都不包含 这m个数字;
ans
这样的话 a%n=x ,(a+c)%n=x;a+c是>a要取最小所以就用mod 出的值来做 标记判重,后面搜索到的数是否能用,
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?
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.
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 3
7 8 9
100 1
0
Sample Output
Case 1: 2345
Case 2: -1
Source
2012 Asia Chengdu Regional Contest
mean:
第一行给你n,m;
第二行给你m个>=0 &&<10的整数;
求多少倍的n使每一位都不包含 这m个数字;
ans
这样的话 a%n=x ,(a+c)%n=x;a+c是>a要取最小所以就用mod 出的值来做 标记判重,后面搜索到的数是否能用,
#include<bits/stdc++.h> using namespace std; int n,m; const int N=1e4+10; int vis ,deal ,cnt; struct node { int pre; string str; }num; void bfs() { queue<node> q; num.pre=0,num.str=""; q.push(num); int i,j; while(!q.empty()) { node tmp=q.front(); q.pop(); for(i=0;i<cnt;i++) { num=tmp; int x=num.pre*10+deal[i]; if(!x) continue; x%=n; if(!vis[x]) { vis[x]=1; num.pre=x; num.str+=deal[i]+'0'; if(!x) { cout<<num.str<<endl; return ; } q.push(num); } } } puts("-1"); return ; } int main() { int i,j,ca=0; while(cin>>n>>m) { memset(vis,0,sizeof(vis)); int stu[111]={0}; cnt=0; while(m--) { int x; scanf("%d",&x); stu[x]=1; } for(i=0;i<10;i++) { if(!stu[i]) deal[cnt++]=i; } printf("Case %d: ",++ca); bfs(); } }
相关文章推荐
- hdu 4474 数位搜索bfs
- 一类数位限制倍数的BFS问题...URAL 1495,POJ 1465,HDU 4474,HDU 1226
- hdu 2717:Catch That Cow(bfs广搜,经典题,一维数组搜索)
- 2012 Asia Chengdu Regional Contest hdu 4474(数位bfs)
- hdu 4474 Yet Another Multiple Problem 模型转换 BFS搜索
- hdu 4474 搜索bfs
- hdu 4474 Yet Another Multiple Problem 模型转换 BFS搜索 根据剩余类建图广搜
- hdu 1043 八数码 经典搜索问题 BFS+MAP
- HDU 4474 Yet Another Multiple Problem(搜索 BFS)
- hdu 4474 数位BFS
- hdu 4474 数位搜索
- HDU 1312 Red and Black(经典搜索,DFS&BFS三种方式)
- HDU-4474 Yet Another Multiple Problem BFS搜索
- 【搜索】 HDU 2128 Tempter of the Bone II BFS 状压
- HDU 1180 (BFS搜索)
- hdu 1664 Different Digits 数位BFS(怎么都是这些题……
- HDU 2612 (BFS搜索+多终点)
- hdu_1240_3维bfs,注意搜索坐标
- hdu 4474 Yet Another Multiple Problem (bfs)
- 【搜索入门专题1】 hdu1242 J - Rescue c++ stl容器之优先队列+BFS