题解 | #称砝码#

称砝码

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

全部评论

相关推荐

不愿透露姓名的神秘牛友
06-27 18:00
点赞 评论 收藏
分享
06-15 18:44
黄淮学院 Java
Lynn012:如果是居民楼还是算了吧,看着有点野呢
点赞 评论 收藏
分享
不愿透露姓名的神秘牛友
06-27 15:19
简历上能写3个月吗?
码农索隆:大胆写,主要你能把实习经历包装好,可以看一下我这篇帖子https://www.nowcoder.com/share/jump/4888395581180798063
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务