Problem:


Integral Rectangles

Let us consider rectangles whose height, h, and
width, w, are both integers. We call such rectangles 
integral rectangles. In this problem, we consider only wide
integral rectangles, i.e., those with w > h.

We define the following ordering of wide integral rectangles. Given
two wide integral rectangles,

The one shorter in its diagonal line is smaller, and

If the two have diagonal lines with the same length, the one shorter
 in its height is smaller.

Given a wide integral rectangle, find the smallest wide integral
rectangle bigger than the given one.

Input

The entire input consists of multiple datasets. The number of
 datasets is no more than 100. Each dataset describes a wide integral
 rectangle by specifying its height and width, namely h
 and w, separated by a space in a line, as follows.

h w

For each dataset, h and w (>h) are both
integers greater than 0 and no more than 100. 

The end of the input is indicated by a line of two zeros separated by a space.

Output

For each dataset, print in a line two numbers describing the
height and width, namely h and w (> h), of the
smallest wide integral rectangle bigger than the one described in the
dataset.  Put a space between the numbers. No other
characters are allowed in the output.

For any wide integral rectangle given in the input, the width and
height of the smallest wide integral rectangle bigger than the given
one are both known to be not greater than 150. 

In addition, although
the ordering of wide integral rectangles uses the comparison of
lengths of diagonal lines, this comparison can be replaced with that
of squares (self-products) of lengths of diagonal lines, which can
avoid unnecessary troubles possibly caused by the use of floating-point
numbers.

Sample Input

1 2
1 3
2 3
1 4
2 4
5 6
1 8
4 7
98 100
99 100
0 0

Output for the Sample Input

1 3
2 3
1 4
2 4
3 4
1 8
4 7
2 8
3 140
89 109



Sample input:
1 2
1 3
2 3
1 4
2 4
5 6
1 8
4 7
98 100
99 100
0 0


Sample output:
1 3
2 3
1 4
2 4
3 4
1 8
4 7
2 8
3 140
89 109

Submission status: Wrong Answer

Solution code:
while True:
    h, w = map(int, input().split())
    if not h:
        break
    diagonal = h**2 + w**2
    ans = []
    append = ans.append
    for y in range(1, 151):
        for x in range(y+1, 151):
            if x**2+y**2 > diagonal:
                append((x**2+y**2, y, x))
                break
    print(*sorted(ans)[0][1:])

Submission status: Wrong Answer

Solution code:
#! /usr/bin/env python
# -*- coding: utf-8 -*-

import os
import sys
import itertools
import math
from collections import Counter, defaultdict

class Main(object):
    
    def __init__(self):
        pass

    def solve(self):
        '''
        insert your code
        '''
        diag = [[None for j in range(151)] for i in range(151)]
        for i in range(1, len(diag)):
            for j in range(1, len(diag[0])):
                if i >= j:
                    continue
                diag[i][j] = i * i + j * j
        while True:
            h, w = map(int, raw_input().split())
            if h == 0 and w == 0:
                break
            m = (float('inf'), float('inf'), float('inf'))
            for i in reversed(range(1, len(diag))):
                for j in reversed(range(1, len(diag[0]))):
                    if (not (i == h and j == w)) and diag[i][j] is not None and diag[i][j] >= h * h + w * w:
                        if diag[i][j] == h * h + w * w:
                            if h < i < m[0]:
                                m = (i, j, diag[i][j])
                        elif m[2] == diag[i][j]:
                            if i < m[0]:
                                m = (i, j, diag[i][j])
                        elif m[2] > diag[i][j]:
                            m = (i, j, diag[i][j])
            print '{0} {1}'.format(m[0], m[1])
                        
                        
        
        return None

if __name__ == '__main__':
    m = Main()
    m.solve()

Submission status: Wrong Answer

Solution code:
import sys
import math

def is_square(n):
    for i in range(n):
        if i*i == n:
            return True
    return False

def f(h,w):
    r = h**2 + w**2

    for hh in range(h+1,w):
        if is_square(r-hh**2):
            return (hh, int(math.sqrt(r-hh**2)))

    t1 = h**2+(w+1)**2
    t2 = (h+1)**2+w**2

    if r+1<t1 and r+1<t2:
        return (1,r+1)
    elif t1 < t2 or h+1==w:
        return (h,w+1)
    else:
        return (h+1,w)


