import sys from typing import List from collections import Counter def find_3sum(values:List[int])->int: result = 0 values.sort() # print(values) n = len(values) counter = Counter(values) for i in range(len(values)-1): for j in range(i+1, len(values)): value = -(values[i] + values[j]) count = counter.get(value, 0) if values[i] == value: count -= 1 if values[j] == value: count -= 1 if count > 0: result += count return result//3 def fill_missing(values:List[int])->List[int]: result = [] for i in range(len(values)): tmp = 0 for j in range(i, len(values)): tmp += values[j] result.append(tmp) return result sys.stdin.readline() values = sys.stdin.readline().split() values = [int(x) for x in values] print(find_3sum(fill_missing(values)))
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 | import sys from typing import List from collections import Counter def find_3sum(values:List[int])->int: result = 0 values.sort() # print(values) n = len(values) counter = Counter(values) for i in range(len(values)-1): for j in range(i+1, len(values)): value = -(values[i] + values[j]) count = counter.get(value, 0) if values[i] == value: count -= 1 if values[j] == value: count -= 1 if count > 0: result += count return result//3 def fill_missing(values:List[int])->List[int]: result = [] for i in range(len(values)): tmp = 0 for j in range(i, len(values)): tmp += values[j] result.append(tmp) return result sys.stdin.readline() values = sys.stdin.readline().split() values = [int(x) for x in values] print(find_3sum(fill_missing(values))) |