HDU4474_Yet Another Multiple Problem
题意很简单,要你用一些数字,组成一个数的倍数,且那个数最小。
比赛的时候没能做出来,深坑啊。
其实我只想说我以前就做过这种类型的题目了,诶。
题目的解法是数位宽搜。
首先把可用的数位提取出来,从小到大一个一个放到后面去。这样保证了出现的数字一定是最小的。
同时记录出现过的余数的情况,因为同一个余数k*10+ai再对n取余的数是相同的,所以对于同一个余数我们不需要走两遍,这样就保证了搜索的时间复杂度为O(n)。
我们只要对于每一个状态,直接枚举可选择的数字放在他们后面,直到找到一个余数为0的就可以停止了。
总的时间复杂度为O(n*10),因为后面最多可能有10个数可以放上面嘛。。。。
对于深搜的每一个状态只要用一个数字和一个前趋节点就可以了,输出的时候递归输出,因为每一个状态都是由前面的状态推过来的嘛。
#include <iostream>
#include <cstdio>
#include <cstring>
#define maxn 10010
using namespace std; int pre[maxn],s[maxn];
char q[maxn];
bool res[maxn];
bool b[];
int a[],N,n,m,k,t1,t2,cur; void output(int pos)
{
if (pre[pos]>) output(pre[pos]);
printf("%d",(int)q[pos]);
} void bfs()
{
if (n==)
{
if (b[]) printf("0\n");
else printf("-1\n");
return;
}
t1=t2=;
memset(res,false,sizeof res);
for (int i=; i<=; i++)
if (b[i]) q[++t2]=i,pre[t2]=,s[t2]=i%n,res[i%n]=true;
while (t1<t2)
{
if (s[++t1]==)
{
output(t1);
printf("\n");
return;
}
for (int i=; i<=N; i++)
if (res[(s[t1]*+a[i])%n]==false)
{
t2++;
s[t2]=(s[t1]*+a[i])%n;
res[s[t2]]=true;
q[t2]=a[i];
pre[t2]=t1;
}
}
printf("-1\n");
} int main()
{
int cas=;
while (scanf("%d%d",&n,&m)!=EOF)
{
for (int i=; i<=; i++) b[i]=true;
N=;
while (m--) scanf("%d",&k),b[k]=false;
for (int i=; i<=; i++) if (b[i]) a[++N]=i;
printf("Case %d: ",++cas);
bfs();
}
return ;
}
HDU4474_Yet Another Multiple Problem的更多相关文章
- 2012Chhengdu K - Yet Another Multiple Problem
K - Yet Another Multiple Problem Time Limit:20000MS Memory Limit:65536KB 64bit IO Format:%I6 ...
- Yet Another Multiple Problem(bfs好题)
Yet Another Multiple Problem Time Limit : 40000/20000ms (Java/Other) Memory Limit : 65536/65536K ( ...
- hdu4474 Yet Another Multiple Problem
Yet Another Multiple Problem Description There are tons of problems about integer multiples. Despite ...
- HDU-4471 Yet Another Multiple Problem (BFS+路径还原)
Problem Description There are tons of problems about integer multiples. Despite the fact that the to ...
- HDU 4474 Yet Another Multiple Problem【2012成都regional K题】 【BFS+一个判断技巧】
Yet Another Multiple Problem Time Limit: 40000/20000 MS (Java/Others) Memory Limit: 65536/65536 K ...
- hdu 4474 Yet Another Multiple Problem
题意: 找到一个n的倍数,这个数不能含有m个后续数字中的任何一个 题解: #include<stdio.h> #include<string.h> #include<qu ...
- HDU 4474 Yet Another Multiple Problem ( BFS + 同余剪枝 )
没什么巧办法,直接搜就行. 用余数作为每个节点的哈希值. #include <cstdio> #include <cstring> #include <cstdlib&g ...
- HDU 4474 Yet Another Multiple Problem BFS
题意:求m的倍数中不包含一些数码的最小倍数数码是多少.比如15 ,不包含0 1 3,答案是45. BFS过程:用b[]记录可用的数码.设一棵树,树根为-1.树根的孩子是所有可用的数码,孩子的孩子也是 ...
- K - Least Common Multiple
Time Limit:1000MS Memory Limit:32768KB 64bit IO Format:%I64d & %I64u Submit Status Descr ...
随机推荐
- 20155322 《Java程序设计》课堂实践项目 数据库-3-4
20155322 <Java程序设计>课堂实践项目 数据库-3-4 数据库-3 实践要求 参考教材代码完成下面的要求,提交能连接到world的截图(有学号水印),并提交代码的码云链接.查询 ...
- 实现Linux下dc的功能,计算后缀表达式的值
提交测试截图和码云练习项目链接,实现Linux下dc的功能,计算后缀表达式的值 -将运算符写在两个操作数之后的表达式称为"后缀表达式",如上面的中缀表达式可转换为后缀表达式1 2 ...
- ELKStack入门篇(四)之Filebeat
Filebeat是轻量级单用途的日志收集工具,用于在没有安装java的服务器上专门收集日志,可以将日志转发到logstash.elasticsearch或redis等场景中进行下一步处理. 官方文档: ...
- Docker入门篇(一)之docker基础
1.Docker 架构 http://blog.csdn.net/u012562943/article/category/6048991/1Docker 使用客户端-服务器 (C/S) 架构模式,使用 ...
- Yii 2.0 Gridview源码分析
GridView yii\grid\GridView 作用:GridView是Yii中的一个Widget,用来展示数据表格.有排序,分页和过滤功能. GridView默认界面如下.这是用Gii生成的. ...
- 建表/修改表名/增加删除字段(MySql)
修改表名:alter table 旧表名 rename 新表名; 删除字段:alter table 表名 drop 字段名; 增加字段:alter table 表名 add 字段名 字段类型 [def ...
- web存储机制(localStorage和sessionStorage)
web存储包括两种:sessionStorage 和 localStorage(都是限定在文档源级别,非同源文档间无法共享) 1.sessionStorage 数据放在服务器上(IE不支持) 严格用于 ...
- js显示对象所有属性和方法的函数
function ShowObjProperty2( obj ) { // 用来保存所有的属性名称和值 var attributes = '' ; var methods = '' // 开始遍历 f ...
- [Ubuntu] <uptime>命令
uptime 命令 就是查看系统启动时间的,前几个大家应该都很熟悉:当前时间.系统启动时间.正在登陆的用户数 最后的三个数字,分别代表过去 1分钟 5分钟 15分钟 的平均负载(Load Ave ...
- MAC终端安装指定版本node
MAC终端安装指定版本node 安装brew 终端上运行 $ /usr/bin/ruby -e “$(curl -fsSL https://raw.githubusercontent.com/Home ...