题解 | #称砝码#

称砝码

https://www.nowcoder.com/practice/f9a4c19050fc477e9e27eb75f3bfd49c?tpId=37&tqId=21264&rp=1&ru=/exam/oj/ta&qru=/exam/oj/ta&sourceUrl=%2Fexam%2Foj%2Fta%3Fdifficulty%3D4%26page%3D1%26pageSize%3D50%26search%3D%26tpId%3D37%26type%3D37&difficulty=undefined&judgeStatus=undefined&tags=&title=

一眼过去就是递归,看完题解发现还是大佬们的方法快,给跪了

from functools import cache

@cache
def dfs(i, total):
    if i == n:
        ans.add(total)
        return
    for j in range(X[i]+1):
        v = j*M[i]
        total += v
        dfs(i+1, total)
        total -= v

while True:
    try:
        n = int(input().strip())
        M = list(map(int, input().strip().split()))
        X = list(map(int, input().strip().split()))
        ans = set()
        dfs(0, total=0)
        print(len(ans))
    except:
        break

全部评论

相关推荐

耀孝女:就是你排序挂了
点赞 评论 收藏
分享
评论
点赞
收藏
分享
牛客网
牛客企业服务