Yet Another Multiple Problem(bfs好题)

Yet Another Multiple Problem

Time Limit : 40000/20000ms (Java/Other)   Memory Limit : 65536/65536K (Java/Other)
Total Submission(s) : 2   Accepted Submission(s) : 1
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?
 
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 ≤ 10[sup]4[/sup]). 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
 题意:就是给你一个n,让找出不含接下来m个数字能组成的n的倍数的最小解;参考了大神的写法;
/***************/

按照数的位数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<stdio.h>
#include<string>
#include<queue>
#include<string.h>
#include<algorithm>
#define mem(a) memset(a,0,sizeof(a))
using namespace std;
const int MAXN=1e4+;
int vis[MAXN],del[MAXN],pre[MAXN];
char al[MAXN];
int n;
void print_ans(){
int r=;
string ans;
while(ans.empty()||r!=){
ans+=al[r];
r=pre[r];//由于anser的下属是0,刚开始的数字的上司又是0,所以最后找到0结束循环;
}
reverse(ans.begin(),ans.end());
puts(ans.c_str());
}
bool bfs(){
queue<int>dl;
dl.push();
while(!dl.empty()){
int f1,f2;
f1=dl.front();//**
dl.pop();
for(int i=;i<=;i++){
if(del[i]||i==&&f1==)continue;
f2=(f1*+i)%n;
if(vis[f2])continue;//应该放上面
pre[f2]=f1;//**
al[f2]=i+'';
if(f2==){
print_ans();
return true;
}
vis[f2]=;
dl.push(f2);
}
}
puts("-1");
return false;
}
int main(){
int m,flot=;
while(~scanf("%d%d",&n,&m)){
mem(vis);mem(del);mem(pre);mem(al);
while(m--){
int temp;
scanf("%d",&temp);
del[temp]=true;
}
printf("Case %d: ",++flot);
bfs();
}
return ;
}

第二种方法wa:

 #include<stdio.h>
#include<string.h>
#include<queue>
#include<algorithm>
using namespace std;
const int MAXN=1e4+;
int num[];
struct Node{
int s[];
int len;
};
int M,C,N;
int vis[MAXN];
int mod(Node a){
int x=;
for(int i=;i<a.len;i++){
x=(x*C+a.s[i])%N;
}
return x;
}
void print_ans(Node a){
for(int i=;i<a.len;i++){
printf("%d",a.s[i]);
}
puts("");
}
void bfs(){
memset(vis,,sizeof(vis));
queue<Node>dl;
Node a;
a.len=;
int md;
for(int i=;i<M;i++){
a.s[]=num[i];
md=mod(a);
if(vis[md]||num[i]==)continue;
if(md==&&num[i]){
printf("%d\n",num[i]);
return;
}
vis[md]=;
dl.push(a);
}
while(!dl.empty()){
a=dl.front();
dl.pop();
for(int i=;i<M;i++){
if(a.len==&&a.s[]==)continue;
a.s[a.len]=num[i];
a.len++;
md=mod(a);
if(vis[md]){
a.len--;
continue;
}
if(md==){ print_ans(a);
return;
}
vis[md]=;
dl.push(a);
a.len--;
}
}
puts("-1");
}
int main(){
int del[],flot=;
while(~scanf("%d%d",&N,&M)){
memset(del,,sizeof(del));
for(int i=;i<M;i++){
int temp;
scanf("%d",&temp);
del[temp]=;
}
int i,j;
for(i=,j=;i<=;i++){
if(!del[i])num[j++]=i;
}
M=j;
printf("Case %d: ",++flot);
C=;
bfs();
}
return ;
}
上一篇:sql server 使用for xml path 将1列多行转换为字符串连接起来,俗称 sql 合并字符


下一篇:Java知多少(14)数组