QOJ.ac

QOJ

Time Limit: 2 s Memory Limit: 64 MB

#22966. 两个数组的和

Statistics

对于数组 $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.