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;
}
想法:首先不管是暴力还是怎么写,都会有一个限制,那就是什么时候表示如果再扩大也不会再有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;
}
相关文章推荐
- 微信公众帐号开发教程第12篇-图文消息全攻略
- Nginx安装与使用
- 数组指针 vs 指针数组
- 购物车搜索栏解决GET请求中文乱码问题
- 原生TabLayout实现ViewPagerIndicator
- concurrent包
- Android自动化测试之Monkeyrunner使用方法及实例 - 大头爸爸的博客 - 博客频道 - CSDN.NET
- 成都Uber优步司机奖励政策(3月22日)
- ExtJs之Ext.core.Element
- 作业三:代码复审
- 用正则表达式验证邮箱和手机号
- Python爬虫入门(5):URLError异常处理
- html5 web storage 存储
- .NET 把数据库数据放入数组的一段代码
- 常用的一些SQL语句
- 微软Hololens应用及开发——HoloLens介绍篇(二)硬件介绍
- VS2008错误调试方法
- Application,Seccion和Cookie的区别
- 北京Uber优步司机奖励政策(3月22日)
- [转]Ext ComboBox 默认选中某一项