For any positive integer number , let be if is even, and if is odd. The Collatz Conjecture states that the sequence leads to 1 for any .
For example, let . We get , for a total of 12 steps. After that, the cycle 4, 2, 1 would keep repeating.
You want to make some computer analysis about the number of steps to get to 1 on several starting numbers, and leave the computer running overnight. To speed things up and avoid recalculation, you plan on storing all already computed numbers in a set. However, you realize that this would quickly exceed the memory constraints of your computer.
To avoid that, you discover LRU (Least Recently Used) caches, which can be helpful. Here’s how they work:
LRU caches have a fixed capacity . They map at most keys to their values.
Whenever a key is checked in the cache, its value will be returned if the key is present. Otherwise, the value will need to be computed, and then stored in the cache.
To store a new (key, value) when the cache is already full, the least recently used pair is evicted. We consider that we “use” a pair whenever we either store or check it.
With the help of this cache, your program will compute for some initial values in the following way:
will be computed recursively via the transformation. However, if is a cache hit (the key exists in the cache), is returned immediately. Otherwise, it’s a cache miss, and is stored in the cache once is computed.
We will never store 1 as a key in the cache, and we can assume .
For instance, this is the state of the cache for the first case of the sample input. It shows the (key, value) pairs, ordered from least to most recently used:
- After the 1st step: [] (The cache is empty.)
- After the 2nd step: [(2, 1)]
- After the 3nd step: [(2, 1)]
- After the 4th step: [(2, 1), (4, 2), (8, 3)] (Note that we can
conclude
only after we know
.)
- After the 5th step: [(16, 4), (32, 5), (64, 6)]
- After the 6th step: [(2, 1), (4, 2), (8, 3)]
- After the 7th step: [(4, 2), (8, 3), (2, 1)]
- After the 8th step: [(2, 1), (8, 3), (16, 4)]
Given and some starting values , please print and the number of cache misses to compute with the previous procedure.
Input consists of several cases. Each case starts with , followed by a non-empty sequence of positive integers ending with a 0. Assume , and that the Collatz sequence of each given will never overflow with the usual 32-bit signed integers.
For each , print , and the number of cache misses found while computing it. Suppose that the cache is empty at the beginning of each case. Print a line with 10 dashes at the end of every case.
Input
3 1 2 2 8 64 8 2 16 0 50 15 7 85 18 92 0
Output
1: 0 0 2: 1 1 2: 1 0 8: 3 2 64: 6 3 8: 3 3 2: 1 0 16: 4 1 ---------- 15: 17 17 7: 16 8 85: 9 5 18: 20 4 92: 17 1 ----------