题目 | Sum and Product
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
Your task is to answer
That is, for the array
and fail because and not also and not ; and satisfies both conditions; and fail because and not also and not ;
Input
The first line contains one integer
The second line of each test case contains one integer
The third line of each test case contains
The fourth line of each test case contains the integer
The next
It is guaranteed that the sum of
Output
For each test case print a line with
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
: , - pair
: , - pair
: ,
From this, we can see that for the first query, the pair
题解
给出
首先联立两个限制,得到
为方便,令
如果
不妨设
直接求解出一对合法的
当然,
对于给出的一对
此时要注意一个细节,如果
代码
#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;
}
本文采用 CC BY-SA 4.0 许可,本文 Markdown 源码:Haotian-BiJi