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 ...
随机推荐
- 20155302 《Java程序设计》实验一(Java开发环境的熟悉)实验报告
20155302 <Java程序设计>实验一(Java开发环境的熟悉)实验报告 实验内容 1.使用JDK编译.运行简单的Java程序: 2.使用Eclipse 编辑.编译.运行.调试Jav ...
- 20145226夏艺华 网络对抗技术EXP8 WEB基础实践
20145226夏艺华 网络对抗技术EXP8 WEB基础实践 实验问题回答 1.什么是表单? 表单在网页中主要负责数据采集功能.一个表单有三个基本组成部分: 表单标签:这里面包含了处理表单数据所用CG ...
- SIM_AT_Command
下面是GET请求 AT+HTTPPARA? 查询设置的Para命令 AT+SAPBR=1,1 (模块启动后设置一次即可)OK AT+HTTPINIT (初始化)OK AT+HTTPPARA=CONTE ...
- mongod 安装
mongod --logpath F:\mongo\db\logs\logs.log --logappend --dbpath F:\mongo\db\data --directoryperdb -- ...
- python装饰器简单使用
装饰器和闭包关联很大,要先明白闭包是什么 原始代码: def foo(): print('fcc') 增加装饰器 from time import ctime,sleep def w(fcc): de ...
- katalon系列九:DEBUG调试功能
Katalon Studio做为一个IDE,具有和其他IDE一样的Debug功能,可以让我们方便的调试代码.将脚本切换到Script模式,在你想设断点的行首双击,或右击选择:(Groovy)Toggl ...
- SPP-net论文总结
SPPNet方法来自<Spatial Pyramid Pooling in Deep Convolutional Networks for Visual Recognition> ,是大神 ...
- Literature Books
Lean In (Sheryl Sandberg) Option B (Sheryl Sandberg) Ready Player One
- sql批量更新
-----------------更新无锡医院名称 update Opt_DKI_Hospital set centerName =tmp.[医院名称] from Opt_DKI_Hospital h ...
- 一个简单的rest_framework demo
models.py from django.db import models class UserInfo(models.Model): username = models.CharField(max ...