CF1857F.Sum and Product
普及/提高-
通过率:0%
时间限制:4.00s
内存限制:256MB
AC君温馨提醒
该题目为【codeforces】题库的题目,您提交的代码将被提交至codeforces进行远程评测,并由ACGO抓取测评结果后进行展示。由于远程测评的测评机由其他平台提供,我们无法保证该服务的稳定性,若提交后无反应,请等待一段时间后再进行重试。
题目描述
You have an array a of length n.
Your task is to answer q queries: given x,y, find the number of pairs i and j (1≤i<j≤n) that both ai+aj=x and ai⋅aj=y.
That is, for the array [1,3,2] and asking for x=3,y=2 the answer is 1:
- i=1 and j=2 fail because 1+3=4 and not 3, also 1⋅3=3 and not 2;
- i=1 and j=3 satisfies both conditions;
- i=2 and j=3 fail because 3+2=5 and not 3, also 3⋅2=6 and not 2;
你有一个长度为 n 的数组 a。
你的任务是回答 q 个查询:对于给定的 x 和 y,找出满足 1≤i<j≤n 且同时满足 ai+aj=x 与 ai⋅aj=y 的下标对 (i,j) 的数量。
例如,对于数组 [1,3,2] 以及查询 x=3, y=2,答案为 1:
- i=1 与 j=2 不满足条件,因为 1+3=4=3,且 1⋅3=3=2;
- i=1 与 j=3 同时满足两个条件;
- i=2 与 j=3 不满足条件,因为 3+2=5=3,且 3⋅2=6=2;
输入格式
The first line contains one integer t (1≤t≤104) — the number of test cases.
The second line of each test case contains one integer n (1≤n≤2⋅105) — the length of the array a.
The third line of each test case contains n integers a1,a2,…,an (1≤∣ai∣≤109) — array a.
The fourth line of each test case contains the integer q (1≤q≤2⋅105) — the number of requests.
The next q lines contain two numbers each x and y (1≤∣x∣≤2⋅109,1≤∣y∣≤1018) — request.
It is guaranteed that the sum of n over all test cases does not exceed 2⋅105. This is also guaranteed for the sum of q values.
第一行包含一个整数 t(1≤t≤104)—— 测试用例的数量。
每个测试用例的第二行包含一个整数 n(1≤n≤2⋅105)—— 数组 a 的长度。
每个测试用例的第三行包含 n 个整数 a1,a2,…,an(1≤∣ai∣≤109)—— 数组 a。
每个测试用例的第四行包含一个整数 q(1≤q≤2⋅105)—— 查询的数量。
接下来的 q 行每行包含两个数 x 和 y(1≤∣x∣≤2⋅109,1≤∣y∣≤1018)—— 一次查询。
保证所有测试用例的 n 之和不超过 2⋅105;同样地,所有测试用例的 q 之和也不超过 2⋅105。
输出格式
For each test case print a line with q numbers — the answers to the queries.
对于每个测试用例,输出一行包含 q 个数字——即各查询的答案。
输入输出样例
输入#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): a1+a2=4, a1⋅a2=3
- pair (a1,a3): a1+a3=3, a1⋅a3=2
- pair (a2,a3): a2+a3=5, a2⋅a3=6
From this, we can see that for the first query, the pair (a1,a3) is suitable, for the second query, it is (a2,a3), 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):a1+a2=4,a1⋅a2=3
- 数对 (a1,a3):a1+a3=3,a1⋅a3=2
- 数对 (a2,a3):a2+a3=5,a2⋅a3=6
由此可知,对于第一个查询,合适的数对是 (a1,a3);对于第二个查询,合适的数对是 (a2,a3);而第三和第四个查询则没有合适的数对。
在第二个测试用例中,所有可能的数对组合均满足条件。
输入解题思路,AI测评打分。不知道怎么写?