εVтLIàgãεYSym}OIyKå}˜P
将输入作为列表(即[526,853]
)。
在线尝试或验证范围内的大多数测试用例[1,999]
。
类似于我下面的旧答案,不同之处在于该[1,n]
列表被硬编码为[1,100]
,并且两次创建笛卡尔列表,每个输入映射都创建一次,这是性能的主要瓶颈。
旧的26个字节的答案更适合于性能:
Z©bgL®gãUεVXεYSym}OsN>èå}P
在此版本中,我以字节为单位进行了交易,以使性能更好,因此可以[1,1000]
轻松运行。[1,9999]
在TIO上大约一秒钟即可完成包含范围内数字的测试用例。测试用例[10000,99999]
在TIO上大约需要10-15秒的范围内。超过此时间将超时。
在线尝试或验证数字范围内的所有测试用例[1,9999]
。
说明:
Z # Push the max of the (implicit) input-list (without popping)
# i.e. [526,853] → 853
© # Store it in the register (without popping)
b # Convert to binary
# i.e. 853 → 1101010101
g # Take its length
# i.e. 1101010101 → 10
L # Pop and push a list [1, n]
# i.e. 10 → [1,2,3,4,5,6,7,8,9,10]
® # Push the max from the register
g # Take its length
# i.e. 853 → 3
ã # Cartesian product the list that many times
# i.e. [1,2,3,4,5,6,7,8,9,10] and 3
# → [[1,1,1],[1,1,2],[1,1,3],...,[10,10,8],[10,10,9],[10,10,10]]
U # Pop and store it in variable `X`
ε } # Map both values of the input list:
V # Store the current value in variable `Y`
Xε } # Map `y` over the numbers of variable `X`
Y # Push variable `Y`
S # Convert it to a list of digits
# i.e. 526 → [5,2,6]
ym # Take each digit to the power of the current cartesian product sublist
# i.e. [5,2,6] and [3,9,3] → [125,512,216]
O # Take the sum of each inner list
# i.e. [[5,2,6],[5,2,36],[5,2,216],...,[125,512,216],...]
# → [13,43,223,...,853,...]
s # Swap to push the (implicit) input
N> # Push the index + 1
# i.e. 0 → 1
è # Index into the input-list (with automatic wraparound)
# i.e. [526,853] and 1 → 853
å # Check if it's in the list of sums
# i.e. [13,43,223,...,853,...] and 853 → 1
P # Check if it's truthy for both both (and output implicitly)
# i.e. [1,1] → 1
17 2401 -> false
。我差点被绊倒了。