5258. 友好数对 (Standard IO)
Time Limits: 1000 ms Memory Limits: 524288 KB Detailed Limits
Description
Input
Output
Sample Input
3 5
1 8 13
7 5 4 8 3
Sample Output
7
Data Constraint
Hint
题解
这题关键在于hash
做法很简单,每次取一个ai,每一位分别改1,加入hash
全部改完后,取一个bi每一位分别改1,在hash中查找个数
最后,减去本来ai=bi的个数,再除以2
由于会卡常,所以不优美的hash是过不了的
代码
#include<cstdio>
#include<algorithm>
#include<vector>
#define mo 8000017
#define ll long long
using namespace std;
struct hash{
long num[mo],sum[mo];
bool check(long x,long y)
{
return !num[y]||(num[y]==x);
}
void push(long x)
{ long y,i;
y=(ll)x*x%mo;
for(i=0;!check(x,(y+(ll)i*i%mo)%mo);i++);
num[(y+(ll)i*i%mo)%mo]=x;
sum[(y+(ll)i*i%mo)%mo]++;
}
long find(long x)
{ long y,i;
y=(ll)x*x%mo;
for(i=0;!check(x,(y+(ll)i*i%mo)%mo);i++);
return sum[(y+(ll)i*i%mo)%mo];
}
}hash_num,hash_same;
int main()
{ long n,m,i,j,x;
long long ans=0,same=0;
scanf("%ld%ld",&n,&m);
for(i=1;i<=n;i++){
scanf("%ld",&x);
hash_same.push(x);
for(j=0;j<30;j++)
hash_num.push(x^(1<<j));
}
for(i=1;i<=m;i++){
scanf("%ld",&x);
same+=hash_same.find(x);
for(j=0;j<30;j++)
ans+=hash_num.find(x^(1<<j));
}
printf("%lld\n",(ans-same*30)/2);
return 0;
}