#P6593. [YsOI2020] 义已失吾亦死

    ID: 5482 远端评测题 1000ms 128MiB 尝试: 0 已通过: 0 难度: 7 上传者: 标签>动态规划,dpO2优化状态压缩,状压

[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 114514114514! 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 1,4,51,4,5.

  • It is congruent to 00 modulo a given constant pp.

Now Ysuperman already has many digits 1,4,51,4,5, with a1,a4,a5a_1,a_4,a_5 of them respectively.

Ysuperman wants to form a charming number of length nn 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 TT test cases. The first line contains TT. Then for each test case:

The first line contains two positive integers n,pn,p, representing the length of the charming number Ysuperman wants to form and the given constant pp.

The second line contains three natural numbers a1,a4,a5a_1,a_4,a_5, 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 11:

In the first test case, you can form 1,4,51,4,5, and the maximum is 55.

In the second test case, you can form 145,155,415,455,515,545145,155,415,455,515,545, and the maximum is 545545.

In the third test case, you can only form 114514114514.


Constraints

To pay tribute to NOI, the problem setter specially prepared a generous partial score table.

Test Point ID nn a1,a4,a5a_1,a_4,a_5 pp
11 =1=1 =0=0 =1=1
22 =2=2 1\le 1 10\le 10
33 =3=3 3\le 3
44 =15=15 15\le 15
55 20\le 20
66 30\le 30
77 35\le 35
88 233\le 233 2\le 2
99
1010 50\le 50 64\le 64
1111 55\le 55
1212 60\le 60
1313 65\le 65
1414 70\le 70
1515 75\le 75
1616 80\le 80
1717 233\le 233 Property 1
1818
1919 Property 2
2020
2121 233\le 233
2222
2323
2424
2525

Property 1: a1+a4+a5=na_1+a_4+a_5=n.

Property 2: a1=a4=a5=na_1=a_4=a_5=n.

For 100%100\% of the testdata, it holds that:

0a1,a4,a52330 \le a_1,a_4,a_5 \le 233.
1n2331 \le n \le 233.
1p641 \le p \le 64.
0T50 \le T \le 5.


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