改写成python语言#include<bits/stdc++.h> using ll=long long; const int maxn=1e3+1e2; const int inf=1e3; int step[maxn]; void init(){ std::queue<int>q; step[0]=1; q.push(1); while(!q.empty()){ int y=q.front();q.pop(); for(int x=1;x<=y;x++){ int ny=y+(y/x); if(step[ny]||ny>inf){ continue; } step[ny]=step[y]+1; q.push(ny); } } } void solve() { int n,k;std::cin>>n>>k; std::vector<int>b(n),c(n); for(auto&i:b)std::cin>>i; for(auto&i:c)std::cin>>i; std::vector<int> dp(maxn*maxn); for(int i=0;i<n;i++){ for(int j=k;j>=step[b[i]];j--){ dp[j]=std::max(dp[j],dp[j-step[b[i]]]+c[i]); } } std::cout<<dp[k]<<'\n'; } int main() { init(); std::cin.tie(nullptr)->sync_with_stdio(false); int t;std::cin>>t; while(t--)solve(); return 0; }
时间: 2024-03-12 12:46:42 浏览: 95
以下是改写后的 Python 代码:
```python
import queue
maxn = 1001
inf = 1000
step = [0] * maxn
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()
```
请注意,该段代码包含了输入输出部分,因此需要在运行时手动输入相关数据。
阅读全文