#P12419. Minimum Weight of Binary Representations in a Range

    ID: 14522 Type: Default 1000ms 256MiB

Minimum Weight of Binary Representations in a Range

Minimum Weight of Binary Representations in a Range

Given a binary string (s), we define its weight as the smallest (k) such that (s) can be partitioned into (k) substrings, with each substring consisting of identical characters. In other words, the weight of (s) is the number of contiguous groups of equal characters in (s). For example, the binary string 1011110000010000 has a weight of (6).

Now, for an integer (n) within the interval ([l, r]), consider its binary representation (without leading zeros) and let its weight be defined as above. Your task is to determine the minimum weight among the binary representations of all integers (n) in the range ([l, r]).

Note: The weight of a binary string (s) can be computed by counting the number of runs (i.e. segments where consecutive characters are the same). For a string (s = s_1s_2...s_m), the weight is (1 + \sum_{i=2}^{m} [s_i \neq s_{i-1}]), where ([P]) equals (1) if the predicate (P) is true, and (0) otherwise.

inputFormat

The input consists of a single line containing two space-separated integers \(l\) and \(r\) (with \(l \le r\)), representing the inclusive range of integers to consider.

Constraints: The range \([l, r]\) is such that a brute force solution will work within the allowed time limits.

outputFormat

Output a single integer, which is the minimum weight among the binary representations (without leading zeros) of all integers in the range \([l, r]\).

sample

1 1
1