It's a simple hash problem.
You hash the number.and you get the answer.
The portal:http://acm.tju.edu.cn/toj/showp4110.html
#include <cstdio>
#include <cstdlib>
#include <cmath>
#include <cstring>
long long f[1005];
void Deal_with(){
int T,n,q;
scanf("%d",&T);
while(T--){
memset(f,0,sizeof(f));
scanf("%d",&n);
for(int i=0;i<n;i++){
int tempa;
scanf("%d",&tempa);
f[tempa] ++;
}
scanf("%d",&q);
long long ans;
for(int i=0;i<q;i++){
int tempa;
ans = 0;
scanf("%d",&tempa);
for(int i=0;i<=1000;i++){
if(f[i] == 0)continue;
if(tempa - i < 0 || tempa - i > 1000)
continue;
if(i == tempa - i)ans += f[i] * (f[i] - 1);
else ans += f[i] * f[tempa - i];
}
printf("%lld\n",ans);
}
}
}
int main(void){
//freopen("a.in","r",stdin);
Deal_with();
return 0;
}