您的位置:首页 > 其它

HDU 4474 Yet Another Multiple Problem bfs枚举所有余数

2016-03-22 09:40 537 查看
题意:给一个数n,和m个十进制个位数,问n的最小倍数是多少,前提是最小倍数里面没有这m个数。



想法:首先不管是暴力还是怎么写,都会有一个限制,那就是什么时候表示如果再扩大也不会再有n的倍数且满足的数了,那么此时输出-1,需要找到这个临界点。如果现在用很多位数组成了一个新的数,如果这个数%n的余数出现过,那么他就没有必要再继续下去了,如果n的所有余数都出现了除了0,还是找不到,那么显然就不存在解了。



pair<string,int>pq;

string a用c语言输出为:printf("%s",a.c_str());



#include<iostream>
#include<cstdio>
#include<queue>
#include<cstring>
#include<string>
using namespace std;
const int nodes=10000+5;
int n,m;
bool vis[nodes],noused[10];
string bfs()
{
queue<pair<string,int> >q;
while(!q.empty()) q.pop();
pair<string,int>head,nxt,init;
init.first="";init.second=0;
q.push(init);
while(!q.empty())
{
head=q.front();
for(int i=0;i<10;i++)
{
if(head.first.length()==0&&i==0) continue;
if(noused[i]) continue;
char p='0'+i;
string kk=head.first+p;
int x=(head.second*10+i)%n;
if(!vis[x])
{
if(!x)
{
return kk;
}
vis[x]=true;
nxt.first=kk;
nxt.second=x;
q.push(nxt);
}
}
q.pop();
}
return "-1";
}
int main()
{
int ca=1;
while(~scanf("%d%d",&n,&m))
{
memset(vis,false,sizeof(vis));
memset(noused,false,sizeof(noused));
for(int i=1;i<=m;i++)
{
int a;
scanf("%d",&a);
noused[a]=true;
}
printf("Case %d: ",ca++);
printf("%s\n",bfs().c_str());
}
return 0;
}

内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: