MULTII - Yet Another Multiple Problem

no tags 

Given a positive integer N (1 <= N <= 10000) and some digits, find the smallest positive multiple of N whose decimal notation does not contain any of the given digits.

Input

Each test case contains two lines. The first line contains two integers N and m separated by a single space. The second line contains m digits separated by spaces. Input terminates by EOF.

Output

For each test case output its case number (starting from 1) and the answer in a single line, or "-1" (without quotes) if the answer doesn't exist.

Example

Input:
2345 3
7 8 9
100 1
0

Output:
Case 1: 2345
Case 2: -1

hide comments
yefricruz_02: 2021-10-17 08:15:27

I also got TLE, but when I added scanf() == 2 (input), I got accepted.

yefricruz_02: 2021-10-17 08:12:42

100 is just a number, you want a multiple of this number e.g. 200, 300, 400, ..., but then, there are 'm' digit restrictions (in 2° example 1) and the digit is 0. With the restriction, the sentence would be like give me a number multiple of 100 BUT the number should not contain digit 0. It is impossible because 100, 200, 300, 1000 always gives contain 0, that's why the answer is -1.

vishalcc: 2021-04-04 16:10:38

2 test case is correct? 100 is not a digit please explain if i m wrong..

mehulcoder: 2020-10-14 22:06:08

Can someone please help me!!!! Why am I getting TLE? The output seems fine to me

[NG]: Don't link to or post code here, use forum.

Last edit: 2020-10-14 23:09:44
mahmoud_acm5: 2019-09-06 22:32:56

i solved it in 0.72 ms but have something missing about when it will end i solve it but can't prove it now should i draw a tree or something on paper ?

Last edit: 2020-07-11 01:15:53
sarwar__05: 2018-04-25 17:17:43

similar one: http://www.spoj.com/problems/ONEZERO/en/

(Tjandra Satria Gunawan)(曾毅昆): 2014-11-13 20:52:24

Finally accepted ;-)
Answer can have more than 10000 digits :-O
It always hard when I deal with BigInteger :-/

Bharath Reddy: 2014-09-24 18:57:51

Not so easy for me :D
Tried three different algorithms and finally got AC in 3.97 sec \m/

Last edit: 2014-09-24 18:58:05
abdelkarim: 2013-08-07 01:34:26

easy one!

Darko Aleksic: 2012-12-11 22:24:17

Got RTE when trying to print the number recursively. This is a general request - can the Java stack be increased?

I knew I've seen this problem somewhere before:
http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&category=21&page=show_problem&problem=1934

Last edit: 2012-12-11 22:57:04

Added by:Fudan University Problem Setters
Date:2012-11-21
Time limit:4s
Source limit:50000B
Memory limit:1536MB
Cluster: Cube (Intel G860)
Languages:All except: ASM64
Resource:ACM/ICPC Regional Contest, Chengdu 2012