Ones
Time limit3sMemory limit512 MB
Given run lengths of n in binary, output run lengths of the binary form of sks(n), the total UFO count over 1 to n.
- Level
Hard9 of 10
- Topics
- Math, Combinatorics, Dynamic programming, Implementation
- Solved
- No attempts yet
Problem
Let be a sequence of zeros and ones. An utterly forlorn one (UFO) in is an extreme one (either the first or the last one in the sequence) that additionally does not neighbour any other one. For example, the sequence has two UFOs, the sequence has no UFO, and the sequence has exactly one UFO.
Let denote the total number of UFOs in the binary representations of the numbers from to . For example, , , , and .
We will work with very large numbers, so we represent them succinctly. Let be a positive integer and let be its binary representation, which starts with . The succinct representation of is the sequence of positive integers giving the lengths of the successive maximal blocks of equal digits. For example:
Given , compute the sequence .
Input
The first line contains one integer (), the length of the succinct representation of a positive integer . The second line contains integers () separated by single spaces; this sequence is the succinct representation of . You may assume that , that is .
Output
Print two lines. The first line contains a single positive integer . The second line contains positive integers separated by single spaces, forming the succinct representation of .