959F - Mahmoud and Ehab and yet another xor task xor+dp+离线

题意

给出 n个值和q个询问,询问l,x,表示前l个数字子序列的异或和为x的子序列有多少,其中空序列的异或和为0,一个数字的子序列的异或和是它本身

思路

维护一个集合,记录已经存在在里面的数。

首先我们证明

1.当x在这个集合,y在这个集合的时候\(x\bigoplus y\)也在这个集合里面,因为

\(x=a\bigoplus b\)

\(y=c\bigoplus d\)

所有\(x\bigoplus y==a\bigoplus b \bigoplus c\bigoplus d\)所一定存在在集合中

2.当x在这个集合中y不在这个集合中的时候,\(x\bigoplus y\)不在这个集合中

假设\(x\bigoplus y\)在这个集合中 那么\((x\bigoplus y)\bigoplus x\)也在这个集合中也就是y在这个集合中与题设矛盾

设dp[i][x]表示前i个异或和为x的数量,则有\(dp[i][x]=dp[i-1][x]+dp[i-1][x\bigoplus a[i]]\)

我们用数学归纳法证明 假设对i-1的都成立。

设dp[i-1][x]=j

假设x和a[i]都在set集合中

那么由以上的证明可以知道\(x\bigoplus a[i]\)也在集合中因此,\(dp[i-1][x]=j\)并且\(dp[i-1][x\bigoplus a[i]]=j\)因为dp[i-1][x]的数量已经知道是j了,而a[i]又在集合中,所以每个异或和为x的子序列再异或一个a[i]就变成了\(dp[i-1][x\bigoplus a[i]]\)所以两者数量都为j。

假设a[i]不在集合中

对于x有三种情况

如果x在集合中,由以上证明\(x\bigoplus a[i]\)不在集合中\(dp[i][x]=dp[i-1][x]+dp[i-1][x\bigoplus a[i]]=j+0=0\)

如果x要在这一步被添加到set中,即\(x\bigoplus a[i]\)在集合中,那么有\(dp[i][x]=dp[i-1][x]+dp[i-1][x\bigoplus a[i]]=0+j=j\)

如果不属于上面三种情况,那么\(dp[i][x]=dp[i-1][x]+dp[i-1][x\bigoplus a[i]]=0+0=0\)

得证

ps:for(auto:s)s.pb()在有的版本不会死循环,但以后要注意,避免傻逼错误

#include<bits/stdc++.h>
#define pb push_back
#define F first
#define S second
#define pii pair<int,int>
typedef long long ll;
using namespace std;
const ll maxn=1e5+7;;
const int mod=1e9+7;
int vis[(1<<20)+5];
vector<int>s;
int ans[maxn];
int a[maxn];
vector<pair<int,int> >v[(1<<20)+5];
int main(){
int n,q;
int x,y;
scanf("%d%d",&n,&q);
for(int i=1;i<=n;i++)scanf("%d",&a[i]);
for(int i=1;i<=q;i++){
scanf("%d%d",&x,&y);
v[x].pb({y,i});
}
ll tmp=1;
s.pb(0);
vis[0]=1;
for(int i=1;i<=n;i++){
//cout<<i<<endl;
if(vis[a[i]]){
tmp=tmp*2%mod;
// cout<<111<<endl;
}
else {
/*for(auto p:s){
vis[p^a[i]]=1;
s.pb(p^a[i]);
}*/
int zz=s.size();
for(int j=0;j<zz;j++){
// cout<<s.size()<<" "<<j<<endl;
vis[s[j]^a[i]]=1;
s.pb(s[j]^a[i]);
}
}
// cout<<333<<endl;
/*for(auto&p:v[i]){
ans[p.S]=tmp*vis[p.F];
}*/
for(int j=0;j<v[i].size();j++){
ans[v[i][j].S]=tmp*vis[v[i][j].F];
}
}
for(int i=1;i<=q;i++){
printf("%d\n",ans[i]);
} return 0;
}

959F - Mahmoud and Ehab and yet another xor task xor+dp(递推形)+离线的更多相关文章

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

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

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

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

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

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

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

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

  5. CF959D Mahmoud and Ehab and another array construction task 数学

    Mahmoud has an array a consisting of n integers. He asked Ehab to find another array b of the same l ...

  6. 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 ...

  7. [CF959D]Mahmoud and Ehab and another array construction task题解

    解法 非常暴力的模拟. 一开始吧\(1 -> 2 \times 10^6\)全部扔进一个set里,如果之前取得数都是与原数组相同的,那么lower_bound一下找到set中大于等于它的数,否则 ...

  8. Codeforces 862C - Mahmoud and Ehab and the xor

    862C - Mahmoud and Ehab and the xor 思路:找两对异或后等于(1<<17-1)的数(相当于加起来等于1<<17-1),两个再异或一下就变成0了 ...

  9. Codeforces 959 F. Mahmoud and Ehab and yet another xor task

    \(>Codeforces\space959 F. Mahmoud\ and\ Ehab\ and\ yet\ another\ xor\ task<\) 题目大意 : 给出一个长度为 \ ...

随机推荐

  1. PTA L2-029 | 特立独行的幸福 (打表+递归)

    题目描述 对一个十进制数的各位数字做一次平方和,称作一次迭代.如果一个十进制数能通过若干次迭代得到 \(1\),就称该数为幸福数.\(1\) 是一个幸福数.此外,例如 \(19\) 经过一次迭代得到 ...

  2. [CF1303D] Fill The Bag - 贪心

    Solution 考虑从低位往高位贪心,设当前在处理第 \(i\) 位,更低位剩余的部分一共可以拼出 \(cnt\) 个 \(2^i\) 如果 \(n\) 的这一位是 \(1\) ,那么这一位就需要处 ...

  3. sql server针对字符串型数字排序(针对此字符串的长度不一致)

    对于不规则的字符串数字排序,无法按照数字大的大小排序的原因是,字符串数字在数据库中按照ASCII码排序,从字符的第一个数字对比,首先就会将为首个数字相同的排在一起,在从这些字符串里面对比第二个数字,如 ...

  4. python递归删除目录本身以及目录下文件

    import os def local_rm(dirpath): if os.path.exists(dirpath): files = os.listdir(dirpath) for file in ...

  5. laravel框架用使用session 和cookie

    1.介绍 cookie 位于用户的计算机上,用来维护用户计算机中的信息,直到用户删除.比如我们在网页上登录某个软件时输入用户名及密码时如果保存为cookie,则每次我们访问的时候就不需要登录网站了.我 ...

  6. Java实现的上传并压缩图片功能【可等比例压缩或原尺寸压缩】

    本文实例讲述了Java实现的上传并压缩图片功能.分享给大家供大家参考,具体如下: 先看效果: 原图:1.33M 处理后:27.4kb 关键代码: package codeGenerate.util; ...

  7. php 公众号开发

    思考: 1,优先使用php扩展库的sdk,配合微信文档. 2,公众号开发比较简单,重在业务. 公众号关联与消息分发 <?php namespace app\wap\controller; use ...

  8. spring-framework核心接口ApplicationContext

    核心接口(ApplicationContext) 继承关系 继承接口: org.springframework.beans.factory.ListableBeanFactory:用于访问应用程序组件 ...

  9. springboot项目集成activity

    1.按照上一篇博客,新建好springboot项目后,在项目pom.xml文件中添加activity依赖 <dependency> <groupId>org.activiti& ...

  10. 在SQL中怎么把一列字符串拆分为多列

    --首先,你是按什么规则拆? 我举个例子 你要按字段中的逗号拆开,假设字段名叫text --用charindex和substring这2个函数    select substring(text,1,c ...