Codeforces Round #539 (Div. 2) - C. Sasha and a Bit of Relax(思维题)
Problem Codeforces Round #539 (Div. 2) - C. Sasha and a Bit of Relax
Time Limit: 2000 mSec
Problem Description
Input
The first line contains one integer n (2≤n≤3⋅10^5) — the size of the array.
The second line contains n integers a1,a2,…,an (0≤ai<2^20) — array itself.
Output
Print one integer — the number of funny pairs. You should consider only pairs where r−l+1is even number.
Sample Input
5
1 2 3 4 5
Sample Output
1
题解:这个题只要知道异或满足
if A ^ B == C then A == B ^ C 这个性质
就会很简单,首先处理出异或前缀和是很自然的,之后对于满足条件的pair,必定有f(r) == f(l - 1),f即异或前缀和,这是必要条件,同时也是充分的,充分性同样利用这个性质
若有a[l] ^ a[l+1] ^ ... ^ a[k] = a[k+1] ^ a[k + 2] ^ ... ^ a[r], 那么就可以利用上述性质使得k == mid,因为如果k > mid,那么,等式两边同时异或a[k]即可将a[k]从等式左边变到右边,继续进行这种操作知道k == mid,对于k < mid,同理,因此现在就是找满足异或前缀和相等的pair,并且要使得r - l + 1是偶数,也就是r 和 l - 1同奇偶,这个问题很好解决,统计每种异或前缀和的个数(对每个值按下标奇偶性分类)即可计算出最终结果,由a数组数据的范围可知异或前缀和 < 2^21,复杂度是完全可以接受的,别忘了开 long long。
#include <bits/stdc++.h> using namespace std; #define REP(i, n) for (int i = 1; i <= (n); i++)
#define sqr(x) ((x) * (x)) const int maxn = + ;
const int maxm = + ;
const int maxs = + ; typedef long long LL;
typedef pair<int, int> pii;
typedef pair<double, double> pdd; const LL unit = 1LL;
const int INF = 0x3f3f3f3f;
const double eps = 1e-;
const double inf = 1e15;
const double pi = acos(-1.0);
const int SIZE = + ;
const LL MOD = ; LL cnt[maxn][];
int n; int main()
{
ios::sync_with_stdio(false);
cin.tie();
//freopen("input.txt", "r", stdin);
//freopen("output.txt", "w", stdout);
cin >> n;
int pre = , x;
for (int i = ; i <= n; i++)
{
cin >> x;
pre ^= x;
//cout << pre << endl;
cnt[pre][i % ]++;
}
LL ans = ;
cnt[][]++;
for (int i = ; i < maxn; i++)
{
ans += cnt[i][] * (cnt[i][] - ) / ;
ans += cnt[i][] * (cnt[i][] - ) / ;
}
cout << ans << endl;
return ;
}
Codeforces Round #539 (Div. 2) - C. Sasha and a Bit of Relax(思维题)的更多相关文章
- Codeforces Round #539 (Div. 2) - D. Sasha and One More Name(思维)
Problem Codeforces Round #539 (Div. 2) - D. Sasha and One More Name Time Limit: 1000 mSec Problem ...
- Codeforces Round #539 (Div. 2) C. Sasha and a Bit of Relax(前缀异或和)
转载自:https://blog.csdn.net/Charles_Zaqdt/article/details/87522917 题目链接:https://codeforces.com/contest ...
- Codeforces Round #539 (Div. 2) C Sasha and a Bit of Relax
题中意思显而易见,即求满足al⊕al+1⊕…⊕amid=amid+1⊕amid+2⊕…⊕ar且l到r的区间长为偶数的这样的数对(l,r)的个数. 若al⊕al+1⊕…⊕amid=amid+1⊕amid ...
- Codeforces Round #539 (Div. 1) C. Sasha and a Patient Friend 动态开点线段树
题解看这里 liouzhou_101的博客园 更简洁的代码看这里: #include <bits/stdc++.h> using namespace std; typedef long l ...
- Codeforces Round #539 (Div. 1) E - Sasha and a Very Easy Test 线段树
如果mod是质数就好做了,但是做除法的时候对于合数mod可能没有逆元.所以就只有存一下mod的每个质因数(最多9个)的幂,和剩下一坨与mod互质的一部分.然后就能做了.有点恶心. CODE #incl ...
- Codeforces Round #539 (Div. 1) 1109F. Sasha and Algorithm of Silence's Sounds LCT+线段树 (two pointers)
题解请看 Felix-Lee的CSDN博客 写的很好,不过最后不用判断最小值是不是1,因为[i,i]只有一个点,一定满足条件,最小值一定是1. CODE 写完就A,刺激. #include <b ...
- Codeforces Round #554 (Div. 2) B. Neko Performs Cat Furrier Transform(思维题+log2求解二进制位数的小技巧)
传送门 题意: 给出一个数x,有两个操作: ①:x ^= 2k-1; ②:x++; 每次操作都是从①开始,紧接着是② ①②操作循环进行,问经过多少步操作后,x可以变为2p-1的格式? 最多操作40次, ...
- Codeforces Round #539 (Div. 2)
Codeforces Round #539 (Div. 2) A - Sasha and His Trip #include<bits/stdc++.h> #include<iost ...
- Codeforces Round #539 (Div. 2) 题解
Codeforces Round #539 (Div. 2) 题目链接:https://codeforces.com/contest/1113 A. Sasha and His Trip 题意: n个 ...
随机推荐
- 如何在Microsoft Word里面插入图片作为背景/封面?
Stay hungry, Stay foolish. 如何在Word里面插入图片作为背景?其实很简单,开门见山,我们只需要这几步即可! 1.第一步,打开要插入图片的Word 2.第二步,插入图 ...
- ASP.NET Core 共享第三方依赖库部署的Bug(*.deps.json on 2.2.0 or 4.6.0 版本)
背景: I try to put the Microsoft.*.dll and System.*.dll togather to a new folder.以便把(第三方或)系统的和应用的dll分开 ...
- 音频处理贤内助--libsndfile
libsndfile是由Erik de Castro Lopo写的的广泛用于读写音频文件的C语言库.它支持的音频格式十分广泛并且能够自动的从一种格式到另外一种格式.它极大的方便了开发者,可以让开发者忽 ...
- win10下 anaconda 环境下python2和python3版本转换
在cmd的环境下,输入以下命令安装Python2.7的环境 conda create -n python27 python=2.7 anaconda 上面的代码创建了一个名为python27的pyth ...
- 每日分享!介绍Css 盒模型!
如何定义盒模型: 在CSS盒子模型理论中,页面中所有的元素都是看成一个盒子,并且还占据一定的空间. 一个页面是由很多这样的盒子组成的.这些盒子之间都会相会影响,因此我们掌握CSS盒模型相当重要.需要理 ...
- PHP内核之旅-6.垃圾回收机制
回收PHP 内核之旅系列 PHP内核之旅-1.生命周期 PHP内核之旅-2.SAPI中的Cli PHP内核之旅-3.变量 PHP内核之旅-4.字符串 PHP内核之旅-5.强大的数组 PHP内核之旅-6 ...
- Postman Mock Server
为了不影响前端开发的进度,一般后端都是先定数据结构,然后写个假接口让前端调用,这样前端就不必等着后端接口开发完成以后再开始了.届时,前后端以及UI和测试就可以并行,待双方都把各自的逻辑写好了,便可以联 ...
- IOS多态在项目中的应用
今天我们讲述一个知识点(大家可能遗漏的) 多态是面试程序设计(OOP)一个重要特征,但在iOS中,可能比较少的人会留意这个特征,实际上在开发中我们可能已经不经意的使用了多态.比如说: 有一个table ...
- Odd-e CSD Course Day 5
因為今天是最後一天了,我趕緊在這次結束前提出一些前一晚上想到的問題 1. 在TDD的循環中有重構,那 DB 也會進行重構嗎? 在TDD 的重構的過程,其實也經常會重構資料庫 , 但重構資料庫這裡有一個 ...
- entity framework 实现按照距离排序
在做项目时,经常会遇到“离我最近”这种需求.顾名思义,它需要根据用户的经纬度和事物的经纬度计算距离,然后进行排序,最后分页(当然这些操作要在数据库中进行,否则就变成假分页了). 我们通常可以用sql语 ...