#P8058. K-th Element in Farey Sequence
K-th Element in Farey Sequence
K-th Element in Farey Sequence
Given a positive integer \(n\), consider the Farey sequence \(F_n\) defined as the list of all proper fractions in simplest form \(\frac{a}{b}\) (with \(1 \le a < b \le n\) and \(\gcd(a,b)=1\)) arranged in increasing order. For example, for \(n=5\), the Farey sequence is:
\(\tfrac{1}{5}, \tfrac{1}{4}, \tfrac{1}{3}, \tfrac{2}{5}, \tfrac{1}{2}, \tfrac{3}{5}, \tfrac{2}{3}, \tfrac{3}{4}, \tfrac{4}{5}\)
Your task is to find the \(k\)-th smallest fraction in \(F_n\). It is guaranteed that the input parameters are such that the \(k\)-th fraction exists.
inputFormat
The input consists of a single line containing two integers \(n\) and \(k\) separated by a space, where \(n\) is the maximum value for both the numerator and denominator, and \(k\) is the 1-indexed position of the required fraction in the sorted Farey sequence of order \(n\).
outputFormat
Output the \(k\)-th smallest fraction from the Farey sequence in the format a/b
(without any additional spaces or characters).
sample
5 1
1/5