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. .html与.htm为网页后缀的区别

    有些人在做网页的时候会有疑问,到底是应该用.html还是.htm做网页后缀呢?.html和.htm有什么区别吗?在做网页时到底用哪一个好呢? 现在,我说一下我对这两个文件后缀的看法: 首先,要想使用后 ...

  2. JDBC----数据库连接池(connection pool)

    •数据库连接池的基本思想就是为数据库连接建立一个"缓冲池".预先在缓冲池中放入一定数量的连接,当需要建立数据库连接时,只需从"缓冲池"中取出一个,使用完毕之后再 ...

  3. win7 x64 驱动

    原文:win7 x64 驱动 从x86转x64 1.编译环境要为x64 2.修改inf文件 [Manufacturer] %MfgName%=Mfg0,NT,NTia64,NTAMD64 [Mfg0] ...

  4. web.py入门

    官网介绍: web.py is a web framework for Python that is as simple as it is powerful. web.py is in the pub ...

  5. 省部级干部list

    省部级干部表 省(市.自治区) 省(市)委书记 省(市)人大主任 省(市)长 省(市)政协主席 北京市 郭金龙 杜德印 王安顺 吉林 天津市 黄兴国 肖怀远 黄兴国 臧献甫 上海市 韩正 殷一璀(女) ...

  6. 51cto培训课程

    课程: 云计算: OpenStack | 虚拟化 | 云平台 | Office 365 | 云服务 | Docker | 其他 大数据: Spark | Hadoop | Storm | Hive | ...

  7. Android最新源码4.3下载-教程 2013-11

    Android最新源码4.3下载-教程 有的下载会出现问题: 需要 修改manifest.xml中的fetch: "git://Android.git.linaro.org/"  ...

  8. poj 1068 Parencodings(栈)

    题目链接:http://poj.org/problem?id=1068 思路分析:对栈的模拟,将栈中元素视为广义表,如 (((()()()))),可以看做 LS =< a1, a2..., a1 ...

  9. java IO回想小结

    java IO原理 IO流用来处理设备之间的传输数据 输入(input):读取外部数据(磁盘.等存储设备)到程序() (内存)中 输出(output):将程序(内存)数据输出到磁盘等存储设备 java ...

  10. if语句之求一元二次方程

    思路:1.首先明白什么叫做一元二次方程,当a不等于0的时候,此方程是一元二次方程 2.根据公式derta=b*b-4*a*c来判断根的情况 ①derta>0时,方程有两个不相等的实根 ②dert ...