CF1857F.Sum and Product
普及/提高-
通过率:0%
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 ;
输入格式
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.
输出格式
For each test case print a line with q numbers — the answers to the queries.
输入输出样例
输入#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.