Problem:

Problem A: Calling Extraterrestrial Intelligence Again

A message from humans to extraterrestrial intelligence was sent through the Arecibo radio telescope in Puerto Rico on the afternoon of Saturday November l6, l974. The message consisted of l679 bits and was meant to be translated to a rectangular picture with 23 &times; 73  pixels. Since both 23 and 73 are prime numbers, 23 &times; 73  is the unique possible size of the translated rectangular picture each edge of which is longer than l pixel. Of course, there was no guarantee that the receivers would try to translate the message to a rectangular picture. Even if they would, they might put 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 translated rectangular picture. The term ``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 should not be greater than m. Both of the width and the height of the translated picture should be prime numbers. The ratio of the width to the height should not be less than a/b nor greater than 1. You should maximize the area of the picture under these constraints.

In other words, you will receive an integer m and a fraction a/b . It holds that m > 4 and 0 < a/b ≤ 1 . You should find the pair of prime numbers p, q such that pq ≤ m and a/b ≤ p/q ≤ 1 , and furthermore, the product pq takes the maximum value among such pairs of two prime numbers. You should report p and q as the "most suitable" width and height of the translated picture. 

Input

The input is a sequence of at most 2000 triplets of positive integers, delimited by a space character in between. Each line contains a single triplet. The sequence is followed by a triplet of zeros, 0 0 0, which indicates the end of the input and should not be treated as data to be processed.

The integers of each input triplet are the integer m, the numerator a, and the denominator b described above, in this order. You may assume 4 < m < 100000 and 1 ≤ a ≤ b ≤ 1000.

Output

The output is a sequence of pairs of positive integers. The i-th output pair corresponds to the i-th input triplet. The integers of each output pair are the width p and the height q described above, in this order.

Each output line contains a single pair. A space character is put between the integers as a delimiter. No other characters should appear in the output. 

Sample Input

5 1 2
99999 999 999
1680 5 16
1970 1 1
2002 4 11
0 0 0

Output for the Sample Input

2 2
313 313
23 73
43 43
37 53



Sample input:
5 1 2
99999 999 999
1680 5 16
1970 1 1
2002 4 11
0 0 0


Sample output:
2 2
313 313
23 73
43 43
37 53

Submission status: Wrong Answer

Solution code:
import math
def sieve (n):
	prime = [0,0]
	prime += [1 for i in range(n-1)]
	ub = math.sqrt(n) + 1
	d = 2
	while d <= ub:
		if prime[d] == 0:
			d += 1
			continue
		prod = 2
		while d * prod <= n:
			prime[d*prod] = 0
			prod += 1
		d += 1
	return prime
prime = sieve(1000)
while 1:
	m,a,b = map(int,raw_input().split(" "))
	if (m,a,b) == (0,0,0):
		break
	rect_max = (0,0,0)
	q = 997
	while q >= 2:
		if prime[q] == 0:
			q -= 1
			continue
		p = min(m / q, q)
		while p * b >= q * a:
			if prime[p] == 0:
				p -= 1
				continue
			if rect_max[0] <= p*q:
				rect_max = (p*q,p,q)
			p -= 1
		q -= 1
	print rect_max[1], rect_max[2]

Submission status: Wrong Answer

Solution code:
import math
def sieve (n):
	prime = [0,0]
	prime += [1 for i in range(n-1)]
	ub = math.sqrt(n) + 1
	d = 2
	while d <= ub:
		if prime[d] == 0:
			d += 1
			continue
		prod = 2
		while d * prod <= n:
			prime[d*prod] = 0
			prod += 1
		d += 1
	return prime
prime = sieve(1000)
while 1:
	m,a,b = map(int,raw_input().split(" "))
	if (m,a,b) == (0,0,0):
		break
	rect_max = (0,0,0)
	q = 997
	while q >= 2:
		if prime[q] == 0:
			q -= 1
			continue
		p = min(m / q + 1, q)
		while p * b >= q * a:
			if prime[p] == 0 or p * q > m:
				p -= 1
				continue
			if rect_max[0] <= p*q:
				rect_max = (p*q,p,q)
			p -= 1
		q -= 1
	print rect_max[1], rect_max[2]

Submission status: Wrong Answer

Solution code:
import math
def sieve (n):
	prime = [0,0]
	prime += [1 for i in range(n-1)]
	ub = math.sqrt(n) + 1
	d = 2
	while d <= ub:
		if prime[d] == 0:
			d += 1
			continue
		prod = 2
		while d * prod <= n:
			prime[d*prod] = 0
			prod += 1
		d += 1
	return prime
