\(>Codeforces\space959 F. Mahmoud\ and\ Ehab\ and\ yet\ another\ xor\ task<\)

题目大意 : 给出一个长度为 \(n\) 序列 \(A\),和 \(q\) 次询问,对于每一次询问给出两个数 \(l, x\) ,你需要计算在前缀和 \(A[1, l]\) 中选取若干个数,使得它们 \(xor\) 起来的结果等于 \(x\) 的方案数

$n , q \leq 10^5 \ 0 \leq A_i \leq 2^{20} $

解题思路 :

首先考虑离线,发现将询问按照 \(l\) 排序之后,询问每一个 \(l\) 时都可以构造出关于前缀$ A[1,l] $的线性基

考虑如果要在前缀 \(A[1,l]\) 中选取若干个数表示出 \(x\), 那么线性基中的元素必然能表示出 \(x\)

与此同时,如果线性基能表示出 \(x\)

那么对于每一个在前缀 \(A[1, l]\) 但不在线性基中元素 \(A_i\) 线性基都能表示出 \((x\ xor\ A_i)\)

所以线性基外的元素都可以选或者不选,那么方案数就是 \(2^{l -size}\) 其中 \(size\) 指的是线性基的大小

那么只需要对于询问离线,边向线性基内插入数边回答询问,判断是否能被线性基表示并算出线性基的大小即可

判断数是否能被线性基表示 : 对于数每一个有 \(1\) 的二进制位,\(xor\) 上线性基的对应位,判断是否变成了 \(0\)

求线性基的大小 : 加入元素的时候通过判断是否加入成功来维护

/*program by mangoyang*/
#include<bits/stdc++.h>
#define inf (0x7f7f7f7f)
#define Max(a, b) ((a) > (b) ? (a) : (b))
#define Min(a, b) ((a) < (b) ? (a) : (b))
#define P 1000000007
typedef long long ll;
using namespace std;
template <class T>
inline void read(T &x){
int f = 0, ch = 0; x = 0;
for(; !isdigit(ch); ch = getchar()) if(ch == '-') f = 1;
for(; isdigit(ch); ch = getchar()) x = x * 10 + ch - '0';
if(f) x = -x;
}
int a[200005], ans[200005], Bit[25], n, m, tot;
struct Query{ int l, x, id; } q[200005];
inline bool cmp(Query A, Query B){ return A.l < B.l; }
inline ll Pow(ll a, ll b){
ll ans = 1;
for(; b; b >>= 1, a = a * a % P)
if(b & 1) ans = ans * a % P;
return ans;
}
inline void ins(int x){
for(int i = 19; ~i; i--) if((1 << i) & x){
if(!Bit[i]){ Bit[i] = x; return; }
x ^= Bit[i];
}
}
inline void Answer(Query now){
int x = now.x, lim = now.l, id = now.id, cnt = 0;
for(int i = 19; ~i; i--) if(Bit[i]){
cnt++;
if((1 << i) & x) x ^= Bit[i];
}
if(x) ans[id] = 0; else ans[id] = Pow(2, lim - cnt);
}
int main(){
read(n), read(m);
for(int i = 1; i <= n; i++) read(a[i]);
for(int i = 1; i <= m; i++){
int l, x;
read(l), read(x), q[i] = (Query){l, x, i};
}
int p = 1;
sort(q + 1, q + m + 1, cmp);
for(; !q[p].l && p <= m; p++)
if(!q[p].x) ans[q[p].id] = 1; else ans[q[p].id] = 0;
for(int i = 1; i <= n; i++){
ins(a[i]);
while(q[p].l == i && p <= m) Answer(q[p++]);
}
for(int i = 1; i <= m; i++) printf("%d\n", ans[i]);
return 0;
}

