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. 英文:known good board ( KGB) / 中文:测试用标准板,黄金板

    作为标准部件提供的.完全符合设计电气性能的在制板,可以作为与其它印制板比较的标准.

  2. Java学习之IO字节流

    字节流分为FileInputStream 和FileOutputStream package com.io; import java.io.File; import java.io.FileInput ...

  3. 《Java并发编程实战》第二章 线程安全性 读书笔记

    一.什么是线程安全性 编写线程安全的代码 核心在于要对状态訪问操作进行管理. 共享,可变的状态的訪问 - 前者表示多个线程訪问, 后者声明周期内发生改变. 线程安全性 核心概念是正确性.某个类的行为与 ...

  4. Codeforces Round #315 (Div. 2A) 569A Music (模拟)

    题目:Click here 题意:(据说这个题的题意坑了不少人啊~~~)题目一共给了3个数---- T 表示歌曲的长度(s).S 表示下载了歌曲的S后开始第一次播放(也就是说S秒的歌曲是事先下载好的) ...

  5. BZOJ 1042: [HAOI2008]硬币购物( 背包dp + 容斥原理 )

    先按完全背包做一次dp, dp(x)表示x元的东西有多少种方案, 然后再容斥一下. ---------------------------------------------------------- ...

  6. 循环调用修正sic86 2改后的(除了第一年有点诡异,其他年份可以正常修复)

    create or replace procedure rebuild_sic86_wyl(pi_aac001 in number, po_fhz out varchar2, po_msg out v ...

  7. 【转】centos安装memcached+php多服务器共享+session多机共享问题

    参考博文: centos安装memcached 源码安装 Yum安装Memcache Memcached内存分配优化及使用问题 <转>php+memcached 实现session共享 P ...

  8. POJ 2479 不相交最大子段和

    题目意思还是很好理解的,在一个数列中,找出不相交的两个子串使得其和最大. 解题思路: 对于每个i来说,求出[0 ~ i - 1] 的最大子段和以及[i ~ n - 1]的最大子段和,在加起来,求最大的 ...

  9. GDG shanghai programming one hour by JavaScript

    刚在昨天参加了一场JS入门编程的活动,目的就是提升对JS的兴趣. 因为是针对零基础开发者的,一上来就是“Hello World!”了 当然,想用JS输出"Hello World!" ...

  10. mul16

    设计思想:乘法运算本身就可以看做是一个移位相加的过程               1 1 0 1 0     = 26*              1 0 1 1 1    = 23          ...