while True:
    line = sys.stdin.readline()
    line = line.split(" ")

    h = int(line[0])
    w = int(line[1])

    if h==0 and w==0:
        break

    ret = f(h,w)
    print(str(ret[0])+" "+str(ret[1]))

Submission status: Wrong Answer

Solution code:
import sys
from math import sqrt

inf = 1<<30

def solve():
    while 1:
        h, w = map(int, sys.stdin.readline().split())

        if h == w == 0:
            return

        L = h**2 + w**2
        ansx = ansy = inf
        maxL = inf

        for x in range(1, 150 + 1):
            for y in range(x + 1, 150 + 1):
                if x**2 + y**2 == L:
                    if h < x:
                        ansx = x
                        ansy = y
                        break
                elif L < x**2 + y**2 < maxL:
                    maxL = x**2 + y**2
                    ansx = x
                    ansy = y

        print(ansx, ansy)

if __name__ == '__main__':
    solve()

Submission status: Wrong Answer

Solution code:
while True:
    H,W = map(int,input().split())
    if H == 0: break
    d0 = H*H + W*W
    ans_d = ans_h = ans_w = 10**9
    for w in range(2,151):
        for h in range(1,w):
            d = h*h + w*w
            if d <= d0: continue
            if d <= ans_d:
                ans_h = h
                ans_w = w
                if d < ans_d:
                    ans_d = d
            break
    print('{0} {1}'.format(ans_h, ans_w))

Submission status: Wrong Answer

Solution code:
def intRec(h,w):
    diag=h**2+w**2
    ans=[]
    temp=1e8
    for H in range(1,151):
        for W in range(w,151):
            if (H**2+W**2)>diag and (H**2+W**2)<temp:
                temp=H**2+W**2
                ans=[H,W]
    return ans

if __name__=="__main__":
    while True:
        h,w=map(int,input().split())
        if h==0:
            break
        ans=intRec(h,w)
        print(ans[0],ans[1])

Submission status: Wrong Answer

Solution code:
def isSmall(h,w):
    diag=h**2+w**2
    minD=1e8
    ans=[]
    for W in range(1,151):
        for H in range(1,W):
            Diag=H**2+W**2
            if diag==Diag and H!=h:
                return [H,W]
            elif diag<Diag and Diag<minD:
                minD=Diag
                ans=[H,W]
    return ans

if __name__=="__main__":
    while True:
        h,w=map(int,input().split())
        if h==0:
            break
        ans=isSmall(h,w)
        print(ans[0],ans[1])

Submission status: Wrong Answer

Solution code:
import sys
import math
from operator import itemgetter

#a = h*h + w*w
#b = math.sqrt(a)

mylist = []

hlist = range(1, 101)
wlist = range(1, 101)

for h1 in hlist:
    for w1 in wlist:
        if not w1 > h1:
             continue
        t1 = math.sqrt(pow(h1, 2) + pow(w1, 2))
        mylist.append((t1, h1, w1))

mylist.sort(key=itemgetter(0))

#for t1 in mylist:
#    print t1

#h = int(sys.argv[1])
#w = int(sys.argv[2])
#arglist = sys.argv[1]

#f = open('1.txt','r')
#flist = f.readlines()

print('input h w ?')
#hw = input('>> ')
#h,w=[int(i) for i in input().split()]
#h,w=map(int,input().split())
hw = raw_input().split()

#print hw[0]
#print hw[1]

h = int(hw[0])
w = int(hw[1])

if h == 0 and w == 0: sys.exit()
t = math.sqrt(pow(h, 2) + pow(w, 2))

for t1,h1,w1 in mylist:
    if t > t1: continue
    if t == t1:
        if h >= h1: continue
    print str(h1) + " " + str(w1)
    break

Submission status: Wrong Answer

Solution code:
import sys
import math
from operator import itemgetter

mylist = []

hlist = range(1, 101)
wlist = range(1, 101)

for h1 in hlist:
    for w1 in wlist:
        if not w1 > h1:
             continue
        t1 = pow(h1, 2) + pow(w1, 2)
        mylist.append((t1, h1, w1))

mylist.sort(key=itemgetter(0))

print('input h w ?')
hw = raw_input().split()

#print hw[0]
#print hw[1]

h = int(hw[0])
w = int(hw[1])

if h == 0 and w == 0: sys.exit()
t = pow(h, 2) + pow(w, 2)

for t1,h1,w1 in mylist:
    if t > t1: continue
    if t == t1:
        if h >= h1: continue
    print str(h1) + " " + str(w1)
    break