prime = sieve(1000)
while 1:
	m,a,b = map(int,raw_input().split(" "))
	if (m,a,b) == (0,0,0):
		break
	rect_max = (0,0,0)
	q = 997
	while q >= 1:
		if prime[q] == 0:
			q -= 1
			continue
		p = min(m / q + 1, q)
		while p * b >= q * a:
			if prime[p] == 0 or p * q > m:
				p -= 1
				continue
			if rect_max[0] <= p*q:
				rect_max = (p*q,p,q)
			p -= 1
		q -= 1
	print rect_max[1], rect_max[2]

Submission status: Wrong Answer

Solution code:
import bisect
R = int(100001**0.5)
p = [1]*R
p[0] = p[1] = 0
for i in range(2,int(R**0.5)+1):
    if p[i]:
        p[2*i::i] = [0]*len(p[2*i::i])
p = [i for i in range(R) if p[i]]
n = len(p)
pxp = sorted([[p[i]*p[j],p[i],p[j]] for i in range(n) for j in range(i,n)])
p,xy = [ele[0] for ele in pxp],[[ele[1],ele[2]] for ele in pxp]

while 1:
    m,a,b = map(int,raw_input().split())
    if m == 0: break
    ab = a*1.0/b

    sp = bisect.bisect(p,m)
    for i in range(sp)[::-1]:
        x,y = xy[i]
        if 1 >= x*1.0/y >= ab:
            print x,y
            break

Submission status: Wrong Answer

Solution code:
import bisect
R = 100001/2
p = [1]*R
p[0] = p[1] = 0
for i in range(2,int(R**0.5)+1):
    if p[i]:
        p[2*i::i] = [0]*len(p[2*i::i])
p = [i for i in range(R) if p[i]]
n = len(p)
pxp = []
for i in range(n):
    for j in range(i,n):
        if p[i]*p[j] > 100000: break
        pxp.append([p[i]*p[j],p[i],p[j]])
pxp = sorted(pxp)
p,xy = [ele[0] for ele in pxp],[[ele[1],ele[2]] for ele in pxp]

while 1:
    m,a,b = map(int,raw_input().split())
    if m == 0: break
    ab = a*1.0/b

    sp = bisect.bisect(p,m)
    for i in range(sp)[::-1]:
        x,y = xy[i]
        if 1 >= x*1.0/y >= ab:
            print x,y
            break
print 193*503

Submission status: Wrong Answer

Solution code:
import bisect
R = 100001/2
p = [1]*R
p[0] = p[1] = 0
for i in range(2,int(R**0.5)+1):
    if p[i]:
        p[2*i::i] = [0]*len(p[2*i::i])
p = [i for i in range(R) if p[i]]
n = len(p)
pxp = []
for i in range(n):
    for j in range(i,n):
        if p[i]*p[j] > 100000: break
        pxp.append([p[i]*p[j],p[i],p[j]])
pxp = sorted(pxp)
p,xyr = [ele[0] for ele in pxp],[[ele[1],ele[2],ele[1]*1.0/ele[2]] for ele in pxp]

while 1:
    m,a,b = map(int,raw_input().split())
    if m == 0: break
    ab = a*1.0/b

    sp = bisect.bisect(p,m)
    for i in range(sp)[::-1]:
        if 1 >= xyr[2] >= ab:
            print xyr[0],xyr[1]
            break

Submission status: Wrong Answer

Solution code:
MAXN = 320
sieve = [0,0] + [1]*MAXN
primes = []
p = 2
while p*p <= MAXN:
    if sieve[p]:
        primes.append(p)
        for q in range(p*2,MAXN+1,p):
            sieve[q] = 0
    p += 1
for q in range(p,MAXN):
    if sieve[q]:
        primes.append(q)

while True:
    M,A,B = map(int,input().split())
    if M == 0: break
    opt_p = opt_q = 0
    for p in primes:
        for q in primes:
            if p > q: continue
            if p*q > M: break
            if A*q > B*p: continue
            if p*q > opt_p*opt_q:
                opt_p,opt_q = p,q
    print('{0} {1}'.format(opt_p,opt_q))

Submission status: Wrong Answer

Solution code:
import math,string,itertools,fractions,heapq,collections,re,array,bisect,sys,random,time,copy,functools

