Opt: 01, 17
This commit is contained in:
@@ -12,8 +12,8 @@ except Exception as e:
|
|||||||
|
|
||||||
res1, res2 = 0, 0
|
res1, res2 = 0, 0
|
||||||
|
|
||||||
for i in range(len(input)):
|
for c in input:
|
||||||
match input[i]:
|
match c
|
||||||
case '(':
|
case '(':
|
||||||
res1 += 1
|
res1 += 1
|
||||||
case ')':
|
case ')':
|
||||||
|
|||||||
45
2015/17.py
45
2015/17.py
@@ -7,36 +7,41 @@ except Exception as e:
|
|||||||
print(f"An error occurred: {e}")
|
print(f"An error occurred: {e}")
|
||||||
|
|
||||||
# Reverse sort input
|
# Reverse sort input
|
||||||
|
|
||||||
|
"""
|
||||||
|
Let:
|
||||||
|
f: i -> X[j] for j in range(len(X)) if i & 1<<j ]
|
||||||
|
|
||||||
|
Then:
|
||||||
|
f is bijection between range(2**len(X)) and the P(X).
|
||||||
|
|
||||||
|
Futhermore:
|
||||||
|
popcount(i) == len(f(i))
|
||||||
|
"""
|
||||||
|
|
||||||
|
def powerset(X):
|
||||||
|
return [[ X[j] for j in range(len(X)) if i & 1<<j ] for i in range(1<<len(X))]
|
||||||
|
|
||||||
|
|
||||||
input= [ int(x) for x in input.split()]
|
input= [ int(x) for x in input.split()]
|
||||||
l = len(input)
|
|
||||||
|
|
||||||
res1, res2, min_popcount = 0, 0, l
|
res1, res2, min_l = 0, 0, len(input)
|
||||||
for i in range(1<<l):
|
|
||||||
"""
|
|
||||||
Let:
|
|
||||||
f: i -> [input[j] for j in range(len(X)) if i & 1<<j ]
|
|
||||||
|
|
||||||
Then:
|
for s in powerset(input):
|
||||||
f is bijection between range(2**len(X)) and the subsets of X.
|
if sum(s) != 150:
|
||||||
|
|
||||||
Futhermore:
|
|
||||||
popcount(i) == len(f(i))
|
|
||||||
"""
|
|
||||||
|
|
||||||
|
|
||||||
if sum([input[j] for j in range(l) if i & 1<<j ]) != 150:
|
|
||||||
continue
|
continue
|
||||||
|
|
||||||
res1 += 1
|
res1 += 1
|
||||||
|
|
||||||
popcount = i.bit_count()
|
l = len(s)
|
||||||
if popcount < min_popcount:
|
|
||||||
min_popcount = popcount
|
if l < min_l:
|
||||||
|
min_l = l
|
||||||
res2 = 0;
|
res2 = 0;
|
||||||
if popcount == min_popcount:
|
if l == min_l:
|
||||||
res2 += 1;
|
res2 += 1;
|
||||||
|
|
||||||
print(res1, res2)
|
print(res1, res2)
|
||||||
|
|
||||||
# One Liner
|
# One Liner
|
||||||
print( sum([ sum( [input[j] for j in range(l) if i & 1<<j ]) == 150 for i in range(1<<l)] ))
|
#print( sum([ sum( [input[j] for j in range(len(input)) if i & 1<<j ]) == 150 for i in range(1<<len(input))] ))
|
||||||
|
|||||||
Reference in New Issue
Block a user