Submission status: Wrong Answer

Solution code:
import sys
import math
from operator import itemgetter

mylist = []

hlist = range(1, 151)
wlist = range(1, 151)

for h1 in hlist:
    for w1 in wlist:
        if not w1 > h1:
             continue
        t1 = pow(h1, 2) + pow(w1, 2)
        mylist.append((t1, h1, w1))

mylist.sort(key=itemgetter(0))

print('input h w ?')
hw = raw_input().split()

#print hw[0]
#print hw[1]

h = int(hw[0])
w = int(hw[1])

if h == 0 and w == 0: sys.exit()
t = pow(h, 2) + pow(w, 2)

for t1,h1,w1 in mylist:
    if t > t1: continue
    if t == t1:
        if h >= h1: continue
    print str(h1) + " " + str(w1)
    break

Submission status: Wrong Answer

Solution code:
import sys
import math
from operator import itemgetter

mylist = []

hlist = range(1, 151)
wlist = range(1, 151)

for h1 in hlist:
    for w1 in wlist:
        if not w1 > h1:
             continue
        t1 = pow(h1, 2) + pow(w1, 2)
        mylist.append((t1, h1, w1))

mylist.sort(key=itemgetter(0))

print('input h w ?')
hw = raw_input().split()

#print hw[0]
#print hw[1]

h = int(hw[0])
w = int(hw[1])

if h == 0 and w == 0: sys.exit()
t = pow(h, 2) + pow(w, 2)

hwlist = []

for t1,h1,w1 in mylist:
    if t > t1: continue
    if t == t1:
        hwlist.append((h1, w1))
        continue
    if t < t1:
        if len(hwlist) == 0:    
            print str(h1) + " " + str(w1)
            break
        else:
            hwlist.sort(key=itemgetter(0))
            print str(hwlist[0][0]) + " " + str(hwlist[0][1])
            break

Submission status: Wrong Answer

Solution code:
import sys
import math
from operator import itemgetter

mylist = []

hlist = range(1, 151)
wlist = range(1, 151)

for h1 in hlist:
    for w1 in wlist:
        if not w1 > h1:
             continue
        t1 = pow(h1, 2) + pow(w1, 2)
        mylist.append((t1, h1, w1))

mylist.sort(key=itemgetter(0))

print('input h w ?')
hw = raw_input().split()

#print hw[0]
#print hw[1]

h = int(hw[0])
w = int(hw[1])

if h == 0 and w == 0: sys.exit()
t = pow(h, 2) + pow(w, 2)

hwlist = []

for t1,h1,w1 in mylist:
    if t > t1: continue
    if t == t1:
        hwlist.append((h1, w1))
        continue
    if t < t1:
        if len(hwlist) == 0:
            print str(h1) + " " + str(w1)
            break
        else:
            hwlist.sort(key=itemgetter(0))
            for h2,w2 in hwlist:
                if h < h2:
                    print str(h2) + " " + str(w2)
                    break

Submission status: Wrong Answer

Solution code:
import sys
import math
from operator import itemgetter

mylist = []

hlist = range(1, 151)
wlist = range(1, 151)

for h1 in hlist:
    for w1 in wlist:
        if not w1 > h1:
             continue
        t1 = pow(h1, 2) + pow(w1, 2)
        mylist.append((t1, h1, w1))

mylist.sort(key=itemgetter(0))

print('input h w ?')
hw = raw_input().split()

#print hw[0]
#print hw[1]

h = int(hw[0])
w = int(hw[1])

if h == 0 and w == 0: sys.exit()
t = pow(h, 2) + pow(w, 2)

hwlist = []

for t1,h1,w1 in mylist:
    if t > t1: continue
    if t == t1:
        hwlist.append((h1, w1))
        continue
    if t < t1:
        if len(hwlist) == 1:
            print str(h1) + " " + str(w1)
            sys.exit()
        else:
            hwlist.sort(key=itemgetter(0))
            for h2,w2 in hwlist:
                if h < h2:
                    print str(h2) + " " + str(w2)
                    sys.exit()
            print str(h1) + " " + str(w1)
            sys.exit()

Submission status: Wrong Answer

Solution code:
import sys
import math
from operator import itemgetter

mylist = []

hlist = range(1, 151)
wlist = range(1, 151)

for h1 in hlist:
    for w1 in wlist:
        if not w1 > h1:
             continue
        t1 = pow(h1, 2) + pow(w1, 2)
        mylist.append((t1, h1, w1))