sys.setrecursionlimit(10**7)
inf = 10**20
eps = 1.0 / 10**10
mod = 998244353

def LI(): return [int(x) for x in sys.stdin.readline().split()]
def LI_(): return [int(x)-1 for x in sys.stdin.readline().split()]
def LF(): return [float(x) for x in sys.stdin.readline().split()]
def LS(): return sys.stdin.readline().split()
def I(): return int(sys.stdin.readline())
def F(): return float(sys.stdin.readline())
def S(): return input()
def pf(s): return print(s, flush=True)


class Prime():
    def __init__(self, n):
        self.M = m = int(math.sqrt(n)) + 10
        self.A = a = [True] * m
        a[0] = a[1] = False
        self.T = t = []
        for i in range(2, int(math.sqrt(m)) + 1):
            if not a[i]:
                continue
            t.append(i)
            for j in range(i*i,m,i):
                a[j] = False

    def is_prime(self, n):
        return self.A[n]

def main():
    rr = []
    pr = Prime(100000**2)
    tl = len(pr.T)

    while True:
        n,a,b = LI()
        if n == 0:
            break
        m = fractions.Fraction(a,b)
        sn = n ** 0.5
        r = 0
        rs = ''
        for ni in range(tl):
            i = pr.T[ni]
            if i > sn:
                break
            for nj in range(ni,tl):
                j = pr.T[nj]
                if i*j > n:
                    break
                if i*j <= r:
                    continue
                if fractions.Fraction(i,j) < m:
                    continue
                r = i*j
                rs = '{} {}'.format(i,j)

        rr.append(rs)

    return '\n'.join(map(str, rr))


print(main())

Submission status: Wrong Answer

Solution code:
import math,string,itertools,fractions,heapq,collections,re,array,bisect,sys,random,time,copy,functools

sys.setrecursionlimit(10**7)
inf = 10**20
eps = 1.0 / 10**10
mod = 998244353

def LI(): return [int(x) for x in sys.stdin.readline().split()]
def LI_(): return [int(x)-1 for x in sys.stdin.readline().split()]
def LF(): return [float(x) for x in sys.stdin.readline().split()]
def LS(): return sys.stdin.readline().split()
def I(): return int(sys.stdin.readline())
def F(): return float(sys.stdin.readline())
def S(): return input()
def pf(s): return print(s, flush=True)


class Prime():
    def __init__(self, n):
        self.M = m = int(math.sqrt(n)) + 10
        self.A = a = [True] * m
        a[0] = a[1] = False
        self.T = t = []
        for i in range(2, int(math.sqrt(m)) + 1):
            if not a[i]:
                continue
            t.append(i)
            for j in range(i*i,m,i):
                a[j] = False

    def is_prime(self, n):
        return self.A[n]

def main():
    rr = []
    pr = Prime(100000**2)
    tl = len(pr.T)

    while True:
        n,a,b = LI()
        if n == 0:
            break
        m = fractions.Fraction(a,b)
        sn = n ** 0.5
        r = 0
        rs = ''
        for ni in range(tl):
            i = pr.T[ni]
            if i > sn:
                break
            j = n // i
            while not pr.is_prime(j):
                j -= 1
            if i*j <= r or fractions.Fraction(i,j) < m:
                continue
            r = i*j
            rs = '{} {}'.format(i,j)

        rr.append(rs)

    return '\n'.join(map(str, rr))


print(main())

Submission status: Accepted

Solution code:
import math
def sieve (n):
	prime = [0,0]
	prime += [1 for i in range(n-1)]
	ub = math.sqrt(n) + 1
	d = 2
	while d <= ub:
		if prime[d] == 0:
			d += 1
			continue
		prod = 2
		while d * prod <= n:
			prime[d*prod] = 0
			prod += 1
		d += 1
	return prime
prime = sieve(50000)
while 1:
	m,a,b = map(int,raw_input().split(" "))
	if (m,a,b) == (0,0,0):
		break
	rect_max = (0,0,0)
	q = m / 2
	while q >= 2:
		if prime[q] == 0:
			q -= 1
			continue
		p = min(m / q + 1, q)
		while p * b >= q * a:
			if prime[p] == 0 or p * q > m:
				p -= 1
				continue
			if rect_max[0] <= p*q:
				rect_max = (p*q,p,q)
			p -= 1
		q -= 1
	print rect_max[1], rect_max[2]

