思路分析
考虑meeting in the middle,枚举其中一位,查另一位的数量,减去自身重复的部分
AC代码
#include <bits/stdc++.h>
#define fi first
#define se second
#define pb push_back
#define mp make_pair
#define fun function
#define sz(x) (int)(x).size()
#define lowbit(x) (x)&(-x)
#define all(x) (x).begin(),(x).end()
#define mem(a,b) memset(a,b,sizeof(a))
namespace FastIO {
#define BUF_SIZE 100000
#define OUT_SIZE 100000
bool IOerror=0;
inline char nc() {
static char buf[BUF_SIZE],*p1=buf+BUF_SIZE,*pend=buf+BUF_SIZE;
if(p1==pend) {
p1=buf;
pend=buf+fread(buf,1,BUF_SIZE,stdin);
if(pend==p1) {
IOerror=1;
return -1;
}
}
return *p1++;
}
inline bool blank(char ch) {
return ch==' '||ch=='\n'||ch=='\r'||ch=='\t';
}
template<class T> inline bool read(T &x) {
bool sign=0;
char ch=nc();
x=0;
for(; blank(ch); ch=nc());
if(IOerror)return false;
if(ch=='-')sign=1,ch=nc();
for(; ch>='0'&&ch<='9'; ch=nc())x=x*10+ch-'0';
if(sign)x=-x;
return true;
}
template<class T,class... U>bool read(T& h,U&... t) {
return read(h)&&read(t...);
}
#undef OUT_SIZE
#undef BUF_SIZE
};
using namespace std;
using namespace FastIO;
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
typedef long long ll;
typedef pair<int,int> pii;
typedef unsigned long long ull;
const int INF = 0x3f3f3f3f;
const int N = 3e5+10;
const int maxsz = 2e7+3;//@maxsz素数表@
//1e7+19,2e7+3,3e7+23,4e5+9 @maxsz最好为素数@
//1e6+3,2e6+3,3e6+7,4e6+9,1e5+3,2e5+3,3e5+7
//@要保证取值的操作值集合小于maxsz,@
//@count操作不增加新节点@
template<typename key,typename val>
class hash_map {
public:
struct node {
key u;
val v;
int next;
};
vector<node> e;
int head[maxsz],nume,numk,id[maxsz];
bool count(key u) {
int hs=(u%maxsz + maxsz) % maxsz;
for(int i=head[hs]; i; i=e[i].next)
if(e[i].u==u) return 1;
return 0;
}
val& operator[](key u) {
int hs=(u%maxsz + maxsz) % maxsz;
for(int i=head[hs]; i; i=e[i].next)
if(e[i].u==u) return e[i].v;
if(!head[hs])id[++numk]=hs;
if(++nume>=e.size())e.resize(nume<<1);
return e[nume]=(node) {u,0,head[hs]},head[hs]=nume,e[nume].v;
}
};
int a[N],b[N];
hash_map<int,int>cnt,cntxor;
signed main() {
#ifdef xiaofan
freopen("1.in","r",stdin);
freopen("1.out","w",stdout);
#endif
int n,m;
read(n,m);
for(int i=1; i<=n; i++) {
read(a[i]);
cnt[a[i]]++;
for(int j=0; j<30; j++) {
int x = a[i]^(1<<j);
cntxor[x]++;
}
}
ll ans = 0;
for(int i=1; i<=m; i++) {
int w;
read(w);
for(int j=0; j<30; j++) {
int x = w^(1<<j);
if(cntxor.count(x)) ans+=cntxor[x];
if(cnt.count(w)) ans-=cnt[w];
}
}
printf("%lld\n",ans/2);
return 0;
}