CF1857F Sum and Product 题解
违规用户名968688 · · 题解
这题题意相对清晰明了,就不给简要题意了。 ::::info[题解]
首先正常暴力再去优化不太现实,我们考虑从式子本身出发。
题目中让我们求有多少对
然后我们就可以解方程了,根据韦达定理易知
我们可以通过求根公式解得
然后就可以通过
当
当
否则,方程式无解,输出
:::: ::::info[代码]
#include <bits/stdc++.h>
#define int long long
using namespace std;
int a[200005];
map<int,int> cnt;
signed main()
{
int t;
scanf("%lld",&t);
while(t--)
{
int n;
scanf("%lld",&n);
for(int i=1;i<=n;i++)
{
scanf("%lld",&a[i]);
cnt[a[i]]++;
}
int q;
scanf("%d",&q);
while(q--)
{
int x,y;
scanf("%d%d",&x,&y);
if(x*x-4*y<0) printf("0 ");
else if(x*x-4*y==0)
{
if(x%2!=0) printf("0 ");
else printf("%lld ",cnt[x/2]*(cnt[x/2]-1)/2);
}
else
{
int qwq = x*x-4*y;
int qaq = sqrt(qwq);
if(qaq*qaq!=qwq)//不要忘记判断是否为整数
printf("0 ");
else
{
int qwq1 = x-qaq;
int qwq2 = x+qaq;
printf("%d ",cnt[qwq1/2]*cnt[qwq2/2]);
}
}
}
puts("");
cnt.clear();//多测不清空,_____
}
}