This page is still under construction.

Parts of this page are still being built. What you see may change.

Pyro Tubes

Interview

Time limit13sMemory limit256 MB

Summary
For each value in a sorted list, count larger list values whose 18-bit patterns differ in at most two bits.
Level

Medium5 of 10

Topics
Bit manipulation, Hash map
Solved
No attempts yet

Problem

A rock festival stage carries 18 pyro tubes, numbered 1 to 18. Each tube fires one flame burst of a fixed luminosity.

Figure 1: tube numbering

Tube 1 produces a luminosity of 1. For N>1N > 1, tube NN produces double the luminosity of tube N−1N-1, so tube NN produces 2N−12^{N-1}.

To reach a wanted luminosity, several tubes fire at the same time. The luminosity achieved is the sum of the luminosities of the tubes that fire, written as a single integer LL. The control software takes LL as input and decides which tubes to activate and which to leave deactivated.

Figure 2: tube states

For a given LL there is exactly one set of activated tubes that produces it.

A sticky valve or a clogged tube sometimes stops the system from firing the requested set. The crew then activates and deactivates tubes to fire a different set. Switching one tube from activated to deactivated, or the other way round, counts as one state change.

Figure 3: the requested tube states (left) and a set 2 state changes away (right)

The two groups in figure 3 differ in two places. Tube 7 is activated on the left and deactivated on the right, and tube 8 is deactivated on the left and activated on the right.

Figure 4: the requested tube states (left) and a set 1 state change away (right)

The left side of figure 4 shows the tube states for a requested luminosity of 45393. On the right, tube 10 is activated as well, which gives a luminosity of 45905.

The producers accept a different luminosity as long as it is brighter than the original one. They give the list of LL values scheduled for the show, and for each original value in the list you count the alternative LL values that meet all three requirements below.

  1. The alternative value is greater than the original value.
  2. The alternative value is another value in the given list.
  3. The alternative value can be reached from the original set by changing at most 2 tube states.

Input

The scheduled LL values are given one per line in increasing order. Each value satisfies 1≤L≤2500001 \le L \le 250000, so there are at most 250000 values. A single -1 on the last line ends the input.

Output

For each original LL value, in the order given, print one line of the form L:C, where LL is the originally requested luminosity and CC is the number of alternative values that meet the three requirements above. Do not put spaces around the colon.

Some strategies produce correct results but do not finish inside the time limit, so an efficient algorithm is needed.

Examples2

  1. Example 1

    Input
    1
    3
    8
    10
    25
    -1
    
    Expected output
    1:3
    3:1
    8:2
    10:0
    25:0
    
  2. Example 2

    Input
    2083
    15093
    15285
    25147
    31413
    47797
    49723
    55989
    58171
    60085
    95670
    -1
    
    Expected output
    2083:0
    15093:3
    15285:2
    25147:2
    31413:3
    47797:2
    49723:1
    55989:1
    58171:0
    60085:0
    95670:0