Sums of 1, 2, 3 (2)
InterviewTime limit1sMemory limit512 MB
Find the k-th composition of n using parts 1, 2 and 3 in lexicographic order, or print -1 when it does not exist.
- Level
Easy3 of 10
- Topics
- Backtracking, Dynamic programming
- Solved
- No attempts yet
Problem
There are 7 ways to write the integer 4 as a sum of 1, 2, and 3. Every sum uses at least one number.
- 1+1+1+1
- 1+1+2
- 1+2+1
- 2+1+1
- 2+2
- 1+3
- 3+1
Sorted in lexicographic order, they come out in this order.
- 1+1+1+1
- 1+1+2
- 1+2+1
- 1+3
- 2+1+1
- 2+2
- 3+1
Given integers and , write a program that finds the expression ranked -th in lexicographic order among the ways to write as a sum of 1, 2, and 3.
Input
The first line contains two integers and separated by a space. is a positive integer smaller than 11, and is a natural number at most .
Output
Print the expression ranked -th in lexicographic order among the ways to write as a sum of 1, 2, and 3. Do not put spaces around the plus signs. If there is no -th expression, print -1.