#P6593. The Charming Number
The Charming Number
The Charming Number
Ysuperman’s kindergarten teaches a well‐rounded education including moral, intellectual, physical, and aesthetic development. One weekend, Ysuperman climbed Y Mountain by a narrow dirt road and discovered an ancient inscription on a huge stone: “义已失吾亦死”. Back at the kindergarten the same day, Ysuperman excitedly created many sentences, but most lost their charm. After two and a half years of research, Ysuperman realized that the sentence “义已失吾亦死” actually corresponds to the number \(114514\). Inspired by this discovery, Ysuperman defined a charming number as a natural number (in decimal notation) that satisfies the following conditions:
- Its digits are drawn only from the set \(\{1,4,5\}\).
- If we take the number modulo a given constant \(p\), the result is \(0\); i.e. \(number \equiv 0 \pmod{p}\).
Ysuperman has a limited supply of digits: at most \(a_1\) copies of digit 1, \(a_4\) copies of digit 4, and \(a_5\) copies of digit 5. He wants to form a charming number with exactly \(n\) digits (by selecting some digits from his supply, without exceeding the available counts) and the number should be as large as possible. If no such number can be formed, output -1
.
inputFormat
The input consists of a single line containing five space‐separated integers:
- \(n\) — the required length of the number.
- \(p\) — the modulus value.
- \(a_1\) — the available count of digit 1.
- \(a_4\) — the available count of digit 4.
- \(a_5\) — the available count of digit 5.
outputFormat
Output the largest charming number that can be formed using exactly \(n\) digits and not exceeding the available counts for each digit. If no such number exists, output -1
.
sample
3 5 1 1 1
415