Problem Description
Given a positive integer n, your task is to find a positive integer m, which is a multiple of n, and that m contains the least number of different digits when represented in decimal. For example, number 1334 contains three different
digits 1, 3 and 4.
 
Input
The input consists of no more than 50 test cases. Each test case has only one line, which contains a positive integer n ( 1<=n < 65536). There are no blank lines between cases. A line with a single `0' terminates the input.
 
Output
For each test case, you should output one line, which contains m. If there are several possible results, you should output the smallest one. Do not output blank lines between cases.
 
Sample Input
7
15
16
101
0
 
Sample Output
7
555
16
1111
 
Source

思路:最多仅仅须要出现两个不同的数字,先尝试一个数字的情况。再搜两个数字的情况。

#include <stdio.h>
#define min(A,B)(A<B?A:B)
#define INF 999999999 struct N{
int len,num;
bool operator<(const struct N &p) const
{
if(len==p.len) return num<p.num; return len<p.len;
}
}sin,tt; struct S{
int val,last,m,len;
}que[1000000],t; int i,j;
bool mod[65536];
char ans[100][1000]; void dfs(int idx)
{
if(que[idx].last!=-1) dfs(que[idx].last); ans[i*10+j][que[idx].len]=que[idx].val+'0';
} int main()
{
int n,k,temp,id,mnlen; while(~scanf("%d",&n) && n)
{
sin.len=INF;
sin.num=INF; for(i=1;i<=9;i++)
{
for(j=0;j<n;j++) mod[j]=0;
temp=0;
for(j=1;;j++)
{
temp=(temp*10+i)%n; if(!temp)
{
tt.len=j;
tt.num=i;
sin=min(sin,tt);
} if(!mod[temp]) mod[temp]=1;
else break;
}
} if(sin.len<INF)
{
for(i=0;i<sin.len;i++) printf("%d",sin.num);
puts(""); continue;
} if(n<100)
{
printf("%d\n",n); continue;
} mnlen=INF; for(i=1;i<=9;i++)
{
for(j=0;j<=9;j++)
{
if(i==j) continue; for(k=0;k<n;k++) mod[k]=0; que[0].val=i;
que[1].val=j;
que[0].last=-1;
que[1].last=0;
que[0].m=i;
que[1].m=i*10+j;
que[0].len=0;
que[1].len=1; mod[i]=mod[i*10+j]=1; int top=0;
int bottom=2; while(top<bottom)
{
t=que[top]; if(!t.m)
{
if(t.len<mnlen)
{
mnlen=t.len;
id=i*10+j;
}
dfs(top);
ans[i*10+j][t.len+1]=0;
break;
} if(i<j)
{
if(!mod[(t.m*10+i)%n])
{
mod[(t.m*10+i)%n]=1; que[bottom].last=top;
que[bottom].len=t.len+1;
que[bottom].m=(t.m*10+i)%n;
que[bottom++].val=i;
} if(!mod[(t.m*10+j)%n])
{
mod[(t.m*10+j)%n]=1; que[bottom].last=top;
que[bottom].len=t.len+1;
que[bottom].m=(t.m*10+j)%n;
que[bottom++].val=j;
}
}
else
{
if(!mod[(t.m*10+j)%n])
{
mod[(t.m*10+j)%n]=1; que[bottom].last=top;
que[bottom].len=t.len+1;
que[bottom].m=(t.m*10+j)%n;
que[bottom++].val=j;
} if(!mod[(t.m*10+i)%n])
{
mod[(t.m*10+i)%n]=1; que[bottom].last=top;
que[bottom].len=t.len+1;
que[bottom].m=(t.m*10+i)%n;
que[bottom++].val=i;
}
} top++;
}
}
} puts(ans[id]);
}
}

HDU-1664-Different Digits(BFS)的更多相关文章

  1. HDU 4333 Revolving Digits 扩张KMP

    标题来源:HDU 4333 Revolving Digits 意甲冠军:求一个数字环路移动少于不同数量 等同 于的数字 思路:扩展KMP求出S[i..j]等于S[0..j-i]的最长前缀 推断 nex ...

  2. HDU 1428 漫步校园 (BFS+优先队列+记忆化搜索)

    题目地址:HDU 1428 先用BFS+优先队列求出全部点到机房的最短距离.然后用记忆化搜索去搜. 代码例如以下: #include <iostream> #include <str ...

  3. [HDU 1973]--Prime Path(BFS,素数表)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1973 Prime Path Time Limit: 5000/1000 MS (Java/Others ...

  4. hdu 2102 A计划-bfs

    Time Limit: 3000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submission( ...

  5. HDU 1072(记忆化BFS)

    题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=1072 题目大意:走迷宫.走到装置点重置时间,到达任一点时的时间不能为0,可以走重复路,求出迷宫最短时 ...

  6. HDU 2364 (记忆化BFS搜索)

    题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=2364 题目大意:走迷宫.从某个方向进入某点,优先走左或是右.如果左右都走不通,再考虑向前.绝对不能往 ...

  7. HDU 2579 (记忆化BFS搜索)

    题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=2579 题目大意:走迷宫.对于障碍点,只有当前(dep+1)%k才能走,问最少时间. 解题思路: 只有 ...

  8. HDU 2653 (记忆化BFS搜索+优先队列)

    题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=2653 题目大意:迷宫中有普通点和陷阱.其中普通点可以走可以飞,但是陷阱只能飞.走耗时1,飞耗时2.但 ...

  9. hdu - 1728逃离迷宫 && hdu - 1175 连连看 (普通bfs)

    http://acm.hdu.edu.cn/showproblem.php?pid=1728 这两道题花了一下午的时候调试,因为以前做过类似的题,但是判断方向的方法是错的,一直没发现啊,真无语. 每个 ...

  10. HDU 4707 Pet(BFS)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4707 题目大意:在一个无环的,从0开始发散状的地图里,找出各个距离0大于d的点的个数 Sample I ...

随机推荐

  1. JavaScript基础知识----基本语法

    JavaScript 语句 JavaScript 语句向浏览器发出的命令.语句的作用是告诉浏览器该做什么. 分号 ; 分号用于分隔 JavaScript 语句. 通常我们在每条可执行的语句结尾添加分号 ...

  2. Laravel 简单使用七牛云服务

    前言 路漫漫其修远兮,吾将上下而求索.学习 Laravel 之初觉得所有东西都很厉害的样子,现在看来就是很厉害啊!最近在写一个项目上传的模块,要上传图片到七牛云,昨天看了一下午七牛云官方的文档感觉还是 ...

  3. 运用Python语言编写获取Linux基本系统信息(二):文件系统使用情况获取

    本文跟着上一篇文章继续写,上一篇文章的链接 运用Python语言编写获取Linux基本系统信息(一):获得Linux版本.内核.当前时间 一.随便说说 获取文件系统使用情况的思路和上一篇获取主要系统是 ...

  4. javascript 检测密码强度

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/ ...

  5. opencv是什么

    OpenCV是一个用于图像处理.分析.机器视觉方面的开源函数库.       不管你是做科学研究,还是商业应用,opencv都能够作为你理想的工具库,由于,对于这两者,它全然是免费的.该库採用C及C+ ...

  6. 公共 DNS server IP 地址

    公共 DNS server IP 地址 名称 DNS server IP 地址 CNNIC SDNS 1.2.4.8 210.2.4.8 114 DNS 114.114.114.114 114.114 ...

  7. Java简单记录

    XML指令: <?xml version="1.0" encoding="UTF-8" standalone="no" ?> & ...

  8. String详解说明

    大家平时都string都不是很在意,但是每当面试碰到String的时候在“==”和equals之间就乱了,下面我来说一说String,也许不够全面,请大家多多指教,希望会帮到处于蒙圈状态的人们. 一. ...

  9. postgres-xc手册生成方法

    步骤   检测编译环境  安装编译工具  编译 以上只在linux环境当中进行,本人所用系统ubuntu15.04 检测编译环境 在posgtgresql目录下运行./configure,并安装需要安 ...

  10. windows环境下Mongodb分片配置

    使用MongoDB的GridFS来存储文件,以前一直使用单个服务,分布式环境也一直没有配置成功,今天参考了几位大神的文章终于配置成功,再也不用担心文件存储的性能和安全啦.以下是自己部署的过程和示例,记 ...