mylist.sort(key=itemgetter(0))

print('input h w ?')
hw = raw_input().split()

#print hw[0]
#print hw[1]

h = int(hw[0])
w = int(hw[1])

if h == 0 and w == 0: sys.exit()
t = pow(h, 2) + pow(w, 2)

hwlist = []

for t1,h1,w1 in mylist:
    if t > t1: continue
    if t == t1:
        if h <> h1 and w <> w1:
            hwlist.append((h1, w1))
        continue
    if t < t1:
        hwlist.sort(key=itemgetter(0))
        for h2,w2 in hwlist:
            if h < h2:
                print str(h2) + " " + str(w2)
                sys.exit()
        print str(h1) + " " + str(w1)
        sys.exit()

Submission status: Wrong Answer

Solution code:
R = 143
R2 = 2*(R**2)+1
dp = [0]*R2
for i in range(1,R):
	for j in range(i+1,R):
		dp[i**2+j**2] = 1
	
while 1:
	h,w = map(int,raw_input().split())
	if h == 0: break
	for i in range(h**2+w**2+1,R2):
		if dp[i]: break
	for hi in range(1,101):
		wi = (i - hi**2)**0.5
		if wi - int(wi) < 1e-6:
			print hi,int(wi)
			break

Submission status: Wrong Answer

Solution code:
R = 143
R2 = 2*(R**2)+1
dp = [0]*R2
for i in range(1,R):
	for j in range(i+1,R):
		dp[i**2+j**2] = 1
	
while 1:
	h,w = map(int,raw_input().split())
	if h == 0: break
	for i in range(h**2+w**2+1,R2):
		if dp[i]: break
	for hi in range(1,101):
		wi = (i - hi**2)**0.5
		if wi - int(wi) < 1e-9:
			print hi,int(wi)
			break

Submission status: Wrong Answer

Solution code:
R = 150
R2 = 2*(R**2)+1
S = [0]*R2
for i in range(1,R):
	for j in range(i+1,R):
		S[i**2+j**2] = 1
	
while 1:
	h,w = map(int,raw_input().split())
	if h == 0: break
	for i in range(h**2+w**2+1,R2):
		if S[i]: break
	for hi in range(1,101):
		wi = (i - hi**2)**0.5
		if wi - int(wi) < 1e-9:
			print hi,int(wi)
			break

Submission status: Wrong Answer

Solution code:
R = 150
R2 = 2*(R**2)+1
S = [0]*R2
for i in range(1,R):
	for j in range(i+1,R):
		S[i**2+j**2] = 1
	
while 1:
	h,w = map(int,raw_input().split())
	if h == 0: break
	for i in range(h**2+w**2+1,R2):
		if S[i]:
			s = i
			break
	for hi in range(1,150):
		wi = (s - hi**2)**0.5
		if wi - int(wi) < 1e-9:
			print hi,int(wi)
			break

Submission status: Wrong Answer

Solution code:
R = 151
R2 = 2*(R**2)+1
S = [0]*R2
for i in range(1,R):
	for j in range(i+1,R):
		S[i**2+j**2] = 1
	
while 1:
	h,w = map(int,raw_input().split())
	if h == 0: break
	for i in range(h**2+w**2+1,R2):
		if S[i]:
			s = i
			break
	for hi in range(1,151):
		wi = (s - hi**2)**0.5
		if wi - int(wi) < 1e-9:
			print hi,int(wi)
			break

Submission status: Wrong Answer

Solution code:
R = 151
R2 = 2*(R**2)+1
S = [[] for i in range(R2)]
for i in range(1,R):
	for j in range(i+1,R):
		if len(S[i**2+j**2]) == 0:
			S[i**2+j**2] = [i,j]

while 1:
	h,w = map(int,raw_input().split())
	if h == 0: break
	for i in range(h**2+w**2+1,R2):
		if S[i]:
			h,w = S[i]
			break
	print h,w

Submission status: Wrong Answer

Solution code:
S = sorted([[i,j] for i in range(1,100) for j in range(i,151)], key = lambda x:x[0]**2+x[1]**2)
while 1:
	h,w = map(int,raw_input().split())
	if h == 0: break
	hi,wi = S[S.index([h,w])+1]
	print hi,wi

Submission status: Wrong Answer

Solution code:
S = sorted([[i,j] for i in range(1,120) for j in range(i,151)], key = lambda x:x[0]**2+x[1]**2)
while 1:
	h,w = map(int,raw_input().split())
	if h == 0: break
	hi,wi = S[S.index([h,w])+1]
	print hi,wi

