#P10091. K-th Reduced Fraction Selection
K-th Reduced Fraction Selection
K-th Reduced Fraction Selection
You are given two sequences \(A = [a_1, a_2, \dots, a_n]\) and \(B = [b_1, b_2, \dots, b_n]\) consisting of \(n\) distinct integers each. Consider the \(n^2\) fractions of the form \(\frac{a_i}{b_j}\) for all valid \(i\) and \(j\). For each fraction, reduce it to its simplest form by dividing the numerator and denominator by their greatest common divisor. Then, sort all the \(n^2\) (not necessarily distinct) reduced fractions in increasing order (based on their numerical value).
You are also given an integer \(q\) and \(q\) queries \(c_1, c_2, \dots, c_q\). For each query \(c_i\), output the \(c_i\)-th smallest fraction from the sorted list. When printing a fraction, output it in the format p/q
where \(p\) and \(q\) are the numerator and denominator in reduced form.
Note: The fractions are counted with multiplicity even if some reduced fractions appear more than once.
inputFormat
The input consists of multiple lines:
- The first line contains two integers \(n\) and \(q\), representing the number of elements in each sequence and the number of queries respectively.
- The second line contains \(n\) space-separated integers denoting the sequence \(A\).
- The third line contains \(n\) space-separated integers denoting the sequence \(B\).
- The fourth line contains \(q\) space-separated integers \(c_1, c_2, \dots, c_q\) representing the query indices (1-indexed).
outputFormat
For each query, output a line containing the fraction in its reduced form corresponding to the query. The fraction should be printed in the format p/q
.
sample
2 2
1 2
2 3
1 4
1/3
1/1
</p>