For each query with m and fraction a/b, find primes p and q with pq <= m and a/b <= p/q <= 1 that maximize the product pq.
Medium5Number theoryMathBrute forceImplementationInterviewNo attempts yetTime limit2sMemory limit512 MBOn the afternoon of Saturday, November 16, 1974, a message from humans to extraterrestrial intelligence was sent through the Arecibo radio telescope in Puerto Rico. The message consisted of 1679 bits and was meant to be read as a rectangular picture of 23 × 73 pixels. Since both 23 and 73 are prime numbers, 23 × 73 is the only possible size of the picture in which each edge is longer than 1 pixel. Of course, there was no guarantee that the receivers would try to read the message as a rectangular picture. Even if they did, they might place the pixels into the rectangle incorrectly. The senders of the Arecibo message were optimistic.
We are planning a similar project. Your task in the project is to find the most suitable width and height of the decoded rectangular picture. "Most suitable" is defined as follows. An integer m greater than 4 is given. A positive fraction a/b less than or equal to 1 is also given. The area of the picture must not be greater than m. Both the width and the height of the picture must be prime numbers. The ratio of the width to the height must be neither less than a/b nor greater than 1. You have to maximize the area of the picture under these constraints.
In other words, you receive an integer m and a fraction a/b with m>4 and 0<a/b≤1. Find the pair of prime numbers p, q such that pq≤m and a/b≤p/q≤1, and the product pq is the maximum among all such pairs. Report p and q as the "most suitable" width and height of the picture.
The input is a sequence of at most 2000 triplets of positive integers. Each line contains one triplet, with a single space between the integers. The sequence is followed by a line with the triplet 0 0 0, which marks the end of the input and is not to be processed.
The three integers of a triplet are the integer m, the numerator a, and the denominator b described above, in this order. It holds that 4<m<100000 and 1≤a≤b≤1000.
Print one line for each input triplet, in the same order. The i-th output line answers the i-th input triplet and contains the width p and the height q described above, in this order, separated by a single space. Print no other characters.