Submission status: Wrong Answer

Solution code:
S = sorted([[i,j] for i in range(1,151) for j in range(i,151)], key = lambda x:x[0]**2+x[1]**2)
while 1:
	h,w = map(int,raw_input().split())
	if h == 0: break
	hi,wi = S[S.index([h,w])+1]
	print hi,wi

Submission status: Accepted

Solution code:
def sol(h,w):
    d = (h*h) + (w*w)
    ans =(150,150)
    for dw in range(1,150):
        for dh  in range(1,dw):
            dd = (dw*dw) +  (dh*dh)
            if dd < d:continue
            if dw == w and dh == h:continue
            if dd == d and dh < h:continue
            da = (ans[0]*ans[0]) + (ans[1]*ans[1])
            if dd < da:ans = (dh,dw)
            elif dd == da:
                if dh < ans[0]: ans = (dh,dw)
    print(*ans)

if __name__ =="__main__":       
    while 1:
        h,w = list(map(int,input().split(' ')))
        if h ==0:break
        sol(h,w)

Submission status: Accepted

Solution code:
while True:
    h, w = map(int, input().split())
    if not h:
        break
    diagonal = h**2 + w**2
    ans = []
    append = ans.append
    for y in range(1, 151):
        for x in range(y+1, 151):
            if x**2+y**2 > diagonal or x**2+y**2 == diagonal and y > h:
                append((x**2+y**2, y, x))
                break
    print(*sorted(ans)[0][1:])

Submission status: Accepted

Solution code:
import math
anslist = []

while True:
    h, w = map(int, input().split())
    if h == 0 and w == 0:
        break

    diagonal = h * h + w * w
    i = h + 1
    #print(diagonal)
    while True:
        #print(i, diagonal - i * i, diagonal)
        if i * i >= diagonal - i * i:
            i = 0
            diagonal += 1
        elif math.sqrt(diagonal - i * i) == int(math.sqrt(diagonal - i * i)):
            anslist.append([i, int(math.sqrt(diagonal - i * i))])
            break
        i += 1

for i, j in anslist:
    print(i, j)

Submission status: Accepted

Solution code:
def less(ah, ad, bh, bd):
    if ad == bd:
        return ah < bh
    return ad < bd

def check(aw, ah, bw, bh, cw, ch):
    ad, bd, cd = aw * aw + ah * ah, bw * bw + bh * bh, cw * cw + ch * ch
    return less(ah, ad, bh, bd) and less(bh, bd, ch, cd)

while True:
    h, w = map(int, input().split())
    if not w:
        break
    d = w * w + h * h
    aw, ah = 1000, 1000
    for hh in range(1, 151):
        for ww in range(hh + 1, 151):
            if check(w, h, ww, hh, aw, ah):
                aw, ah = ww, hh
    print(ah, aw)

Submission status: Accepted

Solution code:
#! /usr/bin/env python
# -*- coding: utf-8 -*-

import os
import sys
import itertools
import math
from collections import Counter, defaultdict

class Main(object):
    
    def __init__(self):
        pass

    def solve(self):
        '''
        insert your code
        '''
        diag = [[None for j in range(151)] for i in range(151)]
        for i in range(1, len(diag)):
            for j in range(1, len(diag[0])):
                if i >= j:
                    continue
                diag[i][j] = i * i + j * j
        while True:
            h, w = map(int, raw_input().split())
            if h == 0 and w == 0:
                break
            m = (float('inf'), float('inf'), float('inf'))
            for i in reversed(range(1, len(diag))):
                for j in reversed(range(1, len(diag[0]))):
                    if diag[i][j] is not None and diag[i][j] >= h * h + w * w:
                        if diag[i][j] == h * h + w * w:
                            if h < i <= m[0]:
                                m = (i, j, diag[i][j])
                        elif m[2] == diag[i][j]:
                            if i < m[0]:
                                m = (i, j, diag[i][j])
                        elif m[2] > diag[i][j]:
                            m = (i, j, diag[i][j])

            print '{0} {1}'.format(m[0], m[1])
                        
                        
        
        return None

if __name__ == '__main__':
    m = Main()
    m.solve()

Submission status: Accepted

