题目大意

给你n个数,你可以交换一个数的任意二进制位,问你可以选出多少区间经过操作后异或和是0。

思路分析:

根据题目,很容易知道,对于每个数,我们可以无视它的1在那些位置,只要关注它有几个1即可,如果它的1的数量可以通过加减为0,那么这个区间就是合法的。

可以看到,两个数(分别有x,y个1,且x〉y)通过交换位置再进行异或操作,可能获得的新数中1的数量为y-x,y-x+2,y-x+4,...,x+y-2,x+y。

那么对于每一个左端点(l),它的合法右端点(r)的数量为sum[r]-sum[l-1]为偶数的r的个数(sum为前缀和数组)。同时,这个区间还要满足区间内1的数量最多的数(假设它在第k位)要小于sum[k]-sum[l-1]的一半,因为假如说它不满足这一条件的话,那么就没有足够多的1同它相抵消,也就不会合法。

那么我们应该如何避免这种状况呢?

很简单,可能会是不合法右端点的几个点(最多64个)直接暴力做就行了。

代码:

var
a,sum,odd,even:array[0..300000]of longint;
i,j,s,max,n,m:longint;
ans,x:int64;
begin
read(n);
for i:=1 to n do
begin
read(x);
while x>0 do
begin
a[i]:=a[i]+x and 1;
x:=x>>1;
end;
end;
for i:=1 to n do
begin
sum[i]:=sum[i-1]+a[i];
odd[i]:=odd[i-1]; even[i]:=even[i-1];
if sum[i]and 1=1 then inc(odd[i]) else inc(even[i]);
end;
for i:=1 to n do
if sum[i-1]and 1=1 then ans:=ans+odd[n]-odd[i-1]
else ans:=ans+even[n]-even[i-1];
writeln(ans);
for i:=1 to n do
begin
if i+64>n then m:=n else m:=i+64;
s:=0; max:=0;
for j:=i to m do
begin
if a[j]>max then max:=a[j];
s:=s+a[j];
if (2*max>s)and(s mod 2=0) then dec(ans);
end;
end;
writeln(ans);
end.

Code Forces 1030E的更多相关文章

  1. 思维题--code forces round# 551 div.2

    思维题--code forces round# 551 div.2 题目 D. Serval and Rooted Tree time limit per test 2 seconds memory ...

  2. Code Forces 796C Bank Hacking(贪心)

    Code Forces 796C Bank Hacking 题目大意 给一棵树,有\(n\)个点,\(n-1\)条边,现在让你决策出一个点作为起点,去掉这个点,然后这个点连接的所有点权值+=1,然后再 ...

  3. Code Forces 833 A The Meaningless Game(思维,数学)

    Code Forces 833 A The Meaningless Game 题目大意 有两个人玩游戏,每轮给出一个自然数k,赢得人乘k^2,输得人乘k,给出最后两个人的分数,问两个人能否达到这个分数 ...

  4. Code Forces 543A Writing Code

    题目描述 Programmers working on a large project have just received a task to write exactly mm lines of c ...

  5. code forces 383 Arpa's loud Owf and Mehrdad's evil plan(有向图最小环)

    Arpa's loud Owf and Mehrdad's evil plan time limit per test 1 second memory limit per test 256 megab ...

  6. code forces 382 D Taxes(数论--哥德巴赫猜想)

    Taxes time limit per test 2 seconds memory limit per test 256 megabytes input standard input output ...

  7. code forces Watermelon

    /* * Watermelon.cpp * * Created on: 2013-10-8 * Author: wangzhu */ /** * 若n是偶数,且大于2,则输出YES, * 否则输出NO ...

  8. code forces Jeff and Periods

    /* * c.cpp * * Created on: 2013-10-7 * Author: wangzhu */ #include<cstdio> #include<iostrea ...

  9. Code Forces Gym 100971D Laying Cables(单调栈)

    D - Laying Cables Time Limit:2000MS     Memory Limit:262144KB     64bit IO Format:%I64d & %I64u ...

随机推荐

  1. Just an Old Puzzle(2019多校1007)

    Problem Description You are given a 4 × 4 grid, which consists of 15 number cells and an empty cell. ...

  2. codewars sum of pairs

    Sum of Pairs Given a list of integers and a single sum value, return the first two values (parse fro ...

  3. 小程序开发-基础组件icon/text/progress入门

    小程序的基础组件--基础内容 基础内容分为三大组件: 1. icon--图标 index.wxml <view class="group"> <block wx: ...

  4. 小程序开发-微信小程序开发入门

    分享一个微信小程序开发的基本流程,仅供参考. 第一步:注册微信小程序公众号,注册成功后,登录微信公众号管理后台,等待下一步操作. 第二步:进入微信小程序的后台后,下载微信内置的微信小程序开发者工具,以 ...

  5. PHP + Redis 生成自定义订单编号

    /** * 订单编号生成规则 * 14位 = 6位时间 + 5位自增 + 3位ID * @param string $prefix 前缀: 默认为order * @param int $userId ...

  6. 鼠标移到图片上图片放大【css3实例】

    <!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title> ...

  7. TaskContinuationsOptions.ExecuteSynchronously探秘

    TPL - Task Parallel Library为我们提供了Task相关的api,供我们非常方便的编写并行代码,而不用自己操作底层的Thread类.使用Task的优势是显而易见的: 提供返回值 ...

  8. 360浏览器最小字号12的坑 -彻底搞清rem

    之前做响应式网站,使用rem作为单位.因为浏览器的默认字号是16px,设置html {font-size: 62.5%; /*10 ÷ 16 × 100% = 62.5%*/},刚好1rem =10p ...

  9. 【高并发】面试官:Java中提供了synchronized,为什么还要提供Lock呢?

    写在前面 在Java中提供了synchronized关键字来保证只有一个线程能够访问同步代码块.既然已经提供了synchronized关键字,那为何在Java的SDK包中,还会提供Lock接口呢?这是 ...

  10. 解决IDEA打包出现中文乱码的问题

    这主要是maven编译时编码问题导致的. 解决办法: 1.在IDEA的File里面打开Settings. 2.找到Runner,在VM Options输入-DarchetypeCatalog=inte ...