python3 为什么用了记忆化还会有一个TLE

P1464 Function

Chen_Yu_Wei @ 2024-08-12 17:19:02

def digui(a,b,c,memo):
    if a<=0 or b<=0 or c<=0:
        return 1
    if a>20 or b>20 or c>20:
        return digui(20,20,20,memo)
    if (a,b,c) in memo:
        return memo[(a,b,c)]
    if a<b and b<c:
        result = digui(a,b,c-1,memo)+digui(a,b-1,c-1,memo)-digui(a,b-1,c,memo)
    else:
        result = digui(a-1,b,c,memo)+digui(a-1,b-1,c,memo)+digui(a-1,b,c-1,memo)-digui(a-1,b-1,c-1,memo)
    memo[(a,b,c)] = result
    return memo[(a,b,c)]

while 1:
    memo = {}
    a,b,c = map(int,input().split())
    if a==-1 and b==-1 and c==-1:
        break
    s = digui(a,b,c,memo)
    print(f"w({a}, {b}, {c}) = {s}")

by lznxes @ 2024-10-31 22:01:53

Py 3 跑的慢


|