from collections import Counter n = int(input()) seq = [int(x) for x in input().split(' ')] prefix_sum = [0] * (n + 1) for i in range(n): prefix_sum[i + 1] = prefix_sum[i] + seq[i] favorite_seq = [] for i in range(n): for j in range(i, n): favorite_seq.append(prefix_sum[j + 1] - prefix_sum[i]) seq_count = Counter(favorite_seq) result = 0 for a1, v1 in seq_count.items(): if a1 == 0: result += v1 * (v1 - 1) * (v1 - 2) else: result += v1 * (v1 - 1) * seq_count[-2 * a1] for a2, v2 in seq_count.items(): if a1 != a2 and - a1 - a2 != a1 and - a1 - a2 != a2: result += v1 * v2 * seq_count[-a1 - a2] print(result // 6)
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 | from collections import Counter n = int(input()) seq = [int(x) for x in input().split(' ')] prefix_sum = [0] * (n + 1) for i in range(n): prefix_sum[i + 1] = prefix_sum[i] + seq[i] favorite_seq = [] for i in range(n): for j in range(i, n): favorite_seq.append(prefix_sum[j + 1] - prefix_sum[i]) seq_count = Counter(favorite_seq) result = 0 for a1, v1 in seq_count.items(): if a1 == 0: result += v1 * (v1 - 1) * (v1 - 2) else: result += v1 * (v1 - 1) * seq_count[-2 * a1] for a2, v2 in seq_count.items(): if a1 != a2 and - a1 - a2 != a1 and - a1 - a2 != a2: result += v1 * v2 * seq_count[-a1 - a2] print(result // 6) |