1. 程式人生 > >python 牛客真題 幸運的袋子

python 牛客真題 幸運的袋子

DFS,需要剪枝

n=int(input())
num=list(map(int,input().split()))
num.sort()
def dfs(num,mysum,mymul):
    count=0
    for i in range(len(num)):
        if i>0 and num[i]==num[i-1]:
            continue
        mysum+=num[i]
        mymul*=num[i]
        if mysum>mymul:
            count+=1+dfs(
num[i+1:],mysum,mymul) elif num[i]==1: count+=dfs(num[i+1:],mysum,mymul) else: break mysum-=num[i] mymul/=num[i] return count print(dfs(num,0,1))