题解 | #kotori和素因子#

kotori和素因子

https://www.nowcoder.com/practice/7b1c858a3e7a41ed8364178979eaae67

import sys

n = list(map(int,input().split()))[0]
arr = list(map(int,input().split()))
dp = [[] for _ in range(n)]
sum_list = []
count = []

a0 = []
su_set = set() 
# 把2—1000的素数找出来:
for i in range(2,1001):
    count = 0
    for j in range(1,i+1):
        if i%j==0:
            count +=1
    if count == 2:
        su_set.add(i)

# 把素因子找出来
for i in range(n):
    for j in range(1,arr[i]+1):
        if arr[i]%j==0 and (j in su_set):
            dp[i].append(j)


# 找素因子之和的最小值,从最上面开始遍历
j=0
def dfs(dp,tmp,n):
    if not dp :
        if len(tmp)==n:
            sum_list.append(sum(tmp))
        return True
    
    else:
        for i in dp[0]:
            if i not in tmp:
                dfs(dp[1:],tmp+[i],n)

dfs(dp,a0,n)

if len(sum_list)==0:
    print(-1)
else:
    print(min(sum_list))

关于递归,我感觉我还是不会哈哈哈

全部评论

相关推荐

1 收藏 评论
分享
牛客网
牛客企业服务