QOJ.ac

QOJ

実行時間制限: 2 s メモリ制限: 64 MB

#22966. 兩個陣列的和

統計

對於陣列 $A[1..n]$,子陣列指滿足 $1 \le i \le j \le n$ 的 $A[i..j]$。子陣列 $A[i..j]$ 的和為 $A[i]+\cdots+A[j]$。給定兩個整數陣列 $A[1..n]$ 和 $B[1..m]$,請撰寫程式,求出所有由 $A$ 的一個子陣列和 $B$ 的一個子陣列組成、且兩個子陣列的和相加等於 $t$ 的子陣列對的數量。

例如,當 $A=(1,3,1,2)$、$B=(1,3,2)$ 且 $t=5$ 時,總和為 5 的 $A$ 和 $B$ 的子陣列對共有以下 7 個。

$$ \begin{aligned} A[1]+B[1]+B[2] &= A[1]+A[2]+B[1] \\ &= A[2]+B[3] \\ &= A[2]+A[3]+B[1] \\ &= A[3]+B[1]+B[2] \\ &= A[3]+A[4]+B[3] \\ &= A[4]+B[2] = 5. \end{aligned} $$

輸入格式

第一行給出 $t$($-1{,}000{,}000{,}000 \le t \le 1{,}000{,}000{,}000$)。

第二行給出陣列 $A$ 的大小 $n$($1 \le n \le 1{,}000$)。

第三行從 $A[1]$ 開始依序給出 $A$ 的各個元素。

第四行給出陣列 $B$ 的大小 $m$($1 \le m \le 1{,}000$)。

最後一行從 $B[1]$ 開始依序給出 $B$ 的各個元素。

每個元素都是不小於 $-1{,}000{,}000$ 且不大於 $1{,}000{,}000$ 的整數,數字之間用一個空格分隔。

輸出格式

在第一行輸出總和為 $t$ 的 $A$ 和 $B$ 的子陣列對的數量。

若不存在總和為 $t$ 的子陣列對,則輸出數字 0。

範例

輸入格式 1

5
4
1 3 1 2
3
1 3 2

輸出格式 1

7

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.