#P5261. Digit Sum Transformation
Digit Sum Transformation
Digit Sum Transformation
Given four positive integers (K), (S), (P), and (D), find the smallest (K)-digit natural number (x) in base-10 such that the sum of the digits of (x) equals (S) and the sum of the digits of (x \times D) equals (P). If no such number exists, output -1.
In formal terms, find the minimum (x) satisfying:
[
\text{digitsum}(x)=S, \quad \text{digitsum}(D\times x)=P, \quad 10^{K-1}\le x <10^{K}.
]
Here, (\text{digitsum}(n)) denotes the sum of the digits of (n) when written in decimal notation.
inputFormat
The input consists of a single line containing four space-separated positive integers: (K), (S), (P), and (D).
outputFormat
Output the smallest (K)-digit number (x) that satisfies the conditions. If no valid number exists, output -1.
sample
1 4 8 2
4