#P6380. 『MdOI R2』Mayuri
『MdOI R2』Mayuri
Background
"Mayuri... how could this be? Why are you..."
"I am the crystallization of spiritual power. After carrying out the seal, of course I will disappear, right?"
"A seal? But you and I..."
"Can't I be sealed even at our first meeting? Idiot. I was born from everyone's spiritual power, so how could I hate you? From the moment I was born, I loved you."
"Mayuri..."
"Even though I try my best not to think about it, I must be very jealous of everyone..."
"Wait, Mayuri, don't disappear..."
"But I still have one thing I can brag about to everyone. Only I am the same as Shido..."
"The same?"
"I am no longer a life born only to disappear. Because I met you... that is enough."
"Mayuri..."
"Thank you, Shido."

Problem Description
Before leaving this world, Mayuri wants to find the Lucky Number that belongs to her.
Mayuri will give a number and a binary string of length .
Simply put, her Lucky Number is a positive integer that satisfies the following conditions:
-
The number of digits of is , and it has no leading .
-
If the -th character of is , then the number formed by the first digits of is a multiple of ; otherwise, the number formed by the first digits of is not a multiple of .
For a number, the number formed by its first digits means the number obtained by concatenating the first digits in order. For example, for , the number formed by its first digits is , and the number formed by its first digits is .
Now, please help Mayuri compute her Lucky Number.
Since there may be multiple numbers that satisfy the conditions, you need to output the smallest one. If it does not exist, output -1.
Input Format
The input consists of two lines.
The first line contains two integers , with meanings as described in the statement.
The second line contains a string of length .
Output Format
Output one line containing the smallest number that satisfies the conditions. If no such number exists, output -1.
2 2
01
10
10 1
1
-1
6 6
110100
601210
Hint
【Help and Hints】
To make it easier for contestants to test their code, this problem additionally provides an extra set of sample cases for contestants to use.
【Sample Explanation】
For sample 1, is a -digit number, and the number formed by the first digit of is , which is not a multiple of , while the number formed by the first digits of is , which is a multiple of . Since is already the smallest two-digit number, there is no smaller number than that satisfies the conditions.
For sample 2, we need to construct a -digit number that is divisible by . Obviously, such a number does not exist.
【Constraints】
This problem uses bundled tests.
| Subtask ID | Score | ||
|---|---|---|---|
| Subtask 1 | |||
| Subtask 2 | |||
| Subtask 3 | |||
| Subtask 4 | |||
| Subtask 5 |
For all testdata, it is guaranteed that , , and contains only 0 and 1.
Translated by ChatGPT 5