第一行输入一个整数
代表给定的整数个数。
第二行输入
个整数
。
保证数据随机生成。
如果存在满足条件的分配方案,输出
,否则输出
。
4 1 5 -5 1
true
在这个样例中,
数组可以为
,
数组可以为
,满足条件。
3 3 5 8
false
import sys
n=int(input())
n_list=list(map(int,input().split()))
a,b,c=[],[],[]
for num in n_list:
if num%3==0 and num%5!=0:
b.append(num)
elif num%5==0:
a.append(num)
else:
c.append(num)
diff=abs(sum(a)-sum(b))
target=(sum(c)-diff)//2
if (sum(c)-diff)%2!=0&nbs***bsp;(target!=0 and c==[]):
print("false")
sys.exit()
if target in c&nbs***bsp;(target==0 and c==[]):
print("true")
sys.exit()
#穷举子集再分别求和。运算量会大一些
'''result=[[]]
for num in c:
result+=[curr+[num] for curr in result]
for num in result:
if sum(num)==target:
print("true")
sys.exit()
print("false")'''
#不穷举子集,直接穷举子集之和。如果想要更快点可以加个判断条件提前终止循环
import copy
flag=[False]*(max(abs(target),abs(min(c)))+max(target,abs(max(c)))+1)
flag[0]=True
for num in c:
flag_temp=copy.deepcopy(flag)#列表、字典必须用这种“深拷贝”
for i in range(max(target,max(c)),min(target,min(c))-1,-1):
flag_temp[i]=flag[i]&nbs***bsp;flag[i-num if i-num<=max(target,max(c)) else i]
flag=flag_temp
print("true" if flag[target] else "false") import itertools
n = int(input())
A = list(map(int, input().split()))
a = []
b = []
rest = []
for i in A:
if i % 5 == 0:
a.append(i)
else:
if i % 3 == 0:
b.append(i)
else:
rest.append(i)
if rest == []:
if sum(a) == sum(b):
print('true')
else:
print('false')
else:
for i in range(1, int(len(rest)/2)+2):
for combin in itertools.combinations(rest, i):
if sum(a)+sum(list(combin)) == sum(b)+sum(rest)-sum(list(combin))&nbs***bsp;sum(b)+sum(list(combin)) == sum(a)+sum(rest)-sum(list(combin)):
print('true')
exit()
print('false') def dfs(five,three,other):
if not other:
if sum(five) == sum(three):
return True
else:
return False
else:
return dfs(five+other[:1],three,other[1:])&nbs***bsp;dfs(five,three+other[:1],other[1:])
n = int(input())
s = list(map(int,input().split()))
a,b,temp = [],[],[]
for i in s:
if i % 5 == 0:
a.append(i)
elif i % 3 == 0:
b.append(i)
else:
temp.append(i)
print('true' if dfs(a,b,temp) else 'false') import sys from itertools import combinations while True: try: n = input() x = list(map(int,input().split())) a3 =[0] + [i for i in x if i%3==0 and i%5!=0] a5 = [0] + [i for i in x if i%5==0] a0 = [0] + [i for i in x if i not in a3+a5] k = 'false' # print(a3,a5,a0,sep = '\n') for i in range(1,len(a0)+1): if k=='true': break for j in combinations(a0,i): if (sum(a3)+sum(a0)-sum(a5))%2==0 and sum(j) == (sum(a3)+sum(a0)-sum(a5))//2: k = 'true' # print(j,sum(j),(sum(a3)+sum(a0)-sum(a5))) break print(k) except: break
import itertools def arr_grouping(arr): # 分组:能被5整除、能被3整除、其余的元素 five_multiple = [x for x in arr if x % 5 == 0] three_multiple = [x for x in arr if x % 3 == 0 and x not in five_multiple] arr_surplus = [x for x in arr if x not in five_multiple and x not in three_multiple] # 总和计算 sum_five = sum(five_multiple) sum_three = sum(three_multiple) # 尝试将 arr_surplus 组合成与剩余元素相等的总和 total_sum = sum(arr_surplus) target_sum = (total_sum + sum_five - sum_three) / 2 # 如果目标和不是整数,表示不能均分 if target_sum != int(target_sum): return False target_sum = int(target_sum) # 检查 arr_surplus 的组合是否能达到 target_sum for k in range(len(arr_surplus) + 1): # 使用组合来检测目标和 for comb in itertools.combinations(arr_surplus, k): if sum(comb) == target_sum: # sum([]) = 0 return True return False # 输入读取 n = int(input()) arr = [int(x) for x in input().split()] # 输出结果 print(str(arr_grouping(arr)).lower())
def fun(sum_3, sum_5, nums_other):
if len(nums_other) == 0:
if sum_3 == sum_5:
return True
else:
return False
else:
fun1 = fun(sum_3 + nums_other[0], sum_5, nums_other[1::])
fun2 = fun(sum_3, sum_5 + nums_other[0], nums_other[1::])
return fun1 or fun2
n = int(input())
nums = list(map(int, input().split()))
nums_5 = []
nums_3 = []
nums_other = []
for i in nums:
if i % 5 == 0:
nums_5.append(i)
elif i % 3 == 0:
nums_3.append(i)
else:
nums_other.append(i)
sum_3 = sum(nums_3[::])
sum_5 = sum(nums_5[::])
print('true') if fun(sum_3, sum_5, nums_other) else print('false')
import sys
# 广度优先
def BFS(fiveL: list, threeL: list, otherL: list):
# 递归结束条件
if not otherL:
if sum(fiveL) == sum(threeL):
return 1
else:
return 0
res1 = BFS(fiveL + otherL[:1], threeL, otherL[1:])
res2 = BFS(fiveL, threeL + otherL[:1], otherL[1:])
return res1 + res2
# 深度优先
def DFS(fiveL: list, threeL: list, otherL: list, cIndex: int = 0, pathR: list = []):
if pathR == []:
pathR = [0 for i in range(len(otherL))]
# 结果判定
if cIndex == len(otherL):
if sum(fiveL) == sum(threeL):
return True
# 递归至所有数分配完毕
if cIndex < len(otherL):
if pathR[cIndex] == 0:
pathR[cIndex] += 1
if DFS(fiveL + [otherL[cIndex]], threeL, otherL, cIndex + 1, pathR):
return True
elif pathR[cIndex] == 1:
pathR[cIndex] += 1
if DFS(fiveL, threeL + [otherL[cIndex]], otherL, cIndex + 1, pathR):
return True
else:
# 回溯至有未走路线的节点
cIndex -= 1
if pathR[cIndex] == 1:
fiveL.pop()
if pathR[cIndex] == 2:
threeL.pop()
while pathR[cIndex] >= 2:
if cIndex == 0:
return
pathR[cIndex] = 0
cIndex -= 1
if pathR[cIndex] == 1:
fiveL.pop()
if pathR[cIndex] == 2:
threeL.pop()
if DFS(fiveL, threeL, otherL, cIndex, pathR):
return True
n = int(input())
numStr = input()
numStr = numStr.replace("\n", "")
numList = list(map(int, numStr.split()))
aList = [] # a组 5的倍数
bList = [] # b组 3(非5)的倍数
cList = [] # c组 待分组
for i in numList:
if i % 5 == 0:
aList.append(i)
elif i % 3 == 0:
bList.append(i)
else:
cList.append(i)
# 广度优先
# res = BFS(aList, bList, cList)
# if res>0:
# print("true")
# else:
# print("false")
# print(res)
# 深度优先
res = DFS(aList, bList, cList)
if res:
print("true")
else:
print("false")
def add(a5,a3,a0):#看别人代码写的 if len(a0) == 0: if a5 == a3: return True else: return False else: return add(a5+a0[0],a3,a0[1:])&nbs***bsp;add(a5,a3+a0[0],a0[1:]) while 1: try: n = int(input()) m = list(map(int,input().split())) a = [[],[],[]] for i in m: if i%5 == 0: a[0].append(i) elif i%3 == 0: a[1].append(i) else: a[2].append(i) a5,a3 = 0,0 for i in a[0]: a5 += i for i in a[1]: a3 += i print(str(add(a5,a3,a[2])).lower()) except: break
n, nums = input(), list(map(int, input().split()))
res = [0]
for i in nums[:]:
if i % 5 == 0:
res[0] += i
nums.remove(i)
elif i % 3 == 0:
res[0] -= i
nums.remove(i)
for i in nums:
new_res = []
for e in res:
new_res.extend([e + i, e - i])
res = list(set(new_res))
print('true' if 0 in res else 'false') n = int(input())
s = list(map(int, input().split()))
# a存5的倍数 包括15
a = list()
# b存3的倍数 不包括15 <- 用 elif 实现
b = list()
# c存其他的数
c = list()
for i in range(len(s)):
if s[i] % 5 == 0:
a.append(s[i])
elif s[i] % 3 == 0:
b.append(s[i])
else:
c.append(s[i])
# sum求和
a = sum(a)
b = sum(b)
# 递归一下
# 虽说条件写的是 k == len(c)
# 但是其实k=0的时候,第一轮儿计算的时候 abc的值并没有改变
# 第二轮儿的时候 dfs(a+c[k],b,c,k+1)&nbs***bsp; dfs(a,b+c[k],c,k+1) 才改变
# 所以停止条件是 k == len(c) 正好把所有的c里面的值都计算了 a == b时 return True
def dfs(a,b,c,k):
if k == len(c) and a == b:
return True
if k == len(c) and a != b:
return False
res = dfs(a+c[k],b,c,k+1)&nbs***bsp; dfs(a,b+c[k],c,k+1)
return res
if dfs(a,b,c,0):
print('true')
else:
print('false') 递归非常简单直观
n = input()
nums = list(map(int, list(input().split())))
def group(three, five, others):
if len(others) == 0:
if sum(three) == sum(five):
return True
else:
return False
return group(three+[others[0]], five, others[1:]) or group(three, five+[others[0]], others[1:])
three, five, others = [], [], []
for num in nums:
if num % 3 == 0:
three.append(num)
elif num % 5 == 0:
five.append(num)
else:
others.append(num)
if group(three, five, others):
print('true')
else:
print('false')
from itertools import combinations
a = input()
nums = list(map(int,input().split()))
que1 = []
que2 = []
que3 = []
for i in nums:
if i % 5 == 0:
que1.append(i)
elif i%3 == 0:
que2.append(i)
else:
que3.append(i)
if not que3:
if sum(que1) == sum(que2):
print('true')
else:
print('false')
else:
flag = 'false'
sum3 = sum(que3)
for i in range(len(que3)+1):
if flag == 'true':
break
for j in combinations(que3,i):
if sum(que1)+2*sum(j) == sum(que2)+sum3:
flag = 'true'
break
print(flag)