Codeforces Round 891 (Div. 3)

F - Sum and Product

https://codeforces.com/contest/1857/problem/F

4 seconds / 256 megabytes

standard input / standard output

Problem

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 (1i<jn) that both ai+aj=x and aiaj=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 13=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 32=6 and not 2;

Input

The first line contains one integer t (1t104) — the number of test cases.

The second line of each test case contains one integer n (1n2105) — 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 (1q2105) — the number of requests.

The next q lines contain two numbers each x and y (1|x|2109,1|y|1018) — request.

It is guaranteed that the sum of n over all test cases does not exceed 2105. This is also guaranteed for the sum of q values.

Output

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

Example

Input

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

Output

1 1 0 0
6
1 1 3

Note

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

  • pair (a1,a2): a1+a2=4, a1a2=3
  • pair (a1,a3): a1+a3=3, a1a3=2
  • pair (a2,a3): a2+a3=5, a2a3=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.

题解

给出 x,y,在一个数列中选择 a,b 两个数,满足 a+b=x,ab=y,问有多少种选法。

首先联立两个限制,得到 a,b 之间的关系:

(ab)2=(a+b)24ab=x24y0

为方便,令 x24y=t. 容易得到如果 t<0 则原式不成立,没有任何 a,b 满足条件。

如果 t0,那么:

|ab|=t

不妨设 a>b,那么:

{ab=ta+b=x

直接求解出一对合法的 a,b

{a=12(x+t)b=12(xt)

当然,a,b 如果不是整数也是不合法的,即要满足:t 是整数、x±t 是偶数。

对于给出的一对 x,y,能够直接求解出唯一解 a,b(或无解),那么就可以直接预先维护每个数的个数 cnt(),最后直接输出:

cnt(a)cnt(b)

此时要注意一个细节,如果 t=0 的时候,求解出来 a=b,那么答案就应该是:

cnt(a)(cnt(a)1)2

代码

#include <bits/stdc++.h>
#define endl '\n'
#define int long long

using namespace std;

void solve()
{
    int n;
    cin >> n;
    vector<int> a(n + 10);
    map<int, int> mp;
    for (int i = 1; i <= n; i++)
    {
        cin >> a[i];
        mp[a[i]]++;
    }
    int q;
    cin >> q;
    while (q--)
    {
        int x, y;
        cin >> x >> y;
        int t = x * x - 4 * y;
        if (t < 0)
        {
            cout << 0 << ' ';
        }
        else if (t == 0)
        {
            int cnt = mp[x / 2];
            cout << cnt * (cnt - 1) / 2 << ' ';
        }
        else // if (t > 0)
        {
            int st = sqrt(t);
            if (st * st != t)
            {
                cout << 0 << ' ';
                continue;
            }
            int a = x + st;
            int b = x - st;
            if (a & 1)
            {
                cout << 0 << ' ';
                continue;
            }
            int ca = mp[a / 2];
            int cb = mp[b / 2];
            cout << ca * cb << ' ';
        }
    }
    cout << endl;
}

signed main()
{
    ios::sync_with_stdio(false);
    cin.tie(0);
    cout.tie(0);
    int t = 1;
    cin >> t;
    while (t--)
        solve();
    return 0;
}