CF BUDDY
← Problems·

1835A · k-th equality

1700 · brute force, implementation, math

Problem: Consider all equalities of form a+b=ca + b = c, where aa has AA digits, bb has BB digits, and cc has CC digits. All the numbers are positive integers and are written without leading zeroes. Find the kk-th lexicographically smallest equality when written as a string like above or determine that it does not exist.

For example, the first three equalities satisfying A=1A = 1, B=1B = 1, C=2C = 2 are

  • 1+9=101 + 9 = 10,
  • 2+8=102 + 8 = 10,
  • 2+9=112 + 9 = 11.

An equality ss is lexicographically smaller than an equality tt with the same lengths of the numbers if and only if the following holds:

  • in the first position where ss and tt differ, the equality ss has a smaller digit than the corresponding digit in tt.

Input Format: Each test contains multiple test cases. The first line of input contains a single integer tt (1t1031 \leq t \leq 10^3) — the number of test cases. The description of test cases follows.

The first line of each test case contains integers AA, BB, CC, kk (1A,B,C61 \leq A, B, C \leq 6, 1k10121 \leq k \leq 10^{12}).

Each input file has at most 55 test cases which do not satisfy A,B,C3A, B, C \leq 3.

Output Format: For each test case, if there are strictly less than kk valid equalities, output 1-1.

Otherwise, output the kk-th equality as a string of form a+b=ca + b = c.

Note: In the first test case, the first 99 solutions are: 1,1,2,1,2,3,1,3,4,1,4,5,1,5,6,1,6,7,1,7,8,1,8,9,2,1,3\langle 1, 1, 2 \rangle, \langle 1, 2, 3 \rangle, \langle 1, 3, 4 \rangle, \langle 1, 4, 5 \rangle, \langle 1, 5, 6 \rangle, \langle 1, 6, 7 \rangle, \langle 1, 7, 8 \rangle, \langle 1, 8, 9 \rangle, \langle 2, 1, 3 \rangle.

Int the third test case, there are no solutions as the smallest possible values for aa and bb are larger than the maximal possible value of cc10+10=20>910 + 10 = 20 > 9.

Please note that whitespaces in the output matter.

Sample Cases

Case 1

Input

7
1 1 1 9
2 2 3 1
2 2 1 1
1 5 6 42
1 6 6 10000000
5 5 6 3031568815
6 6 6 1000000000000

Output

2 + 1 = 3
10 + 90 = 100
-1
9 + 99996 = 100005
-1
78506 + 28543 = 107049
-1

Similar problems

00:00:00
Loading editor…
Welcome! I'm your coding tutor for this problem. Use the chips below to reveal stored hints or get AI feedback on your code. I'll guide you step by step — never giving away the solution.

Sign in to unlock AI tutor feedback