Test38(MX)—T5 XOR
Pre-Words(Feihua)
这tm逆天的20pts被我漏掉了,白送的20分不要,估计也只有我这一个2b了(>__<)
CWOI的这道题数据很水,我TM40pts做法就AC了,数据还是太随机了一点,嘿嘿~~ o( ̄︶ ̄)o
Statement
初始时给定⼀个⻓度为的正整数序列,序列下标为,现有⼀个⼆维数组 ,执⾏以下函数
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];
}现给出 次询问,每次询问给定⾮负整数 ,保证有 ,试给出的权值⼤⼩。
提示:组合数
Samples
Input
4 4
9 5 9 2
1 2
0 0
0 2
3 0
Output
11
9
9
7
Tips
对于的数据,
对于的数据,数据保证每组询问的 为所有可能的询问中等概率⽣成的。
对于所有测评数据,
Solutions
1. 20pts
此时,直接暴力即可,时间复杂度为
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数组每个位置对的贡献次数
于是我们开始思考如何得到?
因为对于40pts的数据,,并不可以暴力预处理b数组,于是我们便寄托于输入的a数组来直接解决问题,由于异或运算可以拆分
PS:异或运算的拆分
所以我们直接计算a数组每个数的贡献次数,就当做是杨辉三角的底层,去传导给杨辉三角的顶层
杨辉三角的计算使用组合数学,我们设处于x行y列,那么对于位置,传导次数为
再不难发现,对于神奇的异或运算,有,推广一下就是对于一个数,异或自己偶数次一定得到
还不难发现,,推广一下就是对于一个数,异或自己奇数次一定得到
由此,我们发现,对于b数组里任意一次贡献(传导),要么贡献一次,要么没有贡献
再根据题目中的提示(PS:这是Lucas定理的变形式,感兴趣的读者可以自行学习)
我们发现,当在代码里判断组合数的奇偶性时,不用进行预处理计算,直接判断即可
再不难发现,“a与b等于b?“,这不是我们熟悉的数学唐老师在高一集合这一章节教过我们的吗?
唐老师说:“这个条件是b是a的子集的充要条件”,即
于是回到40pts做法,发现”数据保证每组询问的 为所有可能的询问中等概率⽣成的。 ”
说人话就是不会出逆天数据卡爆你,非常随机,非常正常
于是做法便出现了,我们直接从a数组传导给,传导次数为,此时
因为只有当时才会产生贡献,即的子集才可以传导贡献,于是我们直接一波枚举的子集(C老讲过如何使用位运算枚举数的二进制子集的,快去翻你的状压DP讲稿吧),将对应的数组的值异或起来,就是答案
时间复杂度为,在随机的普通数据情况下,并不会超时
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
凭什么呢?
那你枚举子集的时候复杂度就达到了,那q此时是,算下来时间复杂度是,就算你的电脑可以一秒运算次,那也需要秒,大概是一分多钟,你这个代码效率好像也挺高!
我们思考如何优化,如果,我们进行b数组的部分预处理,然后直接从预处理的部分进行转移呢?
考虑到我们是从这里枚举子集计算贡献,我们既然没法控制,那就控制t,通过合适的t来减少的子集枚举个数,从而缩减时间复杂度,那么如何做到这一点呢?
我们折中考虑,对于,我们需要减少枚举子集的时间复杂度,如果将设为,也就是,此时减下来,此时枚举子集的时间复杂度瞬间降到,确实快多了,于是我们带入实际中进行优化
我们取来取出x的后位,再与相减,此时的数量已经砍半,我们就可以直接通过数组的前行进行转移,时间复杂度为
轻松通过!!
PS:其实把换成也行
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;
}
完结撒花!!感谢观看!!记得点赞!!

