Distinct Arithmetic Sequences

3.7

3 votes
Count sort, Sorting, Algorithms
Problem

You are given two non-decreasing sequences A=(A1,A2,...,AM) and B=(B1,B2,...,BN).

You can choose any two indices i and j, and then swap Ai and Bj.

Note that the you can do the operations as many times as you want, and your goal is to transform A into an arithmetic sequence. After opeartions, you can rearrange the sequence A.

Now, determine how many distinct arithmetic sequences you can obtain.

Input Format

  • The first line contains a single integer T — the number of test cases.
  • For each testcase:
    • The first line contains two integer M and N — the lengths of the sequences A and B, respectively.
    • The second line contains M integers A1,A2,...,AM.
    • The third line contains N integers B1,B2,...,BN.

Output Format

  • For each testcase, output the number of distinct arithmetic sequences you can obtain.

Constraints

  • 1T103
  • 2NM105
  • MAi,BjM
  • The sum of M+N across all test cases does not exceed 2107.

 

Time Limit: 1
Memory Limit: 256
Source Limit:
Explanation

In the first case, the sequence A could be transfromed into (1,0,1), (1,0,1)(0,1,2) or (2,1,0) by operations. 

In the second case, the sequence A could be transfromed into (1,1,1) or (0,0,0) by operations.

Editor Image

?