One-sequence
Time limit1sMemory limit128 MB
Find the lexicographically smallest walk of length n starting at 0 with +/-1 steps whose total sum is S, or report that none exists.
- Level
Medium6 of 10
- Topics
- Greedy, Implementation, Math, Prefix sum
- Solved
- No attempts yet
Problem
We call a sequence of integers a one-sequence when the difference between any two consecutive elements is either or and its first element is . Formally, is a one-sequence when:
- for every with : , and
- .
You are given the length of the sequence and the required sum of its elements. Several one-sequences of length can share the same sum, so you must output the lexicographically smallest one: compare two sequences element by element and prefer the one whose first differing element is smaller (because is always , the order is decided from onward). If no one-sequence of length sums to , report that no such sequence exists.
Input
The first line contains an integer with , the number of elements in the sequence. The second line contains an integer with , the required sum of the elements.
Output
If a one-sequence of length whose elements sum to exists, print its elements one per line, giving the lexicographically smallest such sequence (the -th element on the -th line). Otherwise print NIE (Polish for "no").