def inv_count(a, m=(1<<32)):
if not m or not a:
return 0
count = 0
ones = []
zeros = []
for n in a:
if n & m:
ones.append(n & ~m)
else:
count += len(ones)
zeros.append(n & ~m)
m /= 2
return count + inv_count(ones, m) + inv_count(zeros, m)
print inv_count([1, 2, 3, 4, 5])
print inv_count([5, 4, 3, 2, 1])
ZGVmIGludl9jb3VudChhLCBtPSgxPDwzMikpOgogIGlmIG5vdCBtIG9yIG5vdCBhOgogICAgICByZXR1cm4gMAogIGNvdW50ID0gMAogIG9uZXMgPSBbXQogIHplcm9zID0gW10KICBmb3IgbiBpbiBhOgogICAgaWYgbiAmIG06CiAgICAgIG9uZXMuYXBwZW5kKG4gJiB+bSkKICAgIGVsc2U6CiAgICAgIGNvdW50ICs9IGxlbihvbmVzKQogICAgICB6ZXJvcy5hcHBlbmQobiAmIH5tKQogIG0gLz0gMgogIHJldHVybiBjb3VudCArIGludl9jb3VudChvbmVzLCBtKSArIGludl9jb3VudCh6ZXJvcywgbSkKCgpwcmludCBpbnZfY291bnQoWzEsIDIsIDMsIDQsIDVdKQpwcmludCBpbnZfY291bnQoWzUsIDQsIDMsIDIsIDFdKQ==