def solve(h, w):
top = 1
left = 0
bottom = h + 1
right = w + 1
x = 0
y = 1
g = 0
while True:
if g == 0:
if x == right - 1:
return y, x
x = right - 1
right -= 1
elif g == 1:
if y == bottom - 1:
return y, x
y = bottom - 1
bottom -= 1
elif g == 2:
if x == left + 1:
return y, x
x = left + 1
left += 1
elif g == 3:
if y == top + 1:
return y, x
y = top + 1
top += 1
g = (g + 1) % 4
def test():
testcases = [[3, 3], [10, 7], [1000000, 333333], [3, 1]]
for tc in testcases:
i, j = solve(*tc)
print("problem(h,w):", *tc, "answer(i,j):", i, j)
test()
CmRlZiBzb2x2ZShoLCB3KToKICAgIHRvcCA9IDEKICAgIGxlZnQgPSAwCiAgICBib3R0b20gPSBoICsgMQogICAgcmlnaHQgPSB3ICsgMQogICAgeCA9IDAKICAgIHkgPSAxCiAgICBnID0gMAogICAgd2hpbGUgVHJ1ZToKICAgICAgICBpZiBnID09IDA6CiAgICAgICAgICAgIGlmIHggPT0gcmlnaHQgLSAxOgogICAgICAgICAgICAgICAgcmV0dXJuIHksIHgKICAgICAgICAgICAgeCA9IHJpZ2h0IC0gMQogICAgICAgICAgICByaWdodCAtPSAxCiAgICAgICAgZWxpZiBnID09IDE6CiAgICAgICAgICAgIGlmIHkgPT0gYm90dG9tIC0gMToKICAgICAgICAgICAgICAgIHJldHVybiB5LCB4CiAgICAgICAgICAgIHkgPSBib3R0b20gLSAxCiAgICAgICAgICAgIGJvdHRvbSAtPSAxCiAgICAgICAgZWxpZiBnID09IDI6CiAgICAgICAgICAgIGlmIHggPT0gbGVmdCArIDE6CiAgICAgICAgICAgICAgICByZXR1cm4geSwgeAogICAgICAgICAgICB4ID0gbGVmdCArIDEKICAgICAgICAgICAgbGVmdCArPSAxCiAgICAgICAgZWxpZiBnID09IDM6CiAgICAgICAgICAgIGlmIHkgPT0gdG9wICsgMToKICAgICAgICAgICAgICAgIHJldHVybiB5LCB4CiAgICAgICAgICAgIHkgPSB0b3AgKyAxCiAgICAgICAgICAgIHRvcCArPSAxCiAgICAgICAgZyA9IChnICsgMSkgJSA0CiAgICAgICAgCmRlZiB0ZXN0KCk6CiAgICB0ZXN0Y2FzZXMgPSBbWzMsIDNdLCBbMTAsIDddLCBbMTAwMDAwMCwgMzMzMzMzXSwgWzMsIDFdXQogICAgZm9yIHRjIGluIHRlc3RjYXNlczoKICAgICAgICBpLCBqID0gc29sdmUoKnRjKQogICAgICAgIHByaW50KCJwcm9ibGVtKGgsdyk6IiwgKnRjLCAiYW5zd2VyKGksaik6IiwgaSwgaikKCnRlc3QoKQ==
problem(h,w): 3 3 answer(i,j): 2 2
problem(h,w): 10 7 answer(i,j): 7 4
problem(h,w): 1000000 333333 answer(i,j): 833334 166667
problem(h,w): 3 1 answer(i,j): 3 1