Codeforces 959 F. Mahmoud and Ehab and yet another xor task
\(>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的更多相关文章
- 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 ...
- Codeforces 959F Mahmoud and Ehab and yet another xor task 线性基 (看题解)
Mahmoud and Ehab and yet another xor task 存在的元素的方案数都是一样的, 啊, 我好菜啊. 离线之后用线性基取check存不存在,然后计算答案. #inclu ...
- 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的子序列 ...
- 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 ...
- [CF959F]Mahmoud and Ehab and yet another xor task题解
搞n个线性基,然后每次在上一次的基础上插入读入的数,前缀和线性基,或者说珂持久化线性基. 然后一个num数组记录当时线性基里有多少数 然后每次前缀操作一下就珂以了 代码 #include <cs ...
- Codeforces 959D. Mahmoud and Ehab and another array construction task(构造, 简单数论)
Codeforces 959D. Mahmoud and Ehab and another array construction task 题意 构造一个任意两个数都互质的序列,使其字典序大等于a序列 ...
- D. Mahmoud and Ehab and another array construction task 因子分界模板+贪心+数学
D. Mahmoud and Ehab and another array construction task 因子分解模板 题意 给出一个原序列a 找出一个字典序大于a的序列b,使得任意 \(i!= ...
- codeforces-473D Mahmoud and Ehab and another array construction task (素数筛法+贪心)
题目传送门 题目大意:先提供一个数组,让你造一个数组,这个数组的要求是 1 各元素之间都互质 2 字典序大于等于原数组 3 每一个元素都大于2 思路: 1.两个数互质的意思就是没有公因子.所以每 ...
- 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 ...
随机推荐
- 【Codeforces752D】Santa Claus and a Palindrome [STL]
Santa Claus and a Palindrome Time Limit: 20 Sec Memory Limit: 512 MB Description 有k个串,串长都是n,每个串有一个a ...
- 通用标签、属性(body属性、路径、格式控制)
通用标签.属性 一.body属性 1.bgcolor属性:网页背景颜色 2.text属性:规定文档中所有文本的颜色. 3.background属性:规定文档的背景图像. 二.路径 1.绝对路径: 从根 ...
- python学习笔记(十三)之lambda表达式
lambda表达式: 用法 lambda x : 2 * x + 1 其中:前面是参数,后面是返回值. >>> def ds(x): ... return 2 * x + 1 ... ...
- 垂直水平居中--css3
在移动前端制作中,很多新的css3特性能够帮助我们更好的制作.例如这个垂直水平居中问题,就有一个简单的代码可以解决: 利用CSS3的transform:translate .center{ width ...
- JSON.parse()——json字符串转JS
JSON 通常用于与服务端交换数据. 在接收服务器数据时一般是字符串. 我们可以使用 JSON.parse() 方法将数据转换为 JavaScript 对象. 语法 JSON.parse(text[, ...
- 关于分布式Session 的几种实现方式
分布式Session的几种实现方式 1.基于数据库的Session共享 2.基于NFS共享文件系统 3.基于memcached 的session,如何保证 memcached 本身的高可用性? 4. ...
- 2017 NEERC
2017 NEERC Problem A. Archery Tournament 题目描述:在二维平面上,会陆续出现一些圆,以及一些询问,询问点是否在圆内,如果是,则输出那个圆,并把那个圆删掉,否则输 ...
- java之正则表达式、日期操作
正则表达式和日期操作 正则表达式简介 正则表达式就是使用一系列预定义的特殊字符来描述一个字符串的格式规则,然后使用该格式规则匹配某个字符串是否符合格式要求. 作用:比如注册邮箱,邮箱有用户名和密码,一 ...
- jmeter----计数器
在测试过程中,往往需要一些有一定规则的数字,这个时候,可以使用配置元件中的计数器去实现. 一.界面显示 二.配置说明 1.名称:标识 2.注释:备注 3.启动:是指计数器开始的值 4.递增:每次增加的 ...
- 【重点】Jmeter----- 将 JDBC Request 查询结果作为下一个接口参数方法(二)
一.说明 jmeter与数据库mysql已连接成功 二.需求 1.前置条件: 1.已user数据库的前8位手机号码作为行动计划的名称 2.行动计划的日期是2018-10-17 2.操作步骤: 1)获取 ...