#P6593. [YsOI2020] 义已失吾亦死
[YsOI2020] 义已失吾亦死
Description
Ysuperman’s kindergarten not only focuses on cultural classes and competitive programming classes, but also teaches everyone to develop morally, intellectually, physically, and aesthetically. One weekend, the well-rounded Ysuperman climbed Mount Y.
When climbing, Ysuperman did not take the main road for vehicles, but instead took the muddy trail beside it. After walking for a long time, he suddenly found that the way back had become blurry, and in front of him stood a huge rock wall. To his shock, he discovered that the wall actually had writings from the last century! “义已失吾亦死”. Looking at these words, he seemed to feel a special kind of charm.
After returning to kindergarten, the excited Ysuperman immediately created other sentences, but found that most of them had lost their charm. After two and a half years of research, TA finally discovered that “义已失吾亦死” actually corresponds to the number string ! With a clearer direction, he decided to study mapping a sentence to a number. A “charming” number must satisfy the following conditions:
-
In decimal, it is a natural number.
-
Its digits only contain the three digits .
-
It is congruent to modulo a given constant .
Now Ysuperman already has many digits , with of them respectively.
Ysuperman wants to form a charming number of length that is as large as possible.
Ysuperman knows that if TA were still a student, TA would surely qualify for the Hydroxyl Program because of this discovery. For TA’s childhood dream, can you help him?
Input Format
This problem contains multiple test cases.
There are test cases. The first line contains . Then for each test case:
The first line contains two positive integers , representing the length of the charming number Ysuperman wants to form and the given constant .
The second line contains three natural numbers , representing the initial counts of digits that Ysuperman has.
Output Format
If Ysuperman cannot obtain a charming number, output -1.
Otherwise, output the maximum charming number Ysuperman can form.
A newline is required between two test cases.
5
1 1
1 1 1
3 5
1 1 2
6 62
3 2 1
23 13
10 10 10
233 10
233 233 233
5
545
114514
55555555554444444441111
-1
5
100 64
33 33 34
114 63
33 33 50
115 62
111 11 1
192 60
8 1 7
233 64
100 100 33
5555555555555555555555555555555555444444444444444444444444444441111111111111111111111111111111414144
555555555555555555555555555555555555555555555555444444444444444444444444444444441111411111111111111111111111111111
5444444444111111111114111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111114
-1
55555555555555555555555555555555544444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444411111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111414144
Hint
Sample Explanation
Sample Explanation :
In the first test case, you can form , and the maximum is .
In the second test case, you can form , and the maximum is .
In the third test case, you can only form .
Constraints
To pay tribute to NOI, the problem setter specially prepared a generous partial score table.
| Test Point ID | |||
|---|---|---|---|
| Property 1 | |||
| Property 2 | |||
Property 1: .
Property 2: .
For of the testdata, it holds that:
.
.
.
.
Hint
If you do not know what a natural number means, Ysuperman provides a link: link.
If you do not know what modulo means, Ysuperman provides another link: link。
Translated by ChatGPT 5
京公网安备 11011102002149号