Codeforces 959 F. Mahmoud and Ehab and yet another xor task的更多相关文章

  1. Codeforces 959 D Mahmoud and Ehab and another array construction task

    Discription Mahmoud has an array a consisting of n integers. He asked Ehab to find another arrayb of ...

  2. Codeforces 959F Mahmoud and Ehab and yet another xor task 线性基 (看题解)

    Mahmoud and Ehab and yet another xor task 存在的元素的方案数都是一样的, 啊, 我好菜啊. 离线之后用线性基取check存不存在,然后计算答案. #inclu ...

  3. 959F - Mahmoud and Ehab and yet another xor task xor+dp(递推形)+离线

    959F - Mahmoud and Ehab and yet another xor task xor+dp+离线 题意 给出 n个值和q个询问,询问l,x,表示前l个数字子序列的异或和为x的子序列 ...

  4. Codeforces 959 E Mahmoud and Ehab and the xor-MST

    Discription Ehab is interested in the bitwise-xor operation and the special graphs. Mahmoud gave him ...

  5. [CF959F]Mahmoud and Ehab and yet another xor task题解

    搞n个线性基,然后每次在上一次的基础上插入读入的数,前缀和线性基,或者说珂持久化线性基. 然后一个num数组记录当时线性基里有多少数 然后每次前缀操作一下就珂以了 代码 #include <cs ...

  6. Codeforces 959D. Mahmoud and Ehab and another array construction task(构造, 简单数论)

    Codeforces 959D. Mahmoud and Ehab and another array construction task 题意 构造一个任意两个数都互质的序列,使其字典序大等于a序列 ...

  7. D. Mahmoud and Ehab and another array construction task 因子分界模板+贪心+数学

    D. Mahmoud and Ehab and another array construction task 因子分解模板 题意 给出一个原序列a 找出一个字典序大于a的序列b,使得任意 \(i!= ...

  8. codeforces-473D Mahmoud and Ehab and another array construction task (素数筛法+贪心)

    题目传送门 题目大意:先提供一个数组,让你造一个数组,这个数组的要求是 1 各元素之间都互质  2  字典序大于等于原数组  3 每一个元素都大于2 思路: 1.两个数互质的意思就是没有公因子.所以每 ...

  9. CF 959 E. Mahmoud and Ehab and the xor-MST

    E. Mahmoud and Ehab and the xor-MST https://codeforces.com/contest/959/problem/E 分析: 每个点x应该和x ^ lowb ...

随机推荐

  1. [php]http响应头解析

    (Status-Line) HTTP/ OK Cache-Control no-cache Content-Length Content-Type image/gif Date Sat, Dec :: ...

  2. 【BZOJ】1875: [SDOI2009]HH去散步 矩阵快速幂

    [题意]给定n个点m边的无向图,求A到B恰好经过t条边的路径数,路径须满足每条边都和前一条边不同.n<=20,m<=60,t<=2^30. [算法]矩阵快速幂 [题解]将图的邻接矩阵 ...

  3. 下载Google My Tracks

    http://code.google.com/p/mytracks/source/browse/?name=2.0.2#hg%2FMyTracks%253Fstate%253Dclosed 需要类似的 ...

  4. 使sqoop能够启用压缩的一些配置

    在使用sqoop 将数据库表中数据导入至hdfs时 配置启用压缩 hadoop 的命令    检查本地库支持哪些  bin/hadoop checknative 需要配置native    要编译版本 ...

  5. bzoj 2303 并查集

    首先如果没有限制的话,我们可以直接求出答案,假设对于n*m的矩阵,我们最上方一行和左方的一列随意确定,那么首先这写确定的状态肯定是不会不合法的,因为我们可以调整剩下的01状态来使得这一行一列的状态合法 ...

  6. windows下面安装Python和pip教程

    第一步,先来安装Python.windows下面的Python安装一般是通过软件安装包安装而不是命令行,所以首先要在Python的官方主页上面下载最新的Python安装包.下载地址是:https:// ...

  7. Linux中fork()函数的底层实现【转】

    转自:http://blog.csdn.net/duoru_xiong/article/details/76358812 1. fork(),vfork(),clone()的区别 这三个系统调用的底层 ...

  8. Android快速入门(转自 农民伯伯: http://www.cnblogs.com/over140/)

    前言 这是前段时间用于公司Android入门培训的资料,学习Android三周时间收集整理的,时间仓促,希望能对像我这样还没入门就直接上项目的人一点帮助  :) 声明 欢迎转载,但请保留文章原始出处: ...

  9. Deep Learning基础--理解LSTM/RNN中的Attention机制

    导读 目前采用编码器-解码器 (Encode-Decode) 结构的模型非常热门,是因为它在许多领域较其他的传统模型方法都取得了更好的结果.这种结构的模型通常将输入序列编码成一个固定长度的向量表示,对 ...

  10. Tutorial 4: Authentication & Permissions

    转载自:http://www.django-rest-framework.org/tutorial/4-authentication-and-permissions/ Tutorial 4: Auth ...