ZOJ 3596 Digit Number(BFS)

Digit Number

Time Limit: 10 Seconds      Memory Limit: 65536 KB

Given an integer n and an integer m, please calculate the minimal multiple of n which consists of exactly m different digits.

Input

This problem has several test cases. The first line of the input is an integer T (0 < T ≤ 150) indicates the number of test cases. Each test case is a line of 2 integers n (0 < n ≤ 1000) and m (0 < m ≤ 10)

Output

For each test case, if you can find the minimal multiple of n and x satisfying the above condition, please output a line as "x=n*y", otherwise output "Impossible" in a single line.

Sample Input

3
37 1
2 2
100 1

Sample Output

111=37*3
10=2*5
Impossible

Author: CAO, Peng
Contest: The 12th Zhejiang University Programming Contest

题意:求一个数N的最小的一个倍数中包含M个不同的数字。

分析:这道题一开始真没想到会用BFS,我用的是最简单粗暴的枚举出倍数判断的方法,果断TLE。

明显这道题如果用枚举倍数再判断的方法是不行的,因为并没有枚举的上界,倍数可以无限大,所以会超时。

换个思路,采用BFS从高位试数字,用二进制或运算来记录哪一些数字是出现过的,还有到目前为止除以x后的余数,最大状态总数1024*1000,还要记录路径,因为要输出结果,目标状态是选取了恰好 m 个不同数字,且余数为 0 。路径可以直接用状态在队列里的下标来表示就可以,这种表示法的队列要求用数组,不能用STL。

原文地址:https://www.cnblogs.com/clliff/p/4483710.html