Submission status: Accepted

Solution code:
import bisect
R = 100001/2
p = [1]*R
p[0] = p[1] = 0
for i in range(2,int(R**0.5)+1):
    if p[i]:
        p[2*i::i] = [0]*len(p[2*i::i])
p = [i for i in range(R) if p[i]]
n = len(p)
pxp = []
for i in range(n):
    for j in range(i,n):
        if p[i]*p[j] > 100000: break
        pxp.append([p[i]*p[j],p[i],p[j]])
pxp = sorted(pxp)
p,xy = [ele[0] for ele in pxp],[[ele[1],ele[2]] for ele in pxp]

while 1:
    m,a,b = map(int,raw_input().split())
    if m == 0: break
    ab = a*1.0/b

    sp = bisect.bisect(p,m)
    for i in range(sp)[::-1]:
        x,y = xy[i]
        if 1 >= x*1.0/y >= ab:
            print x,y
            break

Submission status: Accepted

Solution code:
import bisect
R = 100001/2
p = [1]*R
p[0] = p[1] = 0
for i in range(2,int(R**0.5)+1):
    if p[i]:
        p[2*i::i] = [0]*len(p[2*i::i])
p = [i for i in range(R) if p[i]]
n = len(p)
pxp = []
for i in range(n):
    for j in range(i,n):
        if p[i]*p[j] > 100000: break
        pxp.append([p[i]*p[j],p[i],p[j]])
pxp = sorted(pxp)
p,xyr = [e[0] for e in pxp],[[e[1],e[2],e[1]*1.0/e[2]] for e in pxp]
while 1:
    m,a,b = map(int,raw_input().split())
    if m == 0: break
    ab = a*1.0/b

    sp = bisect.bisect(p,m)
    for i in range(sp)[::-1]:
        if 1 >= xyr[i][2] >= ab:
            print xyr[i][0],xyr[i][1]
            break

Submission status: Accepted

Solution code:
def f(m,a,b,p):
    for i in range(m,0,-1):
        for j in range(2,-~int(i**.5)):
            if p[j] and i%j==0:
                q=i//j
                if a*q<=b*j and j<=q and p[q]:print(j,q);return
n=100000
p=[0]*2+[1]*(n-2)
for i in range(2,-~int(n**.5)):
    if p[i]:
        for j in range(i*i,n,i):
            p[j]=0
while 1:
    m,a,b=map(int,input().split())
    if m==0:break
    f(m,a,b,p)

Submission status: Accepted

Solution code:
import math,string,itertools,fractions,heapq,collections,re,array,bisect,sys,random,time,copy,functools

sys.setrecursionlimit(10**7)
inf = 10**20
eps = 1.0 / 10**10
mod = 998244353

def LI(): return [int(x) for x in sys.stdin.readline().split()]
def LI_(): return [int(x)-1 for x in sys.stdin.readline().split()]
def LF(): return [float(x) for x in sys.stdin.readline().split()]
def LS(): return sys.stdin.readline().split()
def I(): return int(sys.stdin.readline())
def F(): return float(sys.stdin.readline())
def S(): return input()
def pf(s): return print(s, flush=True)


class Prime():
    def __init__(self, n):
        self.M = m = int(math.sqrt(n)) + 10
        self.A = a = [True] * m
        a[0] = a[1] = False
        self.T = t = []
        for i in range(2, m):
            if not a[i]:
                continue
            t.append(i)
            for j in range(i*i,m,i):
                a[j] = False

    def is_prime(self, n):
        return self.A[n]

