# (1).
# @see 6SDvDi
# Conversion.
def iton(x):
r = []
while True:
x, y = divmod(x, 10)
r.append(y)
if x == 0:
return r
def ntos(a):
return ''.join(map(str, reversed(a)))
# Math.
def _iszero(a):
return len(a) == 1 and a[0] == 0
def _zeroextend(a, n):
return a[:] + [0] * (n - len(a))
def _zeroreduce(a):
n = len(a)
while n != 1 and a[n-1] == 0:
n -= 1
return a[:n]
def nshl(a, n):
return _zeroreduce([0] * n + a[:])
def nshr(a, n):
return _zeroextend(a[n:], 1)
def nadd(a, b):
n = 1 + max(len(a), len(b))
r = _zeroextend(a, n)
b = _zeroextend(b, n)
c = 0
for i in range(n):
c, r[i] = divmod(r[i] + b[i] + c, 10)
return _zeroreduce(r)
def nmul(a, b):
if _iszero(b):
return [0]
if b[0] == 0:
return nmul(nshl(a, 1), nshr(b, 1))
return nadd(a, nmul(a, nadd(b, [-1])))
# Test.
n = 50
for x in range(0, n+1):
for y in range(0, n+1):
# nadd
u = int(ntos(nadd(iton(x), iton(y))))
v = x + y
assert u == v, f'{x} + {y}; {u}, {v}'
# nmul
u = int(ntos(nmul(iton(x), iton(y))))
v = x * y
assert u == v, f'{x} * {y}; {u}, {v}'
def show(y):
return y, ntos(y)
x = 123
print(show(iton(0)))
print(show(iton(1)))
print(show(iton(x)))
print('-')
print(show(nadd(iton(0), iton(0))))
print(show(nadd(iton(1), iton(1))))
print(show(nadd(iton(x), iton(x))))
print('-')
print(show(nmul(iton(0), iton(0))))
print(show(nmul(iton(0), iton(1))))
print(show(nmul(iton(1), iton(0))))
print(show(nmul(iton(1), iton(1))))
print(show(nmul(iton(x), iton(1))))
print(show(nmul(iton(1), iton(x))))
print(show(nmul(iton(x), iton(x))))
print(show(nmul(iton(x), iton(x*x))))
print(show(nmul(iton(x*x), iton(x))))
IyAoMSkuCiMgQHNlZSA2U0R2RGkKCiMgQ29udmVyc2lvbi4KCmRlZiBpdG9uKHgpOgogICAgciA9IFtdCiAgICB3aGlsZSBUcnVlOgogICAgICAgIHgs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A9IDUwCmZvciB4IGluIHJhbmdlKDAsIG4rMSk6CiAgICBmb3IgeSBpbiByYW5nZSgwLCBuKzEpOgogICAgICAgICMgbmFkZAogICAgICAgIHUgPSBpbnQobnRvcyhuYWRkKGl0b24oeCksIGl0b24oeSkpKSkKICAgICAgICB2ID0geCArIHkKICAgICAgICBhc3NlcnQgdSA9PSB2LCBmJ3t4fSArIHt5fTsge3V9LCB7dn0nCiAgICAgICAgIyBubXVsCiAgICAgICAgdSA9IGludChudG9zKG5tdWwoaXRvbih4KSwgaXRvbih5KSkpKQogICAgICAgIHYgPSB4ICogeQogICAgICAgIGFzc2VydCB1ID09IHYsIGYne3h9ICoge3l9OyB7dX0sIHt2fScKCmRlZiBzaG93KHkpOgogICAgcmV0dXJuIHksIG50b3MoeSkKCnggPSAxMjMKcHJpbnQoc2hvdyhpdG9uKDApKSkKcHJpbnQoc2hvdyhpdG9uKDEpKSkKcHJpbnQoc2hvdyhpdG9uKHgpKSkKcHJpbnQoJy0nKQpwcmludChzaG93KG5hZGQoaXRvbigwKSwgaXRvbigwKSkpKQpwcmludChzaG93KG5hZGQoaXRvbigxKSwgaXRvbigxKSkpKQpwcmludChzaG93KG5hZGQoaXRvbih4KSwgaXRvbih4KSkpKQpwcmludCgnLScpCnByaW50KHNob3cobm11bChpdG9uKDApLCBpdG9uKDApKSkpCnByaW50KHNob3cobm11bChpdG9uKDApLCBpdG9uKDEpKSkpCnByaW50KHNob3cobm11bChpdG9uKDEpLCBpdG9uKDApKSkpCnByaW50KHNob3cobm11bChpdG9uKDEpLCBpdG9uKDEpKSkpCnByaW50KHNob3cobm11bChpdG9uKHgpLCBpdG9uKDEpKSkpCnByaW50KHNob3cobm11bChpdG9uKDEpLCBpdG9uKHgpKSkpCnByaW50KHNob3cobm11bChpdG9uKHgpLCBpdG9uKHgpKSkpCnByaW50KHNob3cobm11bChpdG9uKHgpLCBpdG9uKHgqeCkpKSkKcHJpbnQoc2hvdyhubXVsKGl0b24oeCp4KSwgaXRvbih4KSkpKQ==
([0], '0')
([1], '1')
([3, 2, 1], '123')
-
([0], '0')
([2], '2')
([6, 4, 2], '246')
-
([0], '0')
([0], '0')
([0], '0')
([1], '1')
([3, 2, 1], '123')
([3, 2, 1], '123')
([9, 2, 1, 5, 1], '15129')
([7, 6, 8, 0, 6, 8, 1], '1860867')
([7, 6, 8, 0, 6, 8, 1], '1860867')