Solution code:
rect = [(i**2+j**2, i, j) for i in range(1,150) for j in range(i+1, 150)]
rect.sort()
while True:
    h, w = map(int, input().split())
    if h == 0 and w == 0: break
    _, n_h, n_w = rect[rect.index((h**2+w**2, h, w))+1]
    print(n_h, n_w)

Submission status: Accepted

Solution code:
#!/usr/bin/env python
# -*- coding: utf-8 -*-

import itertools

rects = sorted([(t[0]**2 + t[1] ** 2,t[0],t[1]) for t in itertools.combinations(range(1,151),2)],key = lambda x:(x[0],x[1]))

while True:
    h,w = map(int,input().split(" "))
    if h == 0 and w == 0:
        break
    rect = h**2 + w**2
    for i in range(len(rects)):
        if rects[i][0] == rect and rects[i][1] == h and rects[i][2] == w:
            H,W = rects[i+1][1:]
            print(H,W)

Submission status: Accepted

Solution code:
def dia(r):
	return r[0]**2 + r[1]**2 
def issmaller (a,b):
 	if dia(a) != dia(b):
		return dia(a) < dia(b)
	else :
		return a[0] < b[0]

while 1 :
	h, w = map(int,raw_input().split())
	if (h, w) == (0,0):
		break
	dia_base = (h, w)
	rect = (149,150) if h + 1 == w else (h+1,w) 
	winc = w + 1
	hinc = winc
	while hinc > 1:
		for hinc in xrange(1,winc):
			if issmaller(dia_base, (hinc, winc)):
				if issmaller((hinc,winc),rect):
					rect = (hinc,winc)
				break
		winc += 1

	winc = w - 1
	hinc = winc
	while hinc > 1:
		for hinc in xrange(1, winc):
			if issmaller((winc-hinc,winc),dia_base):
				break
			if issmaller((winc-hinc,winc),rect):
				rect = (winc-hinc,winc)
		winc -= 1
	
	print rect[0], rect[1]

Submission status: Accepted

Solution code:
import itertools
S=[]
for i in range(1,150):
    for j in range(1,i):
        S.append((i*i+j*j,j,i))
S.sort()
while True:
    h,w=map(int, input().split())
    if h==0 and w==0:
        exit()
    s = S[S.index((w*w+h*h,h,w))+1]
    print(s[1],s[2])

Submission status: Accepted

Solution code:

class T:
	def __init__(self, h, w):
		self.h = h
		self.w = w
		self.d = w*w + h*h
	def __lt__(self, other):
		return (self.d < other.d) or ((self.d == other.d) and self.h < other.h)
	def __gt__(self, other):
		return (self.d > other.d) or ((self.d == other.d) and self.h > other.h)

t = []
for i in range(1, 151):
	for j in range(i+1, 151):
		t.append(T(i, j))
t = sorted(t)

while True:
	h, w = map(int, input().split())
	if (h, w) == (0, 0):
		break
	hw = T(h, w)
	for i in range(len(t)):
		if hw < t[i]:
			print('{} {}'.format(t[i].h, t[i].w))
			break

Submission status: Accepted

Solution code:
from bisect import bisect_right

rectangles = [(h ** 2 + w ** 2, h, w) for h in range(1, 151) for w in range(1, 151) if h < w]
rectangles.sort()

while True:
    h, w = map(int, input().split())
    if not h and not w:
        break

    gt_rectangle = rectangles[bisect_right(rectangles, (h ** 2 + w ** 2, h, w))]
    print(gt_rectangle[1], gt_rectangle[2])

Submission status: Accepted

Solution code:
import sys
import math

def f(h,w):
    r = h**2 + w**2
    ary = []
    for hi in range(1,150+1):
        for wi in range(hi+1,150+1):
            ss = hi**2+wi**2
            if ss == r and hi>h:
                ary.append({
                    "ss": ss
                    ,"h": hi
                    ,"w": wi
                })
            elif ss > r:
                ary.append({
                    "ss": ss
                    ,"h": hi
                    ,"w": wi
                })
    mino = ary[0]
    for i,o in enumerate(ary):
        if mino["ss"]>o["ss"]:
            mino = o
        elif mino["ss"]==o["ss"] and mino["h"]>o["h"]:
            mino = o
    return (mino["h"],mino["w"])

while True:
    line = sys.stdin.readline()
    line = line.split(" ")

    h = int(line[0])
    w = int(line[1])

    if h==0 and w==0:
        break

    ret = f(h,w)
    print(str(ret[0])+" "+str(ret[1]))

Submission status: Accepted

Solution code:
import heapq
q = []

