题目大意

给你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. 用 Spring Boot 和 MybatisPlus 快速构建项目

    自动生成 public class MPGenerator { public static void main(String[] args) { AutoGenerator autoGenerator ...

  2. LAMP 和 LNMP

    #0x01 组成: LAMP==Linux+Apache+Mysql+PHP LNMP==Linux+Nginx+Mysql+PHP LANMP==linux + nginx + apache + m ...

  3. 记一次函数异常(getopt_long)

    前言 以下参考博客以及man手册. getopt_long函数,getopt_long函数包含了getopt函数的功能,并且还可以指定"长参数"(或者说长选项),与getopt函数 ...

  4. 装个Mysql怎么就这么难?

    2020/5/20 程序员是没有女朋友的~~ 声明:这篇文章是我第一次安装MySQL,主要记录了安装的艰辛过程,内容很凌乱,不适合作为参考. CentOS 6.10安装MySQL一直出问题,换了各种不 ...

  5. 使用Excel设计fastreport报表

    最近做项目遇到一些报表设计的问题.项目的报表使用 Fast Report,然而该控件调整报表格式比较繁琐,有时候调整某一栏的宽度,整个标题栏都需要调整,且调整以后 还不一定能对齐.客户的很多报表都是基 ...

  6. Linux下日志文件过大解决方案

    很多Linux服务器里的应用程序都是无间断的输出日志,这对于服务器的硬盘是一个很大的考验.良许之前也分享过一篇文章,介绍如何让应用程序在后台执行: linux后台执行命令:&与nohup的用法 ...

  7. Linux里隐藏的计算器,你知道它的奥秘吗?

    Linux里隐藏的计算器,你知道它的奥秘吗? 大家都知道,windows下有个计算器工具,我们在工作生活中经常使用到它.但是,你可知Linux下也同样有个计算器吗? 当然,良许说的是命令行下的计算器工 ...

  8. Spring Cloud Alibaba生态探索:Dubbo、Nacos及Sentinel的完美结合

    @ 目录 背景 一.项目框架 1.1 采用IDEA和Maven多模块进行项目搭建 1.2 模块管理及版本管理 二.微服务公共接口 2.1 定义一个公共接口Api 2.2 pom.xml 2.3 Goo ...

  9. python-面向过程面向对象和栈的实现

    0x01 大纲 面向过程 函数 参数传递 返回 面向对象 类 栈的数据结构实现 0x02 例子 def add(a,b): return a+b if __name__ == '__main__': ...

  10. day55:django:cookie&session

    目录 1.Cookie 1.Cookie前戏 2.Cookie的引入 3.django中操作cookie 2.Session 1.cookie的局限性 2.session技术 3.django操作se ...