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。

时间: 2024-08-04 22:28:57

ZOJ 3596 Digit Number(BFS)的相关文章

Zoj 2913 Bus Pass BFS

题意: 给你一张图,和一些指定的点,找一个点使得这些指定的点到这个点的距离的最大值最小 对每一个指定的点都做一遍BFS,更新到达每个点的距离,取较大值,然后扫一遍所有的点,找出最小即可. 注意:不同于走格子,因为方向比较多,所以要在扩展节点的时候就更新vis数组,不然有可能导致某个点的距离因为重复更新而不是最小值. 并且不要漏了一开始就更新起点的vis #include <cstdio> #include <vector> #include <cstring> #inc

ZOJ - 3890 Wumpus(BFS基础题)

Wumpus Time Limit: 2 Seconds      Memory Limit: 65536 KB One day Leon finds a very classic game called Wumpus.The game is as follow. Once an agent fell into a cave. The legend said that in this cave lived a kind of monster called Wumpus, and there we

ZOJ 3233 Lucky Number

Lucky Number Time Limit: 5000ms Memory Limit: 32768KB This problem will be judged on ZJU. Original ID: 323364-bit integer IO format: %lld      Java class name: Main Watashi loves M mm very much. One day, M mm gives Watashi a chance to choose a number

zoj 3478 Binary Land bfs

链接:http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=3478 Binary Land Time Limit: 2 Seconds      Memory Limit: 65536 KB Binary Land is a very simple action title for the Famicom with an interesting twist. You control, not one, but two peng

ZOJ 3622 Magic Number 打表找规律

A - Magic Number Time Limit:2000MS     Memory Limit:32768KB     64bit IO Format:%lld & %llu Submit Status Practice ZOJ 3622 Appoint description: Description A positive number y is called magic number if for every positive integer x it satisfies that

zoj 2736 Daffodil number

Daffodil number Time Limit: 2 Seconds      Memory Limit: 65536 KB The daffodil number is one of the famous interesting numbers in the mathematical world. A daffodil number is a three-digit number whose value is equal to the sum of cubes of each digit

ZOJ 3436 July Number(DFS)

题意   把一个数替换为这个数相邻数字差组成的数  知道这个数只剩一位数  若最后的一位数是7  则称原来的数为 July Number  给你一个区间  求这个区间中July Number的个数 从7开始DFS  位数多的数总能由位数小的数推出 #include <bits/stdc++.h> using namespace std; const int N = 1e6; int july[N], n; set<int> ans; int pw[] = {1, 10, 100,

ZOJ 3890 Wumpus (BFS)

题目链接:ZOJ 3890 Wumpus 题意:一个人在n*n的地图的左下角,他想逃出地图,并且他想要分数达到最高,他可以做的操作有直走,左转,右转,射击,爬,挖金矿6个操作,每个操作后分数减10,但是挖到金矿分数加1000,问你逃出地图的最大分数是多少.他可能遇到怪兽但是他不射击(也就是说没有射击的操作),金库在地图中也只有一个. 思路:BFS搜一遍就好了 AC代码: #include <stdio.h> #include <string.h> #include <queu

[递推+dfs]ZOJ 3436. July Number

题目大意: 将一个数字的相邻两位的差(的绝对值)组成一个新的数字,不断重复,如果最后得到7,就称这个数为July Number,比如9024 – 922 – 70 – 7.题目要求1e9范围内给定区间[a, b]里July Number的个数. 思路:逆向递推,既然题目求能化成 7 的数的个数,那么就从 7 逆着找出去 18 ,29,70,81,92等,(要注意的就是:还有从07,007.....等找出去的,每个数同理: 代码: /* SKY */ #include<iostream> #in