for i in xrange(1, 1001):
  for j in xrange(i+1, 1001):
    heapq.heappush(q, (i**2+j**2, (i,j)))
q = sorted(q)

while True:
  h,w = map(int, raw_input().split())
  if h==w==0: break
  for i in xrange(len(q)):
    if q[i][1] == (h, w):
      index = i
  print "%d %d" % (q[index+1][1][0], q[index+1][1][1])

Submission status: Accepted

Solution code:
l = []
for i in range(1,151):
    for j in range(1,151):
        if i==j or i>j: continue
        l += [[i**2+j**2,i,j]]
l = sorted(l)
while True:
    h,w = map(int,input().split())
    if w==h==0: break
    i = l.index([w**2+h**2,h,w])+1
    print(*l[i][1:],sep=' ')

Submission status: Accepted

Solution code:
l = [[i**2+j**2,i,j] for j in range(1,151) for i in range(1,151) if i!=j and i<j]
l = sorted(l)
while True:
    h,w = map(int,input().split())
    if w==h==0: break
    i = l.index([w**2+h**2,h,w])+1
    print(*l[i][1:],sep=' ')

Submission status: Accepted

Solution code:
import bisect
l = [[i**2+j**2,i,j] for j in range(1,151) for i in range(1,151) if i!=j and i<j]
l.sort()
while True:
    h,w = map(int,input().split())
    if w==h==0: break
    r = l[bisect.bisect_right(l,[w**2+h**2,h,w])]
    print(r[1],r[2])

Submission status: Accepted

Solution code:
def f(h,w):
    s=h*h+w*w
    h+=1
    while 1:
        while h*h<s:
            w=h+1
            while h*h+w*w<=s:
                if h*h+w*w==s:print(h,w);return
                w+=1
            h+=1
        s+=1;h=1
while 1:
    h,w=map(int,input().split())
    if h==0:break
    f(h,w)

Submission status: Accepted

Solution code:
import bisect
l = sorted([[i**2+j**2,i,j] for j in range(1,150) for i in range(1,150) if i<j and i!=j])
while True:
    h,w = map(int,input().split())
    if h==w==0: break
    rh,rw = l[bisect.bisect_right(l, [h**2+w**2,h,w])][1:]
    print(rh,rw)

Submission status: Accepted

Solution code:
import sys
from math import sqrt

inf = 1<<30

def solve():
    while 1:
        h, w = map(int, sys.stdin.readline().split())

        if h == w == 0:
            return

        L = h**2 + w**2
        maxL = inf
        ansx = inf
        ansy = inf

        for x in range(1, L):
            if L - x**2 < 0:
                break

            y = nibutan(L - x**2)

            if y <= x:
                break

            if x**2 + y**2 == L:
                if h < x:
                    ansx = x
                    ansy = y
                    break
                else:
                    y += 1

            if x**2 + y**2 < maxL:
                maxL = x**2 + y**2
                ansx = x
                ansy = y

        print(ansx, ansy)

def nibutan(a):
    top = inf
    btm = 0

    while top - btm > 1:
        mid = (top + btm) // 2

        if mid**2 >= a:
            top = mid
        else:
            btm = mid

    return top

if __name__ == '__main__':
    solve()

Submission status: Accepted

Solution code:
import sys
from math import sqrt

inf = 1<<30

def solve():
    while 1:
        h, w = map(int, sys.stdin.readline().split())

        if h == w == 0:
            return

        L = h**2 + w**2

        ansx, ansy = zentansaku(h, w, L)

        print(ansx, ansy)

def zentansaku(h, w, L):
    ansx = ansy = maxL = inf

    for x in range(1, 150 + 1):
            for y in range(x + 1, 150 + 1):
                if x**2 + y**2 == L:
                    if h < x:
                        return x, y
                elif L < x**2 + y**2 < maxL:
                    maxL = x**2 + y**2
                    ansx = x
                    ansy = y

    return ansx, ansy

if __name__ == '__main__':
    solve()

Submission status: Accepted

Solution code:
def compare(h,w):
    hw=h**2+w**2

    tmp_h=150
    tmp_w=150
    tmp_hw=150**2+150**2

    for j in range(1,150):
        for i in range(1,j):
            ij=i**2+j**2
            if (hw < ij) or (hw == ij and h < i) : # ??\????????\???
                #print ("1",h,w,hw,i,j,ij,tmp_h,tmp_w,tmp_hw)
                if  (tmp_hw > ij) or (tmp_hw == ij and tmp_h > i):
                    #print ("2",h,w,hw,i,j,ij,tmp_h,tmp_w,tmp_hw)
                    tmp_h=i
                    tmp_w=j
                    tmp_hw=ij

    print(tmp_h,tmp_w)


