Codeforces 1030E 【暴力构造】

LINK



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

思路

充分必要条件:

  • 区间中二进制1的个数是偶数
  • 区间中二进制位最多的一个数的二进制个数小于等于和的一半

然后因为每个数最少会贡献1,所以直接暴力向前跳128位,再之前的就直接前缀和做掉就可以了


#include<bits/stdc++.h>
using namespace std;
#define LL long long
#define IL inline
#define fu(a,b,c) for(int a=b;a<=c;++a)
#define fd(a,b,c) for(int a=b;a>=c;--a)
#define FLIE ""
IL LL read(){
  LL ans=0,w=1;char c=getchar();
  while(!isdigit(c)&&c!='-')c=getchar();
  if(c=='-')w=-1,c=getchar();
  while(isdigit(c))ans=(ans<<1)+(ans<<3)+c-'0',c=getchar();
  return ans*w;
}
#define N 300010
LL n,a[N];
LL sum[N],sumj[N],sumo[N];
LL cnt(LL x){
  LL num=0;
  while(x){
    if(x&1)num++;
    x>>=1;
  }
  return num;
}
int main(){
  n=read();
  LL ans=0;
  sumo[0]=1;
  fu(i,1,n)a[i]=read();
  fu(i,1,n){
    a[i]=cnt(a[i]);
    sum[i]=sum[i-1]+a[i];
    LL maxv=a[i];
    fd(j,i-1,max(i-128,1)){
      maxv=max(maxv,a[j]);
      LL tmp=sum[i]-sum[j-1];
      if(maxv*2<=tmp&&tmp%2==0)ans++;
    }
    if(i>129){
      if(sum[i]&1)ans+=sumj[i-130];
      else ans+=sumo[i-130];
    }
    sumj[i]=sumj[i-1]+(sum[i]%2==1);
    sumo[i]=sumo[i-1]+(sum[i]%2==0);
  }
  printf("%I64d",ans);
  return 0;
}

原文地址:https://www.cnblogs.com/dream-maker-yk/p/9695445.html

时间: 2024-11-08 02:50:07

Codeforces 1030E 【暴力构造】的相关文章

暴力+构造 Codeforces Round #283 (Div. 2) C. Removing Columns

题目传送门 1 /* 2 题意:删除若干行,使得n行字符串成递增排序 3 暴力+构造:从前往后枚举列,当之前的顺序已经正确时,之后就不用考虑了,这样删列最小 4 */ 5 /************************************************ 6 Author :Running_Time 7 Created Time :2015-8-3 10:49:53 8 File Name :C.cpp 9 ************************************

CodeForces 26C Parquet 构造题

题目链接:点击打开链接 #include <stdio.h> #include <string.h> #include <iostream> #include <algorithm> #include <math.h> #include <set> using namespace std; #define N 105 int n,m,a,b,c; char s[N][N]; set<char>myset; bool inm

Karen and Game CodeForces - 816C (暴力+构造)

On the way to school, Karen became fixated on the puzzle game on her phone! The game is played as follows. In each level, you have a grid with n rows and mcolumns. Each cell originally contains the number 0. One move consists of choosing one row or c

Codeforces Round #487 (Div. 2) A Mist of Florescence (暴力构造)

C. A Mist of Florescence time limit per test 1 second memory limit per test 256 megabytes input standard input output standard output As the boat drifts down the river, a wood full of blossoms shows up on the riverfront. "I've been here once," M

CodeForces 670D1 暴力或二分

今天,开博客,,,激动,第一次啊 嗯,,先来发水题纪念一下 D1. Magic Powder - 1 This problem is given in two versions that differ only by constraints. If you can solve this problem in large constraints, then you can just write a single solution to the both versions. If you find

codeforces #306D Polygon 构造

题目大意:给定n,要求构造一个凸n边形,使得每个内角都相同,每条边长度都不同 膜拜题解 其实我一开始想的是构造一个正n边形然后把每条边微移一下--不过似乎不是很好写的样子= = #include <cmath> #include <cstdio> #include <cstring> #include <iostream> #include <algorithm> #define M 110 #define PI 3.14159265358979

Codeforces 1030D 【构造】

LINK 题目大意:给你n,m,k,让你在一个n*m的点阵里构造出一个面积为\(\frac{n*m}{k}\)的三角形 思路 首先要有一个结论是整点三角形的面积分母最多为2,然后就可以判断不存在的情况了 接下来就直接进行构造就可以了 #include<bits/stdc++.h> using namespace std; #define LL long long #define IL inline #define fu(a,b,c) for(LL a=b;a<=c;++a) #defin

Vicious Keyboard CodeForces - 801A (暴力+模拟)

题目链接 题意: 给定一个字符串,最多更改一个字符,问最多可以有多少个"VK"子串? 思路: 由于数据量很小,不妨尝试暴力写.首先算出不更改任何字符的情况下有多个VK字串,然后尝试每一次更改一个位置的字符,然后暴力算出有多少个VK,取出这些答案中 的最大值,即是答案. 代码: #include <iostream> #include <cstdio> #include <cstring> #include <algorithm> #inc

New Roads CodeForces - 746G (树,构造)

大意:构造n结点树, 高度$i$的结点有$a_i$个, 且叶子有k个. 先确定主链, 然后贪心放其余节点. #include <iostream> #include <algorithm> #include <cstdio> #include <math.h> #include <set> #include <map> #include <queue> #include <string> #include &l