QOJ.ac

QOJ

Limite de temps : 2 s Limite de mémoire : 64 MB

#22966. Sum of Two Arrays

Statistiques

For an array $A[1..n]$, a subarray is $A[i..j]$ with $1 \le i \le j \le n$. The sum of the subarray $A[i..j]$ is $A[i]+\cdots+A[j]$. You are given two arrays of integers, $A[1..n]$ and $B[1..m]$. Write a program that counts all pairs consisting of a subarray of $A$ and a subarray of $B$ whose sums add up to $t$.

For example, if $A=(1,3,1,2)$, $B=(1,3,2)$, and $t=5$, there are 7 pairs of subarrays of $A$ and $B$ with total sum 5, as follows.

$$ \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} $$

Input

The first line contains $t$ ($-1{,}000{,}000{,}000 \le t \le 1{,}000{,}000{,}000$).

The second line contains the size $n$ of array $A$ ($1 \le n \le 1{,}000$).

The third line contains the elements of $A$ in order, starting with $A[1]$.

The fourth line contains the size $m$ of array $B$ ($1 \le m \le 1{,}000$).

The last line contains the elements of $B$ in order, starting with $B[1]$.

Each element is an integer between $-1{,}000{,}000$ and $1{,}000{,}000$, inclusive. Numbers are separated by one space.

Output

On the first line, print the number of pairs of subarrays of $A$ and $B$ whose sums add up to $t$.

If there are no such pairs with total sum $t$, print 0.

Examples

Input 1

5
4
1 3 1 2
3
1 3 2

Output 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.