import sys

for line in sys.stdin:
    h,w=map(int,line.split())
    if h!=0 and w!=0:
        compare(h,w)

Submission status: Accepted

Solution code:
A=[(j,i)for i in range(150)for j in range(1,i)]
A.sort(key=lambda(j,i):(i**2+j**2,j,i))
while 1:
 h,w=map(int,raw_input().split())
 if h<1:break
 h,w=A[A.index((h,w))+1];print h,w

Submission status: Accepted

Solution code:
while True:
    H,W = map(int,input().split())
    if H == 0: break
    d0 = H*H + W*W
    ans = (10**9,H,W)
    for w in range(2,151):
        for h in range(1,w):
            d = h*h + w*w
            if (d,h,w) < (d0,H,W) or (h,w) == (H,W): continue
            ans = min(ans, (d,h,w))
            break
    print('{0} {1}'.format(ans[1], ans[2]))

Submission status: Accepted

Solution code:
def intRec(h,w):
    diag=h**2+w**2
    minD=1e8
    ans=[]
    for H in range(1,151):
        for W in range(1,151):
            if H<W:
                Diag=H**2+W**2
                if diag==Diag and H>h:
                    return [H,W]
                elif diag<Diag and Diag<minD:
                    minD=Diag
                    ans=[H,W]
    return ans

if __name__=="__main__":
    while True:
        h,w=map(int,input().split())
        if h==0:
            break
        ans=intRec(h,w)
        print(ans[0],ans[1])

Submission status: Accepted

Solution code:
from math import ceil

while True:
    h, w = map(int, input().split())
    if h == w == 0:
        break

    ha, wa = 150, 150
    dia = h**2 + w**2
    for hi in range(1, 150):
        wi = ceil((dia - hi ** 2) ** (1 / 2))

        if hi >= wi:
            break

        if dia == hi**2 + wi**2:
            if hi <= h:
                wi += 1

        if ha**2 + wa**2 > hi**2 + wi**2:
            ha, wa = hi, wi

    print(ha, wa)

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 = 10**9+7

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)

def main():
    rr = []
    t = []
    for i in range(1,151):
        for j in range(i+1,151):
            t.append((i**2+j**2, (i,j)))
    t.sort()
    tt = list(map(lambda x: x[1], t))

    while True:
        h,w = LI()
        if h == 0:
            break
        i = tt.index((h,w))
        r = tt[i+1]

        rr.append(' '.join(map(str,r)))

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


print(main())

Submission status: Accepted

Solution code:
lst = [(i**2+j**2,i,j) for i in range(1,201) for j in range(i+1,201)]
lst.sort(key = lambda x:(x[0],x[1]))
while True:
	a,b = map(int,input().split(" "))
	if a == 0 and b == 0:
		break
	else:
		obj = a**2 + b**2
		for i in range(40000):
			if lst[i] == (obj,a,b):
				print(lst[i+1][1],lst[i+1][2])
				break

Submission status: Accepted

Solution code:
d={(i,j):(i**2+j**2)**(0.5) for i in range(1,151) for j in range(1,151) if i<j}
r=sorted(d.items(),key=lambda x:x[1])

while 1:
    h,w=map(int,input().split())
    if not h and not w:break
    idx=[i for (i,j) in enumerate(r) if j[0]==(h,w)][0]
    H,W=r[idx+1][0]
    print(H,W)

Submission status: Accepted

Solution code:
L=[]
for h in range(1,150):
    for	w in range(h+1,151):
	L.append((h*h+w*w,h,w))
L.sort()
while True:
    h,w=map(int,raw_input().split())
    if h==w==0:break
    d=h*h+w*w
    for dd,dh,dw in L:
	if dd==d and dh>h:
            print dh,dw
            break
	elif dd>d:
            print dh,dw
            break

Submission status: Accepted

Solution code:
S = []
for i in range(1,100):
	for j in range(i+1,151):
		S.append([i,j])
S = sorted(S, key = lambda x:x[0]**2 + x[1]**2)
while 1:
	h,w = map(int,raw_input().split())
	if h == 0: break
	hi,wi = S[S.index([h,w])+1]
	print hi,wi