All problems

Problem 877

Sort Integers by The Power Value

Medium · 20 pts · Sorting, Dynamic Programming, Memoization

Problem

The power of an integer x is the number of steps needed to turn it into 1 by repeating: if x is even, x becomes x / 2, otherwise x becomes 3x + 1. Sort the integers from LO to HI by their power, breaking ties by the integer itself, and print the K-th one (counting from 1). Memoising the powers you have already computed makes this fast even though the sequences can climb high.

Input

A single line with LO, HI and K.

Output

Print the K-th integer in the sorted order.

Constraints

1 <= LO <= HI <= 1000; 1 <= K <= HI - LO + 1

Example input

12 15 2

Example output

13

Explanation

For 12 to 15, the powers are 12 → 9, 13 → 9, 14 → 17 and 15 → 17. Sorted by power then value, the order is 12 13 14 15, and the second one is 13.

Every problem here can be used in a RelayTrixCode team match. Create a room.

Previous: Search Suggestions System Next: Reorder Data in Log Files