To count number of quadruplets with sum = 0

Paul Rubin http
Fri Mar 16 04:05:03 CET 2007


"n00m" <n00m at narod.ru> writes:
> http://www.spoj.pl/problems/SUMFOUR/
> 3
> 0 0 0 0
> 0 0 0 0
> -1 -1 1 1
> Answer for this input data is 33.

f = open('input1')
npairs = int(f.readline())

quads = [map(int, f.readline().split()) for i in xrange(npairs)]
assert len(quads) == npairs

da = {}

for p in quads:
    for q in quads:
        z = p[2] + q[3]
        da[z] = da.get(z,0) + 1

print sum([da.get(-(p[0]+q[1]), 0) for p in quads for q in quads])



More information about the Python-list mailing list