k I
j k ZZZZX z
xw k C vp yQ KD xw z j k ZZZZX z
j k ZZZD z xw bZ ZX
k XX z qs xw vp xw xw vp xw vp vp vp k I Xj ZZD hd
xw yQ K k ZZZZX xo exx
qs yQ XA xw xw xw xw z xw bZ K
xw xw k I
j k ZZZZX z
xw k C vp yQ XA hd k I z j k ZZZZX z
j xw k A vp bZ ZX
k ZZZZX z qs xw vp xw xw vp xw vp vp vp k I Xj ZZD hd
xw yQ K k ZZZZX xo exx
qs yQ F k ZZZZK xo
vp
xw xw z qs xw bZ X xw k I z xw Xj K
qs xw bZ KA vp qs xw Xj C hd
qs z xw xw xw xw z qs
xw xw xw xw z qs k I qs k I z xw Xj ZC
qs bZ ZZZX qs xw yQ C hd xw
k I vp qs k I qs
xw Xj ZZC hd hd z Kz ZZD
k I z xw xw xw xw z qs k I qs k I Xj ZZZZF
z
xw xw z qs xw bZ X xw k I z xw Xj K
qs xw bZ KA vp qs xw Xj C hd
z qs xw
xw xw z qs xw bZ X xw k I z xw Xj K
qs xw bZ KA vp qs xw Xj C hd
z vp
xw xw z qs
xw xw z qs
k I qs
xw bZ ZZX k I z qs k I vp
xw k ZA z yQ ZA hd qs k I vp qs k I Xj ZZKD
qs xw Xj ZZK
hd qs xw Xj ZZZZ hd
k ZZZZKD vp xo xw Xj K
在线尝试!
我使用的是C解释器,因为如果不满足向后跳转的条件,那么TIO上的Python解释器会烦人地执行该地址。Python解释器的一个简单解决方法是填充一些nop以使地址变为nop。我相信都不正确:
C Python My interpretation
IP after skiping N words IP+N+1 IP+N+2 IP+N+2
IP after skiping back N words IP-N IP-N+1 IP-N+2
IP after not skiping N words IP+2 IP+2 IP+2
IP after not skiping back N words IP+2 IP+1 IP+2
输入应为两个整数,中间用一个空格隔开,且不尾随换行符。
如果每个单元格都可以存储任意大的值(不限于0-255),则该理论上适用于所有整数。但是| A | + | B |会溢出。> 22.并且|| A | + | B |会非常缓慢地运行 > 6.因此,您实际可以测试的案例并不多,针对这些案例的if-else解决方案可能更短。
想法是通过将值减为0并求和所有中间值来计算三角数T(N)= N(N + 1)/ 2。然后我们可以得到答案A * B = T(A + B)-T(A)-T(B)。
但是要计算所有三个值是很棘手的。为此,它首先计算T(A + B)-A,将A的副本留在堆栈中,然后再加回来,然后用完输入B。然后递归地找到小于该三角形的最大三角数T( A + B-1),但特殊情况为零。我们可以取回B = T(A + B)-A-T(A + B-1)并从那里计算T(B)。
N是一个三角形数,如果它等于小于N的最大三角形数,再加上小于N的非负三角形数,则该数以O(2 ^(T(A + B)-A))表示并且是程序中最慢的部分。
k I Push 1
j k ZZZZKAAA z Input and decrement by 48.
xw k AAA vp yQ (input_a_loop) If the character was '-':
xw z j k ZZZZKAAA z Replace with 0 and input another.
input_a_loop:
j k ZZZAA z xw bZ (input_a_end) Input and break if it is a space.
k ZKA z qs xw vp xw xw vp xw vp vp vp Otherwise multiply the previous
value by 10 and add.
k I Xj (input_a_loop) Continue the loop.
input_a_end: hd Discard the space.
xw yQ (check_sign) k ZZZZKAAA xo exx If A=0, print 0 and exit.
Stack: ?, A_is_positive, A
check_sign:
qs yQ (check_sign_else) If A is positive... or not,
xw xw xw xw z xw bZ (check_sign_end) in either cases, push 2 copies
check_sign_else: xw xw k I of A and the negated flag back
check_sign_end: as a constant.
Stack: A, A, A, A_is_negative
j k ZZZZKAAA z Similar for B.
xw k AAA vp yQ (input_b_loop) If the character was '-':
hd k I z j k ZZZZKAAA z Decrement the flag and input another.
input_b_loop:
j xw k A vp bZ (input_b_end) EOF is checked instead of a space.
k ZZZZKAAA z qs xw vp xw xw vp xw vp vp vp
k I Xj (input_b_loop)
input_b_end: hd
xw yQ (output_sign) k ZZZZKAAA xo exx If B=0, print 0 and exit.
Stack: A, A, A, A*B_is_negative, B
output_sign:
qs yQ (output_sign_end) k ZZZZK xo If negative, output '-'.
output_sign_end:
vp Add. Stack: A, A, A+B
xw xw z qs Insert a 0. Stack: A, A, 0, A+B.
xw bZ { xw k I z xw Xj } Copy and decrement while nonzero.
Stack: A, A, 0, A+B, A+B-1, ..., 0
qs xw bZ { vp qs xw Xj } hd Add while the second value in the
stack is nonzero.
Stack: A, A, T(A+B)
qs z xw xw xw xw z qs Stack: A, C0=T(A+B)-A, C0, F0=0, C0
expand_loop:
xw xw xw xw z qs k I qs Stack: A, C0, C0, F0=0,
..., [P=C, P, S=0, F=1], C
dec_expand: k I z xw Xj (expand_loop) Decrement and continue if nonzero.
Stack: [P=1, P, S, F], C=0
The last number 0 is assumed to
be a triangular number.
test: qs bZ (extract_end) If F=0, break.
qs xw yQ (test_not_first) hd xw If S=0, it's the first triangular
number below previous C. Set S=C.
test_not_first: k I vp qs k I qs S+=1 and restore F=1.
xw Xj (dec_expand) If C!=0, recursively expand from C-1.
hd hd z Kz (test) If S=P, P is a triangular number,
return to the previous level.
k I z xw xw xw xw z qs k I qs Otherwise, decrement P and try again.
k I Xj (dec_expand)
extract_end: Stack: A, C0, C0, T(A+B-1)
z Subtract and get B.
xw xw z qs xw bZ { xw k I z xw Xj } Computes T(B).
qs xw bZ { vp qs xw Xj } hd
Stack: A, C0, T(B)
z qs xw Stack: C0-T(B), A, A
xw xw z qs xw bZ { xw k I z xw Xj } Computes T(A).
qs xw bZ { vp qs xw Xj } hd
z vp Get A*B=(C0-T(B))+(A-T(A))
xw xw z qs Stack: 0, X=A*B
divide: xw xw z qs Stack: 0, ..., Y=0, X
subtract: k I qs Stack: 0, ..., Y, Z=1, X
xw bZ { While X!=0:
k I z qs k I vp X-=1, Z+=1.
xw k ZA z yQ (not_ten) But if Z=11:
hd qs k I vp qs k I Xj (subtract) Y+=1, reset Z and restart the loop.
not_ten: qs xw Xj }
hd qs xw Xj (divide) Put Z under Y and make Y the new X,
continue the loop if X!=0.
hd Discard X.
print_loop:
k ZZZZKAA vp xo xw Xj (print_loop) Add each cell by 47 and print.