Persistent Bits
Time limit1sMemory limit128 MB
Starting from a seed S, repeatedly apply (A*X+B) mod C and report, for each of the 16 bit positions, whether it is always 1, always 0, or varies.
- Level
Medium4 of 10
- Topics
- Bit manipulation, Simulation, Hash map
- Solved
- No attempts yet
Problem
WhatNext Software builds sequence generators that they hope will produce fairly random sequences of 16-bit unsigned integers in the range 0 to 65535. A sequence is specified by integers , , , and , where , , , and . is the first element (the seed) of the sequence, and each later element is generated from the previous one. If is an element of the sequence, then the next element is
where is the remainder operation. Although every element of the sequence is a 16-bit unsigned integer less than 65536, the intermediate result may be larger, so the calculation should use a 32-bit integer rather than a 16-bit one to stay accurate.
Some parameter values produce better sequences than others. The most embarrassing sequences are those that never change one or more bits. A bit that never changes throughout the sequence is called persistent. Ideally a sequence has no persistent bits. Your job is to examine a sequence and determine which bits are persistent.
For example, , , , is a particularly bad choice. It produces the sequence 3, , , , , , and then again, returning to the start. So the sequence repeats the same six values over and over:
The last row of the table shows which bit positions are always 0, always 1, or take on both values in the sequence. Note that 12 of the 16 bits are persistent. (Good random sequences have no persistent bits, but the converse is not necessarily true: the sequence defined by , , , has no persistent bits, yet it is not random either — it simply counts from 0 to 63999 before repeating.) A sequence need not return to its seed: with , , , the sequence goes 2, 4, 8, 0, 0, 0, ....
Input
The input contains from one to sixteen datasets, followed by a line containing only 0. Each dataset is a single line containing decimal integer values for , , , and , separated by single blanks.
Output
Print one line of output for each dataset. Each line contains 16 characters — either '1', '0', or '?' for each of the 16 bits in order, most significant bit first. '1' means the corresponding bit is always 1, '0' means it is always 0, and '?' means the bit takes on both 0 and 1 in the sequence.