QOJ.ac

QOJ

Time Limit: 2 s Memory Limit: 64 MB

#22966. 2 つの配列の和

Statistics

配列 $A[1..n]$ に対し、$1 \le i \le j \le n$ を満たす $A[i..j]$ を部分配列と呼びます。部分配列 $A[i..j]$ の和は $A[i]+\cdots+A[j]$ です。2 つの整数配列 $A[1..n]$ と $B[1..m]$ が与えられます。$A$ の部分配列と $B$ の部分配列を 1 つずつ選び、それぞれの和を足すと $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} $$

入力

1 行目に $t$($-1{,}000{,}000{,}000 \le t \le 1{,}000{,}000{,}000$)が与えられます。

2 行目に配列 $A$ の大きさ $n$($1 \le n \le 1{,}000$)が与えられます。

3 行目に $A$ の要素が $A[1]$ から順番に与えられます。

4 行目に配列 $B$ の大きさ $m$($1 \le m \le 1{,}000$)が与えられます。

最後の行に $B$ の要素が $B[1]$ から順番に与えられます。

各要素は $-1{,}000{,}000$ 以上 $1{,}000{,}000$ 以下の整数で、数の間は 1 つの空白で区切られます。

出力

1 行目に、合計が $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.