#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 aa and a binary string SS of length bb.

Simply put, her Lucky Number is a positive integer nn that satisfies the following conditions:

  • The number of digits of nn is bb, and it has no leading 00.

  • If the ii-th character of SS is 11, then the number formed by the first ii digits of nn is a multiple of aa; otherwise, the number formed by the first ii digits of nn is not a multiple of aa.

For a number, the number formed by its first ii digits means the number obtained by concatenating the first ii digits in order. For example, for 312311312311, the number formed by its first 33 digits is 312312, and the number formed by its first 55 digits is 3123131231.

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 a,ba, b, with meanings as described in the statement.

The second line contains a string SS of length bb.

Output Format

Output one line containing the smallest number nn 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 Input Sample Output


【Sample Explanation】

For sample 1, 1010 is a 22-digit number, and the number formed by the first 11 digit of 1010 is 11, which is not a multiple of 22, while the number formed by the first 22 digits of 1010 is 1010, which is a multiple of 22. Since 1010 is already the smallest two-digit number, there is no smaller number than 1010 that satisfies the conditions.

For sample 2, we need to construct a 11-digit number that is divisible by 1010. Obviously, such a number does not exist.


【Constraints】

This problem uses bundled tests.

Subtask ID aa \leq bb \leq Score
Subtask 1 1010 11 2020
Subtask 2 22
Subtask 3 66
Subtask 4 22 1818
Subtask 5 1010 10510^5

For all testdata, it is guaranteed that 2a102 \le a \le 10, 1b1051 \le b \le 10^5, and SS contains only 0 and 1.

Translated by ChatGPT 5