#P1494. Colorful Socks
Colorful Socks
Colorful Socks
Small Z is tired of spending his mornings hunting through a disorganized pile of colorful socks. One day, fed up with the hassle, he decides to let fate choose his socks for him. Here is his procedure:
- He numbers his N socks from 1 to N.
- From a chosen interval [L, R] (1-indexed), he randomly picks two socks.
Although he does not mind if the socks do not form a perfect pair, he cares a great deal about their colors: wearing two socks of different colors is extremely embarrassing.
Your task is to determine the probability that the two socks he picks have the same color. If in the query L = R (i.e. the interval contains only one sock), output 0/1
as a special case.
The probability should be represented as an irreducible fraction. Note that the total number of ways to choose 2 socks from k socks is \[ C(k,2)=\frac{k(k-1)}{2} \] and the number of favorable ways (choosing two socks of the same color) is the sum over all colors, where for a color occurring n times the number of pairs is \[ C(n,2)=\frac{n(n-1)}{2}. \]
Input and output details are described below.
inputFormat
The first line contains two integers N and Q, representing the number of socks and the number of queries, respectively.
The second line contains N integers, where the i-th integer represents the color of the sock with number i.
Then Q lines follow. Each line contains two integers L and R (1-indexed) representing a query interval.
outputFormat
For each query, output the probability of picking two socks of the same color as an irreducible fraction in the format a/b
. If the interval has only one sock (L = R
), output 0/1
.
sample
5 3
1 2 1 2 1
1 3
2 5
4 4
1/3
1/3
0/1
</p>