救命有没有大佬帮忙改改刚学dfs,16分
查看原帖
救命有没有大佬帮忙改改刚学dfs,16分
1201924
i_love_python楼主2023/11/24 15:59
n, k = map(int, input().split())
x = list(map(int, input().split()))
path = [0] * k  # 记录数据
ans = 0  # 记录有几个满足条件
def dfs(depth):
	# 出口
	if depth == k:
		# 判断符合题目的条件
		if sum(path) <= 1:
			return
		for i in range(2, sum(path) // 2):
			if sum(path) % i == 0:
				return
		global ans
		ans += 1
		return
	for i in range(n):
		# 对path里的数据进行限制
		if path.count(x[i]) == 0:
			path[depth] = x[i]  # 给path用于记录
			dfs(depth + 1)
dfs(0)
print(ans)
2023/11/24 15:59
加载中...