QOJ.ac

QOJ

時間限制: 6 s 記憶體限制: 1024 MB 總分: 10

#8418. 非常喜愛的數列 [C]

统计

3SUM 是一個著名的演算法問題,給定一個整數數列 $c_1, c_2, \dots, c_m$,目標是找到三個索引 $i < j < k$ 使得 $c_i + c_j + c_k = 0$。

目前尚未有針對任意整數數列解決此問題且複雜度顯著優於 $O(m^2)$ 的演算法。幸運的是,Bajtek 並不知道這一點,他決定為他的「非常喜愛數列」(Bardzo Ulubiony Ciąg)解決這個問題。

Bajtek 的「喜愛數列」由 $n$ 個整數 $a_1, a_2, \dots, a_n$ 組成。Bajtek 的「非常喜愛數列」是透過觀察「喜愛數列」中所有 $\frac{n(n+1)}{2}$ 個連續區間,計算每個區間的元素總和,並將這些總和放入一個新的數列中(包含重複值)所產生的。區間總和的排列順序為:先按區間起始索引遞增排序,若起始索引相同,則按區間結束索引遞增排序。

為了增加難度,Bajtek 不僅僅是想找到一組索引 $i < j < k$。他想要知道所有滿足 $i < j < k$ 且對應元素總和為零的索引三元組的確切數量。

請幫助他編寫一個程式來計算這樣的數量!

輸入格式

第一行包含一個整數 $n$ ($1 \le n \le 500$),代表 Bajtek 的「喜愛數列」長度。

接下來一行包含 $n$ 個整數 $a_i$ ($|a_i| \le 20\,000$),代表 Bajtek 的「喜愛數列」中的元素。

輸出格式

輸出僅包含一行,即一個整數,代表 Bajtek 的「非常喜愛數列」中滿足 $i < j < k$ 且對應元素總和為 $0$ 的索引三元組數量。

範例

範例輸入 1

3
7 -4 -2

範例輸出 1

1

範例輸入 2

10
0 0 0 0 0 0 0 0 0 0

範例輸出 2

26235

說明

在第一個範例中,Bajtek 的「非常喜愛數列」為 $[7, 3, 1, -4, -6, -2]$,唯一一組總和為 $0$ 的三個不同元素為 $3 + 1 + (-4)$,因此答案為 $1$。

在第二個範例中,Bajtek 的「非常喜愛數列」由五十五個零組成。對於任意三個索引 $i < j < k$,其對應元素的總和皆為 $0$,這樣的組合共有 $26\,235$ 種。

Discussions

About Discussions

The discussion section is only for posting: General Discussions (problem-solving strategies, alternative approaches), and Off-topic conversations.

This is NOT for reporting issues! If you want to report bugs or errors, please use the Issues section below.

Open Discussions 0
No discussions in this category.

Issues

About Issues

If you find any issues with the problem (statement, scoring, time/memory limits, test cases, etc.), you may submit an issue here. A problem moderator will review your issue.

Guidelines:

  1. This is not a place to publish discussions, editorials, or requests to debug your code. Issues are only visible to you and problem moderators.
  2. Do not submit duplicated issues.
  3. Issues must be filed in English or Chinese only.
Active Issues 0
No issues in this category.
Closed/Resolved Issues 0
No issues in this category.