def main():
    rr = []
    pr = Prime(100000**2)
    tl = len(pr.T)

    while True:
        n,a,b = LI()
        if n == 0:
            break
        m = fractions.Fraction(a,b)
        sn = n ** 0.5
        r = 0
        rs = ''
        for ni in range(tl):
            i = pr.T[ni]
            if i > sn:
                break
            j = min(n // i, int(i/m)+1)
            if j < i:
                continue
            while not pr.is_prime(j):
                j -= 1
            ji = pr.T.index(j)
            while fractions.Fraction(i,pr.T[ji]) < m:
                ji -= 1
            j = pr.T[ji]
            if i*j <= r:
                continue
            r = i*j
            rs = '{} {}'.format(i,j)

        rr.append(rs)

    return '\n'.join(map(str, rr))


print(main())

Submission status: Accepted

Solution code:
import sys
import bisect


MAX = 100001
prime = [-1] * MAX
i = 2
p = []
while MAX > i:
	if prime[i - 1] == -1:
		prime[i - 1] = 0
		p.append(i)
		temp = i * 2
		while temp < MAX:
			prime[temp - 1] = 1
			temp += i
	if i == 2:
		i += 1
	else:
		i += 2


while True:
    m, a, b = map(int, sys.stdin.readline().split())
    if m == 0:
        break
    cand = (-1, -1, -1)
    for q in p:
        idx = bisect.bisect_right(p, min(m / q, q)) - 1
        if idx == -1:
            continue
        value = p[idx]
        if value >= float(a) / b * q and cand[0] < q * value and value <= q:
            cand = (q * value, value, q)
    print cand[1], cand[2]

Submission status: Accepted

Solution code:
#!/usr/bin/env python
# -*- coding: utf-8 -*-
"""
Problems 1232
Problem A: Calling Extraterrestrial Intelligence Again
"""
import math
def eratosthenes(n):
    """
    エラトステネスのふるい
    """
    primes = [i for i in range(n+1)]
    primes[1] = 0 # 1は素数ではない
    for prime in primes:
        if prime > math.sqrt(n):
            break
        if prime == 0:
            continue
        for non_prime in range(2 * prime, n, prime): # primeの分、合成数をふるいかけていく
            primes[non_prime] = 0 # 素数でない要素は0にする
    prime_list = []
    for prime in primes:
        if prime != 0:
            prime_list.append(prime)
    return prime_list

# 素数を生成
primes = eratosthenes(100000)
while True:
    m, a, b = map(int, input().split())
    if m <= 4 or a == 0 or b == 0:
        break
    p = 0
    q = 0
    max_s = 0
    for i in range(len(primes)):
        for j in range(i, len(primes)):
            if (primes[i]*primes[j] <= m) and (a / b <= primes[i] / primes[j] <= 1):
                if primes[i]*primes[j] >= max_s:
                    max_s = primes[i]*primes[j]
                    p = primes[i]
                    q = primes[j]
            else:
                break
    print("{0} {1}".format(p, q))

Submission status: Accepted

Solution code:
# エラトステネスの篩
def Eratosthenes(N):
    ans = [True for i in range(N + 1)]
    ans[0], ans[1] = False, False
    
    for i in range(int(N ** (1 / 2)) + 1):
        if ans[i]:
            for j in range(2 * i, N, i):
                ans[j] = False
    
    return [i for i in range(N + 1) if ans[i]]

ans = [] # 答え
while True:
    M, A, B = map(int,input().split())
    if not M:
        break
    
    AdivB = A / B + 0.0000001
    
    prime_list = Eratosthenes(M)
    tmp_max = 0
    tmp_ans = [0, 0]
    
    # 必要な部分だけ探索
    for i in range(len(prime_list)):
        for j in range(i + 1, len(prime_list)):
            if prime_list[i] / prime_list[j] < AdivB or prime_list[i] * prime_list[j] > M:
                if tmp_max < prime_list[i] * prime_list[j - 1] and prime_list[i] * prime_list[j - 1] <= M:
                    tmp_max = prime_list[i] * prime_list[j - 1]
                    tmp_ans = [prime_list[i], prime_list[j - 1]]
                break

    ans.append(tmp_ans)

# 出力
[print(i[0], i[1]) for i in ans]

Submission status: Accepted

Solution code:
answer = []


def prime(n):
    prime_flag = [0 for i in range(n + 1)]
    prime_list = []
    for i in range(2, n + 1):
        if prime_flag[i] == 0:
            prime_list.append(i)

            temp = 1
            while temp * i <= n:
                prime_flag[temp * i] = 1
                temp += 1
    return prime_list


prime_list = prime(100000)

while True:

    m, a, b = map(int, input().split())
    if m == a == b == 0:
        break
    rate = a / b
    ans = [0, 0]
    for i in prime_list:
        if i > m:
            break
        for j in prime_list:
            if i * j <= m:
                if i / j >= rate and i / j <= 1:
                    if ans[0] * ans[1] < i * j:
                        ans = [i, j]
            else:
                break

    answer.append(ans)

for i in answer:
    print(*i)

Submission status: Accepted

Solution code:
MAX = 100000
isPrime = [True]*(MAX+1)
isPrime[0] = False
isPrime[1] = False
for i in range(2,int((MAX)**0.5)):
    if isPrime[i]:
        for j in range(i*i, MAX+1, i):
            isPrime[j] = False

primes = [x for x in range(MAX+1) if isPrime[x]]

while True:
    m, a, b = map(int, input().split())
    if m == 0:
        break
    p=2
    q=2
    for x in primes:
        for y in primes:
            if x < y or x*y > m:
                break
            if a*x <= b*y and x*y > p*q:
                p = y
                q = x
    print(p,q)

Submission status: Accepted

Solution code:
def main(m, a, b):
    ans = [0,0,0]
    for x in p:
        for y in p:
            if x * y > m:
                break
            if 1 >= x / y >= a / b and x * y > ans[0]:
                ans = [x * y, x, y]
    print(ans[1], ans[2])

def primes(n):
    """ 
    Input n>=6, Returns a list of primes, 2 <= p < n 
    6以上の数であれば p (素数) <= n のlistを返す 
    :param int n:
    :rytpe list:
    """
    n, correction = n-n%6+6, 2-(n%6>1)
    sieve = [True] * (n//3)
    for i in range(1,int(n**0.5)//3+1):
        if sieve[i]:
            k=3*i+1|1
            sieve[      k*k//3      ::2*k] = [False] * ((n//6-k*k//6-1)//k+1)
            sieve[k*(k-2*(i&1)+4)//3::2*k] = [False] * ((n//6-k*(k-2*(i&1)+4)//6-1)//k+1)
    return [2,3] + [3*i+1|1 for i in range(1,n//3-correction) if sieve[i]]

if __name__ == "__main__":
    p = primes(10000)
    while 1:
        m, a, b = map(int, input().split())
        if m == a == b == 0:
            break
        else:
            main(m,a,b)

Submission status: Accepted

Solution code:
#!usr/bin/env python3
from collections import defaultdict,deque
from heapq import heappush, heappop
import sys
import math
import bisect
import random
def LI(): return [int(x) for x in sys.stdin.readline().split()]
def I(): return int(sys.stdin.readline())
def LS():return [list(x) for x in sys.stdin.readline().split()]
def S(): return list(sys.stdin.readline())[:-1]
def IR(n):
    return [I() for i in range(n)]
def LIR(n):
    return [LI() for i in range(n)]
def SR(n):
    return [S() for i in range(n)]
def LSR(n):
    return [LS() for i in range(n)]

sys.setrecursionlimit(1000000)
mod = 1000000007

M = 10000
l = [1]*(M+1)
prime = []
p = 2
while p <= M:
    while p <= M and not l[p]:
        p += 1
    j = p
    while j <= M:
        l[j] = 0
        j += p
    prime.append(p)
l = len(prime)
def solve(m,a,b):
    ans = [0,0,0]
    for i in range(l):
        p = prime[i]
        k = min(l,bisect.bisect_right(prime,p*b/a)+1,bisect.bisect_right(prime,m/p)+1)
        for j in range(max(0,k-3),k):
            q = prime[j]
            if p <= q:
                if a*q <= b*p:
                    s = p*q
                    if s <= m:
                        if ans[0] <= s:
                            ans = [s,p,q]
    print(*ans[1:])
    return

#Solve
if __name__ == "__main__":
    while 1:
        m,a,b = LI()
        if m == a == b == 0:
            break
        solve(m,a,b)

Submission status: Accepted

Solution code:
import sys
sys.setrecursionlimit(10000000)
input=lambda : sys.stdin.readline().rstrip()

def eratosthenes(n):
    prime_table = [False,False,True]+[False if i%2!=0 else True for i in range(n-2)]
    i=3
    primes=[]
    while i*i<=n:
        if prime_table[i]:
            j=i*i
            while j<=n:
                prime_table[j]=False
                j+=i
        i+=2
    for i in range(len(prime_table)):
        if prime_table[i]:
            primes.append(i)
    return primes

prime_table = eratosthenes(50001)
while True:
    m,a,b=map(int,input().split())
    if m==a==b==0:
        break
    l=0
    mx=(0,0,0)
    for r in range(len(prime_table)):
        while True:
            if  mx[0]<prime_table[l]*prime_table[r]<=m and prime_table[l]/prime_table[r]>=a/b:
                mx=(prime_table[l]*prime_table[r],l,r)
            if prime_table[l]*prime_table[r]>m and l-1>=0:
                l-=1
            elif l+1<=r and (prime_table[l+1])*prime_table[r]<=m:
                l+=1
            else:
                break
    print(prime_table[mx[1]],prime_table[mx[2]])

Submission status: Runtime Error

Solution code:
import math
def sieve (n):
	prime = [0,0]
	prime += [1 for i in range(n-1)]
	ub = math.sqrt(n) + 1
	d = 2
	while d <= ub:
		if prime[d] == 0:
			d += 1
			continue
		prod = 2
		while d * prod <= n:
			prime[d*prod] = 0
			prod += 1
		d += 1
	return prime
prime = sieve(1000)
while 1:
	m,a,b = map(int,raw_input().split(" "))
	if (m,a,b) == (0,0,0):
		break
	rect_max = (0,0,0)
	q = 997
	while q >= 2:
		if prime[q] == 0:
			q -= 1
			continue
		p = q
		while p * b >= q * a:
			if prime[p] == 0 or p * q > m:
				p -= 1
				continue
			if rect_max[0] <= p*q:
				rect_max = (p*q,p,q)
			p -= 1
		q -= 1
	print rect_max[1], rect_max[2]

Submission status: Runtime Error

Solution code:
p = [1]*100001
p[0] = p[1] = 0
for i in range(2,int(100001**0.5)+1):
    if p[i]:
        p[2*i::i] = [0]*len(p[2*i::i])
p = [i for i in range(100001) if p[i]]

while 1:
    m,a,b = map(int,raw_input().split())
    if m == 0: break
    
    aw = ah = 2
    mx = 4
    for w in p:
        for h in p:
            if w*h > m: break
            if w*h > mx and 1 >= w*1.0/h >= a*1.0/b:
                mx = w*h
                aw,ah = w,h
    print aw,ah

Submission status: Runtime Error

Solution code:
import math,string,itertools,fractions,heapq,collections,re,array,bisect,sys,random,time,copy,functools

sys.setrecursionlimit(10**7)
inf = 10**20
eps = 1.0 / 10**10
mod = 998244353

def LI(): return [int(x) for x in sys.stdin.readline().split()]
def LI_(): return [int(x)-1 for x in sys.stdin.readline().split()]
def LF(): return [float(x) for x in sys.stdin.readline().split()]
def LS(): return sys.stdin.readline().split()
def I(): return int(sys.stdin.readline())
def F(): return float(sys.stdin.readline())
def S(): return input()
def pf(s): return print(s, flush=True)


class Prime():
    def __init__(self, n):
        self.M = m = int(math.sqrt(n)) + 10
        self.A = a = [True] * m
        a[0] = a[1] = False
        self.T = t = []
        for i in range(2, m):
            if not a[i]:
                continue
            t.append(i)
            for j in range(i*i,m,i):
                a[j] = False

    def is_prime(self, n):
        return self.A[n]

def main():
    rr = []
    pr = Prime(100000**2)
    tl = len(pr.T)

    while True:
        n,a,b = LI()
        if n == 0:
            break
        m = fractions.Fraction(a,b)
        sn = n ** 0.5
        r = 0
        rs = ''
        for ni in range(tl):
            i = pr.T[ni]
            if i > sn:
                break
            for nj in range(ni,tl):
                j = pr.T[nj]
                if i*j > n:
                    break
                if i*j <= r:
                    continue
                if fractions.Fraction(i,j) < m:
                    continue
                r = i*j
                rs = '{} {}'.format(i,j)

        rr.append(rs)

    return '\n'.join(map(str, rr))


print(main())

Submission status: Runtime Error

Solution code:
import math
N=100000
prime=[True]*(N+1)
prime[0]=prime[1]=False
for i in range(2,int(math.sqrt(N))):
    if prime[i]==True:
        j=i
        while i*j<=N:
            prime[i*j]=False
            j+=1
prime=[i for i in range(N+1) if prime[i]]

while True:
    m,a,b=map(int,raw_input().split())
    if m==a==b==0:break
    maxInt=0
    ans=(0,0)
    for p in range(len(prime)):
        if prime[p]>m:break
        for q in range(p,len(prime)):
            mul=prime[p]*prime[q]
            if mul>m: break
            if mul<=m and a*prime[q]<=prime[p]*b:
                if mul>maxInt:
                    maxInt=mul
                    ans=(prime[p],prime[q])
    print ans[0],ans[1]