from itertools import combinations_with_replacement
def test(kom):
gram = [ 0 for i in range(n) ]
for i in kom:
gram[i]+=1
mg = [ 0 for i in range(n) ]
for i in range(n):
x = gram[i]
suml=sum(gram[0:i])
sumr=sum(gram[i+1:])
mg[i] = (
# x*suml*(suml-1)//2 +
# x*sumr*(sumr-1)//2 +
x*suml*sumr +
x*(x-1)//2*suml +
x*(x-1)//2*sumr +
x*(x-1)*(x-2)//6)
for i in range(n):
if miasta[i] > mg[i]:
return(False)
return(True)
t = int(input())
for i in range(t):
n = int(input())
gracze = [ i for i in range(n) ];
miasta = [ int(i) for i in input().split() ]
znal = False
for licz in range(sum(miasta)*(n+5)):
for komp in combinations_with_replacement(gracze, licz):
if test(komp):
gram = [ 0 for i in range(n) ]
for i in komp:
gram[i]+=1
print(licz)
znal = True
break
if znal:
break
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 | from itertools import combinations_with_replacement def test(kom): gram = [ 0 for i in range(n) ] for i in kom: gram[i]+=1 mg = [ 0 for i in range(n) ] for i in range(n): x = gram[i] suml=sum(gram[0:i]) sumr=sum(gram[i+1:]) mg[i] = ( # x*suml*(suml-1)//2 + # x*sumr*(sumr-1)//2 + x*suml*sumr + x*(x-1)//2*suml + x*(x-1)//2*sumr + x*(x-1)*(x-2)//6) for i in range(n): if miasta[i] > mg[i]: return(False) return(True) t = int(input()) for i in range(t): n = int(input()) gracze = [ i for i in range(n) ]; miasta = [ int(i) for i in input().split() ] znal = False for licz in range(sum(miasta)*(n+5)): for komp in combinations_with_replacement(gracze, licz): if test(komp): gram = [ 0 for i in range(n) ] for i in komp: gram[i]+=1 print(licz) znal = True break if znal: break |
English