[Atcoder ARC124] XOR Matching 2-小思维 | 暴力

在这里插入图片描述
样例输入 Copy
【样例1】

3
1 2 3
6 4 7

【样例2】

2
0 1
0 2

【样例3】

24
14911005 70152939 282809711 965900047 168465665 337027481 520073861 20800623 934711525 944543101 522277111 580736275 468493313 912814743 99651737 439502451 365446123 198473587 285587229 253330309 591640417 761745547 247947767 750367481
805343020 412569406 424258892 329301584 123050452 1042573510 1073384116 495212986 158432830 145726540 623594202 836660574 380872916 722447664 230460104 718360386 620079272 109804454 60321058 38178640 475708360 207775930 393038502 310271010

样例输出 Copy
【样例1】

1
5

【样例2】

0

【样例3】

8
107543995
129376201
139205201
160626723
312334911
323172429
481902037
493346727

题意:
给出n,两个数列a[1] -> a[n],b[1] -> b[n]
问有多少个x,可以使得在我们任意一种方式排列b[]之后,有a[i] ^ b[i] == x (1 <= i <= n)

思路:
首先我们可以确定所有的答案一定在a[1] ^ b[i] (1 <= i <= n)之内,所以我们只需要将这些个x的解空间单独放到数组c[]里,然后遍历x的解空间c[],将c[i] ^ a[i]的结果记录在d[]里面,然后判断b[],d[]是否完全相同即可,如果完全相同,就可以记录答案,注意最终答案要进行去重

int n,a[maxn],b[maxn],c[maxn],d[maxn];
bool check() {
    for(int i=1; i<=n; i++) {
        if(b[i] != d[i]) return false;
    }
    return true;
}
vector<int> ans;
int main() {
    n = read;
    for(int i=1; i<=n; i++) a[i] = read;
    for(int i=1; i<=n; i++) b[i] = read;
    sort(b+1,b+1+n);
    for(int i=1; i<=n; i++) {
        c[i] = a[1] ^ b[i];
        for(int j=1; j<=n; j++) {
            d[j] = a[j] ^ c[i];
        }
        sort(d+1,d+1+n);
        if(check()) ans.push_back(c[i]);
    }
    sort(ans.begin(),ans.end());
    ans.erase(unique(ans.begin(),ans.end()),ans.end());
    cout << ans.size() <<endl;
    if(ans.size() == 0) return 0;
    int siz = ans.size();
    for(int i=0;i<siz;i++){
        printf("%d\n",ans[i]);
    }
    return 0;
}
posted @ 2021-08-15 11:39  PushyTao  阅读(25)  评论(0编辑  收藏  举报