Next partition into terms
Time limit2sMemory limit1024 MB
Given a partition of n written in nondecreasing order, print the next partition in lexicographic order, or No solution if none exists.
- Level
Medium6 of 10
- Topics
- Greedy, Math, Combinatorics, Implementation
- Solved
- No attempts yet
Problem
A partition of a number into terms is a multiset of positive integers whose sum is . Partitions that differ only in the order of their terms are considered the same, so we may assume the terms of a partition are sorted in nondecreasing order.
For example, there are 7 partitions of 5 into terms:
\begin{align*} 5&=1+1+1+1+1\\ 5&=1+1+1+2\\ 5&=1+1+3\\ 5&=1+2+2\\ 5&=1+4\\ 5&=2+3\\ 5&=5 \end{align*}
In the example above the partitions are ordered lexicographically: first by the first term of the partition, then by the second term, and so on. In this problem you are given a partition into terms and must find the next partition in lexicographic order.
Input
The input file contains one line, a partition of the number into terms (). The terms of the partition are given in nondecreasing order.
Output
Print to the output file one line, the partition of into terms that comes next in lexicographic order after the one in the input file. If the input file contains the last partition of into terms, print <<No solution>>.