您的位置:首页 > 其它

hdu 4474 Yet Another Multiple Problem (bfs)

2014-10-24 18:53 465 查看
///给你一个数n,再给你m个k  求 各位都不为这m个数的n的最小倍数
# include<stdio.h>
# include<algorithm>
# include<iostream>
# include<string.h>
# include <string>
# include<queue>
using namespace std;
int vis[100010],map[11],tt[11],n;
int k,x;
struct node
{
int mod;
string s;
} e,w;
void bfs()
{
memset(vis,0,sizeof(vis));
queue<node>q;
e.mod=0;
e.s="";
q.push(e);
x=0;
while(!q.empty())
{
e=q.front();
q.pop();
for(int i=0; i<k; i++)
{
w=e;///
x=w.mod*10+tt[i];
if(x==0)
continue;
x%=n;
if(!vis[x])
{

w.mod=x;
w.s+=(tt[i]+'0');
vis[x]=1;
if(x%n==0)
{
cout<< w.s <<endl;
return ;
}
q.push(w);
}
}
}
puts("-1");
}
int main()
{
int m,a,i,cas=0;
while(~scanf("%d%d",&n,&m))
{
memset(map,0,sizeof(map));
while(m--)
{
scanf("%d",&a);
map[a]=1;
}
k=0;
for(i=0; i<10; i++)
{
if(map[i]==0)
tt[k++]=i;
}
printf("Case %d: ",++cas);
bfs();
}
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: