
ZOJ Problem Set  1665
Rational numbers are numbers represented by ratios of two integers. For a prime number p, one of the elementary theorems in the number theory is that there is no rational number equal to the square root of p (abbreviated to SQRT(p)). Such numbers are called irrational numbers. It is also known that there are rational numbers arbitrarily close to SQRT(p). Now, given a positive integer n, we define a set Qn of all rational numbers whose elements are represented by ratios of two positive integers both of which are less than or equal to n. For example, Q4 is a set of 11 rational numbers {1/1, 1/2, 1/3, 1/4, 2/1, 2/3, 3/1, 3/2, 3/4, 4/1, 4/3}. 2/2, 2/4, 3/3, 4/2 and 4/4 are not included here because they are equal to 1/1, 1/2, 1/1, 2/1 and 1/1, respectively. Your job is to write a program that reads two integers p and n and reports two rational numbers x/y and u/v, where u/v < SQRT(p) < x/y and there are no other elements of Qn between u/v and x/y. When n is greater than SQRT(p), such a pair of rational numbers always exists.
Source: Asia 1999, Kyoto (Japan) 