Datasets:

Modalities:
Image
Text
Formats:
parquet
Size:
< 1K
Tags:
code
Libraries:
Datasets
pandas
License:
hackercup / 2012 /round2 /sequence_slicing.html
wjomlex's picture
2012 Problems
91a79e0 verified
raw
history blame
2.96 kB
<p>Let <b>S</b> be a sequence of <b>N</b> natural numbers. We can define an infinite sequence <b>MS</b> in the following way:
<b>MS</b>[k] = <b>S</b>[k mod <b>N</b>] + <b>N</b> * floor(k / <b>N</b>).
Where k is a zero based index. </p>
<p>For example if the sequence <b>S</b> is {2, 1, 3} then <b>MS</b> would be {2, 1, 3, 5, 4, 6, 8, 7, 9, 11, 10, 12...}</p>
<p>Now consider a subsequence of <b>MS</b> generated by picking two random indices <b>a</b>, <b>b</b> from the range [<b>0</b>..<b>R</b>] inclusive, and taking all the elements between them, that is:
<b>MS</b>[min(<b>a</b>, <b>b</b>)..max(<b>a</b>, <b>b</b>)].
<p>If we use the same <b>MS</b> as in the example above and <b>a</b> = 2, <b>b</b> = 5 then our subsequence would be {3, 5, 4, 6}.
</p>
<p>Your task is to calculate the probability that the selected subsequence has at least <b>K</b> distinct elements. <b>a</b> and <b>b</b> are selected independently and with a uniform distribution. The result should be printed as a fraction. See the "Output" section for clarification.
<h3>Input</h3>
<p>The first line of the input file contains an integer <b>T</b>. This is followed by <b>T</b> test cases, each of which has two lines.</p>
<p>The first line of each test case contains three integers separated by spaces, <b>N</b>, <b>K</b>, and <b>R</b>.<br/>
<p>The second line contains <b>N</b> space separated integers, <b>S</b>[0] through <b>S</b>[<b>N</b>-1].</p>
<h3>Constraints</h3>
<p>1 &le; <b>T</b> &le; 20<br/>
1 &le; <b>N</b> &le; 2,000<br/>
1 &le; <b>K</b> &le; <b>R</b> &le; 1,000,000,000<br/>
1 &le; <b>S</b>[i] &le; 100,000</p>
<h3>Output</h3>
<p>
For each of the test cases numbered in order from 1 to <strong>T</strong>, output "Case #<b>i</b>: " followed by the probability that the selected subsequence of <b>MS</b> has at least <b>K</b> distinct elements. The probability should be expressed as a fraction <b>p</b>/<b>q</b>, where <b>p</b> and <b>q</b> represent the numerator and denominator respectively and are relatively prime (that is they share no common positive divisors except 1).
</p>
<p>If the probability is 0 or 1 output 0/1 or 1/1 respectively.</p>
<h3>Examples</h3>
<p>In the first example there are 36 different subsequences to consider. 6 of them have only a single number, and the remaining 30 have at least 2 different numbers, so the answer is 5/6.
</p>
<p>
The second example is similar, but now the sequence looks like {2, 1, 5, 5, 4, 8}. There are 8 subsequences with less than 2 distinct numbers: the six single number subsequences plus (a=2, b=3) and (a=3, b=2) which both result in {5,5}. That gives a probability of (36 - 8) / 36 = 7/9.
</p>
<p>
The third example uses the same sequence as the second example, but now we want to have subsequences with at least 4 different numbers. All pairs of indices that have this property are: (0,4), (0, 5), (1, 5), (4, 0), (5, 0), and (5, 1). Six out of thirty six results in a probability of 1/6.
</p>