Test38(MX)--T5 XOR题解

· · 题解

Test38(MX)--T5 XOR

Pre-Words(Feihua)

这tm逆天的20pts被我漏掉了,白送的20分不要,估计也只有我这一个2b了(>__<)

CWOI的这道题数据很水,我TM40pts做法就AC了,数据还是太随机了一点,嘿嘿~~ o( ̄︶ ̄)o

Statement

初始时给定⼀个⻓度为n的正整数序列a_i,序列下标为[0,n-1],现有⼀个⼆维数组 b ,执⾏以下函数

void init(){
    for(int i=0;i<n;i++) b[0][i] = a[i];
    for(int i=1;i<n;i++)
        for(int j=0;j<n-i;j++) b[i][j] = b[i - 1][j] ^ b[i - 1][j + 1];
 }

现给出 q 次询问,每次询问给定⾮负整数x,y ,保证有 0 \leq x+y \leq n-1 ,试给出b[x][y]的权值⼤⼩。

提示:组合数{\binom{b}{a}\mod 2= [a \land b == b]}

Samples

Input

4 4
9 5 9 2
1 2
0 0
0 2
3 0

Output

11
9 
9
7

Tips

对于20\%的数据,1\leq n \leq 5000

对于40\%的数据,数据保证每组询问的 x,y 为所有可能的询问中等概率⽣成的。

对于所有测评数据,1 \leq n,q \leq 10^5,0\leq a_i \leq 10^9

Solutions

1. 20pts

此时n \leq 5000,直接暴力即可,时间复杂度为O(n^2 + q)

Code:

#include <bits/stdc++.h>
using namespace std;
typedef long long LL;
typedef __int128 SLL;//super_long_long
typedef unsigned long long ULL;
typedef pair<int,int> PII;
typedef pair<int,pair<int,int>> PIP;
typedef pair<LL,LL> PLL;
template <typename T> inline void read(T &x){
    x = 0;
    LL f = 1;
    char c = getchar();
    for(; !isdigit(c); c=getchar()) if(c == '-') f = -1;
    for(; isdigit(c); c=getchar()) x = (x << 1) + (x << 3) + (c ^ 48);
    x *= f;
}
template <typename T> inline void print(T x){
    if(x < 0) x = -x,putchar('-');
    if(x > 9) print(x / 10);
    putchar(x % 10 + 48);
}
const int N = 5e3 + 11,M = 25;
int n,q;
int a[N],b[N][N];
int main(){
    read(n);read(q);
    for(int i=0;i<n;i++) read(a[i]);
    for(int i=0;i<n;i++) b[0][i]=a[i];
    for(int i=1;i<n;i++)
        for(int j=0;j<n-i;j++) b[i][j]=b[i-1][j]^b[i-1][j+1];
    while(q --){
        int x,y;
        read(x);read(y);
        print(b[x][y]);
        putchar('\n');
    }
    return 0;
}

2. 40pts

因为查询是关于b数组的,我们直接深入研究一波

for(int i=0;i<n;i++) b[0][i] = a[i];
for(int i=1;i<n;i++)
    for(int j=0;j<n-i;j++) b[i][j] = b[i - 1][j] ^ b[i - 1][j + 1];

可以看到这个b数组的是做异或运算,需要从上一层的上一个和右上一个位置异或得到

b数组传导方式如下:

      图一:b数组的传导

                                                                                                        图二:杨辉三角

仔细观察,可以发现,这个传导方式酷似杨辉三角,于是我们继续深入思考

杨辉三角和b数组的关系何在?

我们不难发现,杨辉三角的数字正代表着b数组每个位置的出度,我们换句话说,就是b数组每个位置对b[i][j]的贡献次数

于是我们开始思考如何得到b[x][y]?

因为对于40pts的数据,n\leq 10^5,并不可以暴力预处理b数组,于是我们便寄托于输入的a数组来直接解决问题,由于异或运算可以拆分

PS:异或运算的拆分

\because a \oplus b = c,c \oplus d = e\\ \therefore a\oplus b\oplus d = e

所以我们直接计算a数组每个数的贡献次数,就当做是杨辉三角的底层,去传导给杨辉三角的顶层

杨辉三角的计算使用组合数学,我们设b[x][y]处于x行y列,那么对于位置(a,b),传导次数为\binom{x - a}{b - y}

再不难发现,对于神奇的异或运算,有x \oplus x = 0,推广一下就是对于一个数x,x异或自己偶数次一定得到0

还不难发现,0 \oplus x = x,推广一下就是对于一个数x,x异或自己奇数次一定得到x

由此,我们发现,对于b数组里任意一次贡献(传导),要么贡献一次,要么没有贡献

再根据题目中的提示{\binom{b}{a}\mod 2= [a \land b == b]}(PS:这是Lucas定理的变形式,感兴趣的读者可以自行学习)

我们发现,当在代码里判断组合数的奇偶性时,不用进行预处理计算,直接判断a \land b == b即可

再不难发现,"a与b等于b?",这不是我们熟悉的数学唐老师在高一集合这一章节教过我们的吗?

唐老师说:"这个条件是b是a的子集的充要条件",即b \subseteq a

