This is an encoding problem: agree in advance on a family of candidate subsets, find one contained in the input set , and transmit its identifier. The second execution reconstructs the selected subset from that identifier alone.
Let and . Subtract from every input value, so that the universe is .
Shared permutations and buckets
Prepare permutations , reproducible in both executions. The implementation initializes with a fixed seed, repeatedly shuffles the same array, and stores a copy after each shuffle. The seed is fixed independently of the input and test data.
For each permutation, map every to , and divide the universe into buckets of size . There are
buckets. A mapped value is represented by its bucket and its position inside that bucket:
If a bucket contains at least input elements, consider its -element subsets. For sorted positions
define the colexicographic combination rank
This is a bijection between the -element subsets of a bucket and the integers , where
Fitting the message into 64 bits
Encoding the permutation, bucket, and unrestricted combination rank would require codes, which exceeds the available range. We therefore allow only some combinations in each permutation--bucket pair.
Reserve messages for directly transmitting one original input value. Message is unused. Allocate the remaining space equally among the pairs, with
codes per pair. Combine the two indices as .
For each , deterministically generate coefficients from a second fixed seed and . Choose
The implementation uses SplitMix64 and retries the choice of until it is coprime to . Both executions reproduce the same coefficients, so they need not be transmitted.
Transform a combination rank by
This map is invertible modulo . Accept the candidate only when , and transmit
Consequently,
The invertible affine filter distributes the permitted ranks more broadly than simply accepting the smallest original ranks.
The encoder visits permutations and buckets in order. In each bucket containing input elements, it enumerates all subsets if necessary, stopping immediately when one passes the filter. There is no cutoff on the number of combinations examined. If every candidate in all permutations fails, transmit one element of in its original, one-based form. This is the one-element fallback.
Decoding
For a message produced by the encoder, identifies the fallback value directly. Otherwise, recover
and then
Regenerate the coefficients for , and invert the filter:
The inverse is taken modulo , not modulo . Normalize the subtraction before multiplying by the inverse.
Unrank the combination in reverse order, from down to . Choose the largest permitted satisfying
subtract this binomial coefficient from , and restrict the next position to values smaller than . Binary search finds each position. Finally, map each through and add to restore the original numbering.
Invertibility of the rank and affine maps ensures that a successful search reconstructs exactly the selected distinct input elements. The fallback reconstructs one input element. Thus every reconstructed value belongs to .
Precompute the binomial coefficients up to . These coefficients, ranks, and messages fit in unsigned 64-bit integers. Products such as , modular multiplication during decoding, and message construction use intermediates. The extended Euclidean algorithm for the modular inverse uses signed 128-bit intermediates.
Estimated frequency of recovering nine elements
For an estimate, idealize the shared permutations as independent uniform random permutations. Also approximate each affine permutation of combination ranks by a uniform random permutation of all ranks. The latter is an approximation: the affine family does not contain every permutation of the rank space.
Under the uniform-shuffle model, the number of input elements in a particular bucket has the hypergeometric distribution
If the bucket contains elements, it offers candidate subsets; take for . In the uniform-rank-permutation model, the probability that all candidates fail is
Hence the estimated probability that a particular bucket provides an encodable subset is
If we additionally ignore dependencies between buckets and trials, the estimated failure probability over all pairs is
giving an estimated nine-element recovery rate of about
This numerical estimate relies on the uniformity and independence approximations above. With fixed seeds, the implementation is deterministic; the estimate is not a worst-case guarantee of recovering nine elements for every valid . When no candidate passes the filter, the encoder uses the valid one-element fallback.
mt19937\_64unsigned \_\_int128