Test38(MX)--T5 XOR题解
Claire0630 · · 题解
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的数据,
PS:异或运算的拆分
所以我们直接计算a数组每个数的贡献次数,就当做是杨辉三角的底层,去传导给杨辉三角的顶层
杨辉三角的计算使用组合数学,我们设
再不难发现,对于神奇的异或运算,有
还不难发现,
由此,我们发现,对于b数组里任意一次贡献(传导),要么贡献一次,要么没有贡献
再根据题目中的提示
我们发现,当在代码里判断组合数的奇偶性时,不用进行预处理计算,直接判断
再不难发现,"a与b等于b?",这不是我们熟悉的数学唐老师在高一集合这一章节教过我们的吗?
唐老师说:"这个条件是b是a的子集的充要条件",即
于是回到40pts做法,发现"数据保证每组询问的
说人话就是不会出逆天数据卡爆你,非常随机,非常正常
于是做法便出现了,我们直接从a数组传导给
因为只有当
时间复杂度为
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
凭什么呢?
那你枚举子集的时候复杂度就达到了也挺高!
我们思考如何优化,如果,我们进行b数组的部分预处理,然后直接从预处理的部分进行转移呢?
考虑到我们是从
我们折中考虑,对于
我们取
轻松通过!!
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;
}
完结撒花!!感谢观看!!记得点赞!!