Traceback (most recent call last): File "E:\two2\python\pycharm\py\suanfa\15\3.py", line 35, in <module> init() File "E:\two2\python\pycharm\py\suanfa\15\3.py", line 17, in init if step[ny] or ny > inf: ~~~~^^^^ IndexError: list index out of range
时间: 2023-07-20 22:34:51 浏览: 104
这个错误的原因是数组 `step` 的大小不够,无法容纳 `ny` 这个下标。根据 C++ 代码,数组 `step` 的大小为 `maxn=1001`,因此 Python 代码中也需要将 `step` 的大小定义为 `maxn+1=1002`。
以下是修改后的 Python 代码:
```python
import queue
maxn = 1001
inf = 1000
step = [0] * (maxn + 1)
def init():
global step
q = queue.Queue()
step[0] = 1
q.put(1)
while not q.empty():
y = q.get()
for x in range(1, y + 1):
ny = y + (y // x)
if step[ny] or ny > inf:
continue
step[ny] = step[y] + 1
q.put(ny)
def solve():
n, k = map(int, input().split())
b = list(map(int, input().split()))
c = list(map(int, input().split()))
dp = [0] * (maxn * maxn)
for i in range(n):
for j in range(k, step[b[i]] - 1, -1):
dp[j] = max(dp[j], dp[j - step[b[i]]] + c[i])
print(dp[k])
if __name__ == '__main__':
init()
t = int(input())
for _ in range(t):
solve()
```
阅读全文