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: Accepted

Solution code:
S = sorted([[i,j] for i in range(1,100) for j in range(i+1,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:
a = []
for w in range(2, 201):
    for h in range(1, w):
        a.append([h**2+w**2, h, w])
a.sort()

while 1:
    h,w = map(int,input().split())
    if h == 0: break
    # print(a.index([h**2+w**2,h,w]))
    ans = a[a.index([h**2+w**2,h,w])+1]
    print(ans[1], ans[2])

Submission status: Accepted

Solution code:
from sys import exit, stderr, stdin
input = stdin.readline
# setrecursionlimit(10**7)

def debug(var, name="hoge"):
    print(str(type(var)) + name +":" + " = " + repr(var), file=stderr)
    return

class Tri:

    def __init__(self, h, w):
        self.h = h
        self.w = w
        self.diag = h**2 + w**2

    def __lt__(t1, t2):
        if t1.diag < t2.diag:
            return True
        elif t1.diag > t2.diag:
            return False
        else:
            return t1.h < t2.h

    def __repr__(self):
        return str(self.h) +" " + str(self.w)

    def __str__(self):
        return 0

def main():
    while(1):
        H, W = list(map(int, input().split()))
        if (H + W == 0):
            break
        diag = H**2 + W**2
        tris = []
        for h in range(1, 150):
            for w in range(h+1, 150):
                tris.append((Tri(h,w), h==H and w==W))
        # print(Tri(5, 6) < Tri(1, 8) < Tri(4, 7))
        # print(Tri(5, 6).diag,Tri(1, 8).diag, Tri(4, 7).diag)
        tris = sorted(tris, key=lambda x:x[0])
        # print(tris[:5])
        # for t in tris[:6]:
        #     print(t[0].h, t[0].w, t[0].diag)
        for i in range(len(tris)):
            if tris[i][1] == True:
                print(tris[i+1][0].h, tris[i+1][0].w)


if __name__ == "__main__":
    main()

Submission status: Accepted

Solution code:
import sys
input = sys.stdin.readline

cheat = []
len_ = 0
for w in range(1, 151):
    for h in range(1, w):
        cheat.append((w**2+h**2, h, w))
        len_ += 1
cheat = sorted(cheat, key=lambda x:(x[0], x[1]))

while True:
    h, w = map(int, input().split())
    if h == 0:
        break
    L = h**2 + w**2
    for i in range(len_):
        if cheat[i][0] == L:
            if cheat[i][1] > h:
                print(cheat[i][1], cheat[i][2])
                break
        elif cheat[i][0] > L:
            print(cheat[i][1], cheat[i][2])
            break

Submission status: Accepted

Solution code:
poly = []
for w in range(2,150):
	for h in range(1,w):
		poly.append((w*w+h*h,h,w))
poly.sort()

while True:
	h,w = map(int,input().split())
	if h + w == 0:
		break
	a = poly.index((w*w+h*h,h,w))
	print(poly[a+1][1],poly[a+1][2])

Submission status: Accepted

Solution code:
from inspect import currentframe


def debug_print(s):
    # print(s)
    return


def debug_key(*args):
    names = {id(v): k for k, v in currentframe().f_back.f_locals.items()}
    debug_print(', '.join(names.get(id(arg), '???')+' = '+repr(arg) for arg in args))


def solve(h, w):
    debug_print("\n-----solve-----")

    if h == 0:
        return 
    d0 = h * h + w * w
    ans = (10 ** 9, h, w)
    for w in range(2, 151):
        for heisght in range(1, w):
            d = heisght * heisght + w * w
            if (d, heisght, w) < (d0, h, w) or (heisght, w) == (h, w):
                continue
            ans = min(ans, (d, heisght, w))
            break
    print('{0} {1}'.format(ans[1], ans[2]))

    return


if __name__ == '__main__':

    while True:
        h_input, w_imput = map(int, input().split())
        if h_input == w_imput == 0:
            break
        solve(h_input, w_imput)

Submission status: Accepted

Solution code:
while True:

    H, W = [int(i) for i in input().split()]

    if H == 0:
        break

    min_h = 10 ** 8
    min_w = 10 ** 8
    a = H ** 2 + W ** 2

    for w in range(1, 151):
        for h in range(1, w):
            d = w ** 2 + h ** 2
            e = min_h ** 2 + min_w ** 2
            if d < a:
                continue
            if d == a and h <= H:
                continue

            if d < e:
                min_h = h
                min_w = w
            elif d == e and h < min_h:
                min_h = h
                min_w = w

    print(min_h, min_w)

Submission status: Accepted

Solution code:
def main(h,w):
    a = h * h + w * w
    ans = []
    for x in range(1,151):
        for y in range(x+1,151):
            if a <= x ** 2 + y ** 2:
                b = x ** 2 + y ** 2
                ans.append((b, x, y))
    ans.sort()
    for b, x, y in ans:
        if a < b or x > h:
            ans = [x, y]
            break
    print(" ".join(list(map(str,ans))))
while 1:
    h, w = map(int, input().split())
    if h == w == 0:
        break
    main(h,w)

Submission status: Accepted

Solution code:
def hypot2(x, y):
    return x**2 + y**2


while True:
    h, w = map(int, input().split())
    if h == 0:
        exit()
    lb = hypot2(h, w)
    ans = []
    for y in range(1, 151):
        for x in range(y + 1, 151):
            if lb < hypot2(x, y) or (lb == hypot2(x, y) and h < y):
                ans.append([hypot2(x, y), y, x])
    d, h, w = sorted(ans)[0]
    print(h, w)

Submission status: Accepted

Solution code:
while True:
    H, W = map(int, input().split())
    if H == 0:
        break
    original_diagonal = H**2 + W**2

    ans_h = 200
    ans_w = 200
    ans_diagnal = 200000000

    for i in range(1, 150):
        for j in range(i+1, 150):
            new_diagonal = i**2 + j**2
            if original_diagonal > new_diagonal or (original_diagonal == new_diagonal and i <= H):
                continue
            if (new_diagonal < ans_diagnal) or (new_diagonal == ans_diagnal and i < ans_h):
                ans_h = i
                ans_w = j
                ans_diagnal = new_diagonal

    print(ans_h, ans_w)

Submission status: Accepted

Solution code:
H,W = 150,150
dp = [[0 for w in range(W+1)] for h in range(H+1)]
for h in range(1,H+1):
  for w in range(1,W+1):
    if h<w:
      dp[h][w] = h*h + w*w
while True:
  h,w = map(int,input().split())
  if h+w==0:
    break
  #####h<w#####
  l = dp[h][w]
  a,b = h,w
  for i in range(1,H+1):
    for j in range(1,W+1):
      if dp[i][j]==l and a<i:
        a,b = i,j
        break
    if a!=h or b!=w:
      break
  if a!=h or b!=w:
    print(a,b)
  else:
    m = 10**10
    for i in range(1,H+1):
      for j in range(1,W+1):
        if dp[i][j]>l and dp[i][j]<m:
          m = dp[i][j]
          a,b = i,j
    print(a,b)

Submission status: Accepted

Solution code:
X=[]
while(True):
    h,w=map(int,input().split())
    if (h,w)==(0,0):
        break
    else:
        X.append((h,w))
Y=[]
N=200
for h in range(1,N+1):
    for w in range(h+1,N+1):
        Y.append((h*h+w*w,min(h,w),h,w))
Y.sort()
Z=[[-1 for i in range(N+1)] for j in range(N+1)]
for i in range(len(Y)):
    p,q,r,s=Y[i]
    Z[r][s]=i
#(h*h+w*w,min(h,w))
for seq in X:
    h,w=seq
    x,y,a,b=Y[Z[h][w]+1]
    print(a,b)

Submission status: Accepted

Solution code:
import math
while True:
    h,w=map(int,input().split())
    if w==0 and h==0:
        break
    tikk=w*w+h*h
    
    min=151*151
    minw=0
    minh=0
    for ih in reversed(range(1,int(math.sqrt(tikk))+2)):
        for iw in reversed(range(ih+1,int(math.sqrt(tikk))+2)):
            itikk=iw*iw+ih*ih
            if tikk>itikk:
                continue
            if itikk<=min and (iw!=w or ih!=h):
                if itikk==tikk and ih<h:
                    continue
                min=itikk
                minw=iw
                minh=ih
    print("{} {}".format(minh,minw))

Submission status: Accepted

Solution code:
def main():
    while True:
        h, w = map(int, input().split(" "))
        if h == 0: break
        
        initOblSq = OblSq(w, h)
        minOblSq = OblSq(149, 149)
        minw = 149
        minh = 149
        for varw in range(1, 150):
            for varh in range(1, varw):
                varsq = OblSq(varw, varh)
                if varsq >= initOblSq and varsq <= minOblSq:
                    if varsq == initOblSq and varh <= h: continue
                    if varsq == minOblSq and varh >= minh: continue
                    minOblSq = varsq
                    minw = varw
                    minh = varh
        
        print(minh, minw)

def OblSq(width, height):
    return width**2 + height**2

main()

Submission status: Accepted

Solution code:
while 1:
    h,w=map(int,input().split())
    if h==w==0:break
    r=h**2+w**2
    d_m=float("inf")
    ax,ay=0,0
    for x in range(1,int(r**0.5)+2):
        y=int([(r-x**2),0][(r-x**2)<0]**0.5)
        y+=[1,0][r==x**2+y**2 and h<x]
        if x>=y:continue
        dia=x**2+y**2
        if d_m>dia:d_m,ax,ay=dia,x,y
    print(ax,ay)

Submission status: Accepted

Solution code:
lst = []
for h in range(1, 151):
    for w in range(h + 1, 151):
        lst.append((h ** 2 + w ** 2, h, w))
lst.sort()
while True:
    h, w = map(int, input().split())
    if h == 0:break
    ind = lst.index((h ** 2 + w ** 2, h, w))
    print(*lst[ind + 1][1:])

Submission status: Accepted

Solution code:
t = [(i * i + j * j, (i, j)) for i in range(1, 151) for j in range(i + 1, 151)]
t.sort()
d = {a[1]: b[1] for a, b in zip(t, t[1:])}
res = []
for e in iter(input, '0 0'):
    h, w = map(int, e.split())
    ans = d[h, w]
    res.append(' '.join(map(str, ans)))
print('\n'.join(map(str, res)))

Submission status: Accepted

Solution code:
# coding: utf-8
data={}
for i in range(1,151):#w
    for j in range(1,151):#h
        if (i>j):
            data[(i,j)]=i*i+j*j
data=sorted(data.items(),key=lambda x:(x[1],x[0][1]))
while 1:
    n,m=map(int,input().split())
    if n==0:
        break
    for i in range(len(data)):
        if data[i][0]==(m,n):
            print(*data[i+1][0][::-1])
            break

Submission status: Accepted

Solution code:
a_list = []
for i in range(1, 151):
    for j in range(1, 151):
        tmp_list = []
        tmp_list.append(i**2+j**2)
        tmp_list.append(i)
        tmp_list.append(j)
        a_list.append(tmp_list)

a_list.sort()
while(True):
    n = input()
    n = n.split()
    h = int(n[0])
    w = int(n[1])
    flag = 0

    if h==0 and w==0:
        break
    a = h**2 + w**2
    for i in range(len(a_list)):
        if a == a_list[i][0] and flag==0:
            if h < a_list[i][1] and a_list[i][1] < a_list[i][2]:
                print(str(a_list[i][1])+" "+str(a_list[i][2]))
                flag = 1
        elif a < a_list[i][0] and flag==0:
            if a_list[i][1] < a_list[i][2]:
                print(str(a_list[i][1]) + " " + str(a_list[i][2]))
                flag = 1

Submission status: Runtime Error

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()

for l in flist:
    hw = l.split(' ')
    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