友好数对

4 篇文章 0 订阅
2 篇文章 0 订阅

题目

这里写图片描述

输入

这里写图片描述

输出

这里写图片描述

Sample Input

3 5
1 8 13
7 5 4 8 3

Sample Output

7

数据范围

这里写图片描述


解法

首先如果要x xor y=k中,k有仅有两位是1的话。
那么就:(x xor(2^i)) xor (y xor(2^j))=0。
则 x xor (2^i)=y xor (2^j)。
于是可以想到折半搜索。
先枚举i,然后把x的异或值扔进hash里面,再枚举j,从hash里找数量。
当然要减去i=j的情况。(方法很多,例如枚举时强制i < j)。


代码

#include<cstdio>
#include<algorithm>
#include<cstdlib>
#include<cstring>
#include<cmath>
#define fo(i,a,b) for(int i=a;i<=b;i++)
#define ll long long

using namespace std;

const int maxn=1e5+5,M=30,mo=23332333;
int n,m,a[maxn],b[maxn],er[M],pos;
ll ans;
struct cy{
    int num,val;
}ha[mo+2];

int read()
{
    int x=0;
    char c;
    for(c=getchar();c<'0'||c>'9';c=getchar());
    for(c;c>='0'&&c<='9';c=getchar()) x=x*10+c-48;
    return x;
}
void hash(int x)
{
    pos=x%mo;
    while (ha[pos].val!=0&&ha[pos].val!=x) pos=pos%mo+1;
    ha[pos].val=x; ++ha[pos].num;
}
int getha(int x)
{
    pos=x%mo;
    while (ha[pos].val!=0&&ha[pos].val!=x) pos=pos%mo+1;
    return ha[pos].num;
 } 
int main()
{
    n=read(); m=read();
    er[0]=1; 
    fo(i,1,M) er[i]=er[i-1]*2;
    fo(i,1,n) {
        a[i]=read();
        int now=a[i]^er[0];
        hash(now);  
    }
    fo(i,1,m) b[i]=read();
    fo(i,1,29){
        fo(j,1,m){
            int now=b[j]^er[i];
            ans+=getha(now);
        }
        fo(j,1,n){
            int now=a[j]^er[i];
            hash(now);
        }
    }
    printf("%lld\n",ans);
}

这里写图片描述

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值