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 ...
随机推荐
- 【待解决】关于CLASSPATH的显示问题
CLASSPATH? 环境变量? 环境变量CLASSPATH是操作系统JVM的搜索路径,它可以让JVM依据这些路径找到可执行文件.class文件,就相当于环境变量PATH对操作系统windows的作用 ...
- noone is not in the sudoers file ubuntu
Login as root or su to get root prompt type visudo an editor will open find a line says root ALL=( ...
- sql语句-6-高级查询
- LVS入门篇(三)之LVS的工作模式和调度算法
1.NAT模型 (1)原理图: ①.客户端(200.10.10.1)将请求发往前端的负载均衡器(114.100.80.10),请求报文源地址是CIP(客户端IP),后面统称为CIP),目标地址为VIP ...
- Yii 2.0 使用验证码
Yii2.0 提供了验证码组件.调用起来比较方便.以登录页面添加验证码为例. 1. 模型中添加字段和验证规则. common\models\LoginForm 添加如下代码 public $captc ...
- LAUNCHXL-28379D入门学习-第一篇
1. 首先安装controlSUITE或者C2000ware软件,TI官网下载,安装后包括C2000的函数库和例程之类的,还可以和CCS搭配使用.controlSUITE安装完之后大约4个G,所以我安 ...
- 利用webbrowser自动查取地点坐标
概述 有时候我们需要去查询某些地点的坐标,那么我们可以用百度提供的坐标拾取系统http://api.map.baidu.com/lbsapi/getpoint/index.html,但是会发现它只能一 ...
- php开发文章发布示例(正则表达式实例开发)
存档: post.php <form method="post" action="viewthread.php" target="_blank& ...
- 译图智讯VIN码识别助力汽配商转型升级
汽配猫是上海佳驰经合能源科技有限公司自主开发的汽车配件B2B网上商城及服务平台,该平台依托互联网云技术.利用创新的商业模式及互联网思维,整合汽配产业链优秀资源,为汽车维修保养企业等产业链各方面提供汽配 ...
- Raft 一致性协议算法 《In search of an Understandable Consensus Algorithm (Extended Version)》
<In search of an Understandable Consensus Algorithm (Extended Version)> Raft是一种用于管理日志复制的一致性算 ...