Codeforces 1113C: Sasha and a Bit of Relax(位运算|异或)
发布日期:2022-04-01 13:25:19 浏览次数:33 分类:博客文章

本文共 3240 字,大约阅读时间需要 10 分钟。

time limit per test: 1 second

memory limit per test: 256 megabytes
input: standard input
output: standard output

Sasha likes programming. Once, during a very long contest, Sasha decided that he was a bit tired and needed to relax. So he did. But since Sasha isn’t an ordinary guy, he prefers to relax unusually. During leisure time Sasha likes to upsolve unsolved problems because upsolving is very useful.

Therefore, Sasha decided to upsolve the following problem:

You have an array

a
a
with
n
n
integers. You need to count the number of funny pairs
(
l
,
r
)
 
(
l
r
)
(l,r)\ (l≤r)
. To check if a pair
(
l
,
r
)
(l,r)
is a funny pair, take
m
i
d
=
l
+
r
1
2
mid=\frac{l+r-1}{2}
, then if
r
l
+
1
r−l+1
is an even number and
a
l
a
l
+
1
a
m
i
d
=
a
m
i
d
+
1
a
m
i
d
+
2
a
r
a_{l} \oplus a_{l+1} \oplus \ldots \oplus a_{m i d}=a_{m i d+1} \oplus a_{m i d+2} \oplus \ldots \oplus a_{r}
, then the pair is funny. In other words,
of elements of the left half of the subarray from
l
l
to
r
r
should be equal to
of elements of the right half. Note that
denotes the .

It is time to continue solving the contest, so Sasha asked you to solve this task.

Input

The first line contains one integer

n
 
(
2
n
3
1
0
5
)
n\ (2≤n≤3⋅10^5)
— the size of the array.

The second line contains

n
n
integers
a
1
,
a
2
,
,
a
n
(
0
a
i
<
2
20
)
a_{1}, a_{2}, \dots, a_{n}\left(0 \leq a_{i}<2^{20}\right)
— array itself.

Output

Print one integer — the number of funny pairs. You should consider only pairs where

r
l
+
1
r−l+1
is even number.

Examples

input

51 2 3 4 5

output

1

input

63 2 2 3 7 6

output

3

input

342 4 2

output

0

Note

Be as cool as Sasha, upsolve problems!

In the first example, the only funny pair is

(
2
,
5
)
(2,5)
, as
2
3
=
4
5
=
1
2 \oplus 3=4 \oplus 5=1
.

In the second example, funny pairs are

(
2
,
3
)
(2,3)
,
(
1
,
4
)
(1,4)
, and
(
3
,
6
)
(3,6)
.

In the third example, there are no funny pairs.

题意

n
n
个数,对于偶数长度的区间
[
l
,
r
]
[l,r]
m
i
d
=
l
+
r
1
2
mid=\frac{l+r-1}{2}
,要求
[
l
,
m
i
d
]
,
[
m
i
d
+
1
,
r
]
[l,mid],[mid+1,r]
两个区间内的数的异或值相等,问有多少个这样的区间

Solve

利用异或的性质:出现偶数次的数异或值为

0
0

如果

[
l
,
m
i
d
]
,
[
m
i
d
+
1
,
r
]
[l,mid],[mid+1,r]
区间数的异或值相等,则
[
l
,
r
]
[l,r]
区间的数的异或值为
0
0

可以推出:如果当前位置的异或值出现过,并且之前出现的位置与当前出现位置下标的奇偶性相同,那么这两个位置之间的区域就是题目中要求的funny pairs

Code

/*************************************************************************    > File Name: C.cpp    > Author: WZY    > Created Time: 2019年02月17日 19:59:36 ************************************************************************/#include
#define ll long long#define ull unsigned long long#define ms(a,b) memset(a,b,sizeof(a))#define pi acos(-1.0)#define INF 0x7f7f7f7fconst double E=exp(1);const int maxn=1e6+10;const int mod=1e9+7;using namespace std;ll sum[1<<20][2];int main(int argc, char const *argv[]){ ios::sync_with_stdio(false); cin.tie(0); int n; cin>>n; ll ans=0; ll x; ll res=0; sum[0][0]=1; for(int i=1;i<=n;i++) { cin>>x; res^=x; ans+=sum[res][i&1]; sum[res][i&1]++; } cout<
<

转载地址:https://www.cnblogs.com/Friends-A/p/11054964.html 如侵犯您的版权,请留言回复原文章的地址,我们会给您删除此文章,给您带来不便请您谅解!

上一篇:codevs 1300:文件排版(DP)
下一篇:Codeforces 1073D:Berland Fair(模拟)

发表评论

最新留言

能坚持,总会有不一样的收获!
[***.219.124.196]2024年04月17日 12时58分53秒

关于作者

    喝酒易醉,品茶养心,人生如梦,品茶悟道,何以解忧?唯有杜康!
-- 愿君每日到此一游!

推荐文章