Negative Base
Time limit2sMemory limit512 MB
Find the integer of least absolute value whose negabinary (base -2) representation contains at least k consecutive zeroes, breaking ties by shortest representation.
- Level
Medium6 of 10
- Topics
- Math, Bit manipulation, Brute force, Implementation
- Solved
- No attempts yet
Problem
The binary positional numeral system works as follows. Writing a nonnegative integer as a string "" of zeroes and ones means . Leading zeroes are omitted, so , except when , which is written as "".
The negabinary notation works in a similar way. Writing a number as a string "" of zeroes and ones means . Leading zeroes are omitted here as well, so , except when , which is written as "". This notation has a unique representation for every integer, not only for nonnegative integers.
Negabinary notation of the numbers from to :
Given an integer , find a number whose negabinary representation contains at least consecutive zeroes. Among such numbers, find the one with the least absolute value. If several answers remain, pick the one with the shortest negabinary representation.
Input
The first line of input contains an integer ().
Output
Print one integer: the answer to the problem.