#3391. big

题目描述

你需要在 $[ 0,2^n )$ 中选一个整数 $x$,接着把 $x$ 依次异或 $m$ 个整数 $a_1 \sim a_m$。

在你选出 $x$ 后,你的对手需要选择恰好一个时刻(刚选完数时、异或一些数后或是最后),将 $x$ 变为 $(\lfloor \frac{2x}{2^n} \rfloor + 2x )\mod 2^n$ 。

你想使 $x$ 最后尽量大,而你的对手会使 $x$ 最后尽量小。

你需要求出 $x$ 最后的最大值,以及得到最大值的初值数量。

数据范围

$n\le 30 , m\le 100000, 0 \leq a_i < 2^n$

题解

很妙的一道题

考虑那个奇妙操作,手画一下发现就是把最高位移到最后

然后假设 $x$ 在前 $i$ 个数 $(0 \le i \le m)$ 后做了得到了 $x ⊕ s_i$ ,然后经过这个操作得到了 $y$ ,考虑到异或是按位异或,所以如果我们先把 $x$ 和 $s_i$ 先通过这个操作得到 $x'$ 和 $s_i'$ ,那 $x' ⊕ s_i'=y$

于是我们可以先把 $s'_i ⊕ (s_m ⊕ s_i)$ 建立棵 $trie$ ,在 $trie$ 上寻找最优解即可

效率 $O(nm)$

代码

#include <bits/stdc++.h>
using namespace std;
const int N=1e5+5;
int n,m,s,t=1,a[N],ch[N*30][2];
void ins(int x){
    for (int p=1,v,j=n-1;~j;j--){
        v=(x>>j)&1;
        if (!ch[p][v]) ch[p][v]=++t;
        p=ch[p][v];
    }
}
void dfs(int x,int y,int d){
    if (!ch[x][0] && !ch[x][1]){
        if (y>s) s=y,t=0;t+=(s==y);return;
    }
    if (!ch[x][0] || !ch[x][1]) dfs(ch[x][!ch[x][0]],y|(1<<d),d-1);
    else dfs(ch[x][0],y,d-1),dfs(ch[x][1],y,d-1);
}
int main(){
    scanf("%d%d",&n,&m);
    for (int i=1;i<=m;i++)
        scanf("%d",&a[i]),s^=a[i];ins(s);
    for (int i=1;i<=m;i++){
        s^=a[i];
        a[i]=((a[i]<<1)&((1<<n)-1))|(a[i]>>(n-1));
        s^=a[i];ins(s);
    }
    s=t=0;dfs(1,0,n-1);printf("%d\n%d\n",s,t);
    return 0;
}

 

posted @ 2019-08-30 15:26  xjqxjq  阅读(160)  评论(0编辑  收藏  举报