CF1857F.Sum and Product

普及/提高-

通过率:0%

时间限制:4.00s

内存限制:256MB

AC君温馨提醒

该题目为【codeforces】题库的题目,您提交的代码将被提交至codeforces进行远程评测,并由ACGO抓取测评结果后进行展示。由于远程测评的测评机由其他平台提供,我们无法保证该服务的稳定性,若提交后无反应,请等待一段时间后再进行重试。

题目描述

You have an array aa of length nn.

Your task is to answer qq queries: given x,yx,y, find the number of pairs ii and jj (1≤i<j≤n1 \le i \lt j \le n) that both ai+aj=xa_i + a_j = x and ai⋅aj=ya_i \cdot a_j = y.

That is, for the array [1,3,2][1,3,2] and asking for x=3,y=2x=3,y=2 the answer is 11:

  • i=1i=1 and j=2j=2 fail because 1+3=41 + 3 = 4 and not 3,3, also 1⋅3=31 \cdot 3=3 and not 22;
  • i=1i=1 and j=3j=3 satisfies both conditions;
  • i=2i=2 and j=3j=3 fail because 3+2=53 + 2 = 5 and not 3,3, also 3⋅2=63 \cdot 2=6 and not 22;

你有一个长度为 nn 的数组 aa。

你的任务是回答 qq 个查询:对于给定的 xx 和 yy,找出满足 1≤i<j≤n1 \le i \lt j \le n 且同时满足 ai+aj=xa_i + a_j = x 与 ai⋅aj=ya_i \cdot a_j = y 的下标对 (i,j)(i, j) 的数量。

例如,对于数组 [1,3,2][1,3,2] 以及查询 x=3, y=2x=3,\ y=2,答案为 11:

  • i=1i=1 与 j=2j=2 不满足条件,因为 1+3=4≠31 + 3 = 4 \ne 3,且 1⋅3=3≠21 \cdot 3 = 3 \ne 2;
  • i=1i=1 与 j=3j=3 同时满足两个条件;
  • i=2i=2 与 j=3j=3 不满足条件,因为 3+2=5≠33 + 2 = 5 \ne 3,且 3⋅2=6≠23 \cdot 2 = 6 \ne 2;

输入格式

The first line contains one integer tt (1≤t≤1041\le t\le 10^4) — the number of test cases.

The second line of each test case contains one integer nn (1≤n≤2⋅1051 \le n \le 2\cdot 10^5) — the length of the array aa.

The third line of each test case contains nn integers a1,a2,…,ana_1,a_2,\dots,a_n (1≤∣ai∣≤1091 \le |a_i| \le 10^9) — array aa.

The fourth line of each test case contains the integer qq (1≤q≤2⋅1051 \le q \le 2\cdot 10^5) — the number of requests.

The next qq lines contain two numbers each xx and yy (1≤∣x∣≤2⋅109,1≤∣y∣≤10181 \le |x|\le 2\cdot 10^9,1\le |y|\le 10^{18}) — request.

It is guaranteed that the sum of nn over all test cases does not exceed 2⋅1052\cdot 10^5. This is also guaranteed for the sum of qq values.

第一行包含一个整数 tt(1≤t≤1041\le t\le 10^4)—— 测试用例的数量。

每个测试用例的第二行包含一个整数 nn(1≤n≤2⋅1051 \le n \le 2\cdot 10^5)—— 数组 aa 的长度。

每个测试用例的第三行包含 nn 个整数 a1,a2,…,ana_1,a_2,\dots,a_n(1≤∣ai∣≤1091 \le |a_i| \le 10^9)—— 数组 aa。

每个测试用例的第四行包含一个整数 qq(1≤q≤2⋅1051 \le q \le 2\cdot 10^5)—— 查询的数量。

接下来的 qq 行每行包含两个数 xx 和 yy(1≤∣x∣≤2⋅109, 1≤∣y∣≤10181 \le |x|\le 2\cdot 10^9,\,1\le |y|\le 10^{18})—— 一次查询。

保证所有测试用例的 nn 之和不超过 2⋅1052\cdot 10^5;同样地,所有测试用例的 qq 之和也不超过 2⋅1052\cdot 10^5。

输出格式

For each test case print a line with qq numbers — the answers to the queries.

对于每个测试用例,输出一行包含 qq 个数字——即各查询的答案。

输入输出样例

  • 输入#1

    3
    3
    1 3 2
    4
    3 2
    5 6
    3 1
    5 5
    4
    1 1 1 1
    1
    2 1
    6
    1 4 -2 3 3 3
    3
    2 -8
    -1 -2
    7 12

    输出#1

    1 1 0 0 
    6 
    1 1 3

说明/提示

For the first test case, let's analyze each pair of numbers separately:

  • pair (a1,a2)(a_1,a_2): a1+a2=4a_1 + a_2 = 4, a1⋅a2=3a_1 \cdot a_2 = 3
  • pair (a1,a3)(a_1,a_3): a1+a3=3a_1 + a_3 = 3, a1⋅a3=2a_1 \cdot a_3 = 2
  • pair (a2,a3)(a_2,a_3): a2+a3=5a_2 + a_3 = 5, a2⋅a3=6a_2 \cdot a_3 = 6

From this, we can see that for the first query, the pair (a1,a3)(a_1,a_3) is suitable, for the second query, it is (a2,a3)(a_2,a_3), and there are no suitable pairs for the third and fourth queries.

In the second test case, all combinations of pairs are suitable.

对于第一个测试用例,我们分别分析每一对数字:

  • 数对 (a1,a2)(a_1,a_2):a1+a2=4a_1 + a_2 = 4,a1⋅a2=3a_1 \cdot a_2 = 3
  • 数对 (a1,a3)(a_1,a_3):a1+a3=3a_1 + a_3 = 3,a1⋅a3=2a_1 \cdot a_3 = 2
  • 数对 (a2,a3)(a_2,a_3):a2+a3=5a_2 + a_3 = 5,a2⋅a3=6a_2 \cdot a_3 = 6

由此可知,对于第一个查询,合适的数对是 (a1,a3)(a_1,a_3);对于第二个查询,合适的数对是 (a2,a3)(a_2,a_3);而第三和第四个查询则没有合适的数对。

在第二个测试用例中,所有可能的数对组合均满足条件。

输入解题思路,AI测评打分。不知道怎么写?

首页