于是回到40pts做法,发现"数据保证每组询问的 x,y 为所有可能的询问中等概率⽣成的。 "

说人话就是不会出逆天数据卡爆你,非常随机,非常正常

于是做法便出现了,我们直接从a数组传导给b[x][y],传导次数为\binom{x - a}{b - y},此时a = 0

因为只有当x \land (b-y) == (b-y)时才会产生贡献,即x的子集才可以传导贡献,于是我们直接一波枚举x的子集(C老讲过如何使用位运算枚举数的二进制子集的,快去翻你的状压DP讲稿吧),将对应的a数组的值异或起来,就是答案

时间复杂度为O(q \times 2^{popcount(x)}),在随机的普通数据情况下,并不会超时

Code:

#include <bits/stdc++.h>
using namespace std;
typedef long long LL;
typedef __int128 SLL;//super_long_long
typedef unsigned long long ULL;
typedef pair<int,int> PII;
typedef pair<int,pair<int,int>> PIP;
typedef pair<LL,LL> PLL;
template <typename T> inline void read(T &x){
    x = 0;
    LL f = 1;
    char c = getchar();
    for(; !isdigit(c); c=getchar()) if(c == '-') f = -1;
    for(; isdigit(c); c=getchar()) x = (x << 1) + (x << 3) + (c ^ 48);
    x *= f;
}
template <typename T> inline void print(T x){
    if(x < 0) x = -x,putchar('-');
    if(x > 9) print(x / 10);
    putchar(x % 10 + 48);
}
const int N = 1e5 + 11,M = 25;
int n,q;
int a[N];
int main(){
    read(n);read(q);
    for(int i=0;i<n;i++) read(a[i]);
    while(q --){
        int x,y;
        read(x);read(y);
        int ans = a[y];
        for(int i=x;i;i=(i-1)&x){
            ans ^= a[i + y];
        }
        print(ans);
        putchar('\n');
    }
    return 0;
}
//数据水过头了哥们,这TM是40pts写法

3. 100pts

根据上面一通瞎jb乱操作完美操作,我们成功的通过了大部分随机数据

BUTTTTTTTT~~!!

这组数据甩给你你不就炸了吗老弟!

其他的不重要,重要的是x=65536

凭什么呢?

(65536)_{10} = (\underbrace{111...1}_{16个1})_{2}

那你枚举子集的时候复杂度就达到了O(q \times 2^{16}),那q此时是10^5,算下来时间复杂度是O(6553600000),就算你的电脑可以一秒运算10^8次,那也需要65.536秒,大概是一分多钟,你这个代码效率好像也挺高!

我们思考如何优化,如果,我们进行b数组的部分预处理,然后直接从预处理的部分进行转移呢?

考虑到我们是从(x - t)这里枚举子集计算贡献,我们既然没法控制x,那就控制t,通过合适的t来减少x-t的子集枚举个数,从而缩减时间复杂度,那么如何做到这一点呢?

我们折中考虑,对于x=65536,我们需要减少枚举子集的时间复杂度,如果将t设为512,也就是2^8,此时减下来x-t=(65024)_{10} = (\underbrace{111...1}_{8个1}\underbrace{000...0}_{8个0}),此时枚举子集的时间复杂度瞬间降到O(2^8),确实快多了,于是我们带入实际中进行优化

我们取t = x\mod 512来取出x的后8位,再与x相减,此时1的数量已经砍半,我们就可以直接通过b数组的前512行进行转移,时间复杂度为O(512 \times n + 2^{popcount(x-t)}),popcount(x-t)\in [0,8]

轻松通过!!

PS:其实把512换成256也行

Code:

#include <bits/stdc++.h>
using namespace std;
typedef long long LL;
typedef __int128 SLL;//super_long_long
typedef unsigned long long ULL;
typedef pair<int,int> PII;
typedef pair<int,pair<int,int>> PIP;
typedef pair<LL,LL> PLL;
template <typename T> inline void read(T &x){
    x = 0;
    LL f = 1;
    char c = getchar();
    for(; !isdigit(c); c=getchar()) if(c == '-') f = -1;
    for(; isdigit(c); c=getchar()) x = (x << 1) + (x << 3) + (c ^ 48);
    x *= f;
}
template <typename T> inline void print(T x){
    if(x < 0) x = -x,putchar('-');
    if(x > 9) print(x / 10);
    putchar(x % 10 + 48);
}
const int N = 1e5 + 11,M = 520;
int n,q;
int a[N];
int b[M][N];
int main(){
    read(n);read(q);
    for(int i=0;i<n;i++) read(a[i]);
    for(int i=0;i<n;i++) b[0][i] = a[i];
    for(int i=1;i<512;i++){
        for(int j=0;j<n-i;j++) b[i][j] = b[i - 1][j] ^ b[i - 1][j + 1];
    }
    while(q --){
        int x,y;
        read(x);read(y);
        if(x < 512){
            print(b[x][y]);
            putchar('\n');
            continue;
        }
        int t = x % 512,ans = b[t][y];
        for(int i=x-t;i;i=(i-1)&(x-t)){
            ans ^= b[t][i + y];
        }
        print(ans);
        putchar('\n');
    }
    return 0;
}

完结撒花!!感谢观看!!记得点赞!!