[CF1030E]Vasya and Good Sequences

[CF1030E]Vasya and Good Sequences

题目大意:

给定一个长度为\(n(n\le3\times10^5)\)的数列\(a_i(1\le a_i\le10^{18})\)。可以任意对若干数进行操作,交换这个数的任意二进制位。求有多少区间,使得这个区间内的数经过操作使得异或和为\(0\)。

思路:

显然这与\(a_i\)本身的值无关,只与二进制下\(1\)的个数有关。

一个区间\([l,r]\)需要满足以下条件:

  1. 二进制下\(1\)的个数为偶数;
  2. 二进制下\(1\)的个数最多的那个数的\(1\)的个数不超过剩下的数。

对于条件\(1\),我们可以很自然地得到一个\(\mathcal O(n)\)的做法。

对于条件\(2\),由于对于每个数,\(1\)的个数至少是\(1\),最多不超过\(60\),所以只需要枚举\(60\)个即可。

时间复杂度\(\mathcal O(60n)\)。

源代码:

#include<cstdio>
#include<cctype>
#include<algorithm>
typedef long long int64;
inline int64 getint() {
    register char ch;
    while(!isdigit(ch=getchar()));
    register int64 x=ch^'0';
    while(isdigit(ch=getchar())) x=(((x<<2)+x)<<1)+(ch^'0');
    return x;
}
const int N=3e5+1;
int c[N],sum[N],cnt[2]={1};
int main() {
    const int n=getint();
    int64 ans=0;
    for(register int i=1;i<=n;i++) {
        c[i]=__builtin_popcountll(getint());
        sum[i]=sum[i-1]+c[i];
        ans+=cnt[sum[i]&1];
        for(register int j=i,k=i+1,max=0;j>=1&&j>=i-61;j--) {
            while(k>j) max=std::max(max,c[--k]);
            if(max*2>sum[i]-sum[j-1]&&(sum[i]%2==sum[j-1]%2)) ans--;
        }
        cnt[sum[i]%2]++;
    }
    printf("%lld\n",ans);
    return 0;
}

原文地址:https://www.cnblogs.com/skylee03/p/9740195.html

时间: 2024-11-04 12:14:43

[CF1030E]Vasya and Good Sequences的相关文章

codeforces 1041 E.Vasya and Good Sequences(暴力?)

E. Vasya and Good Sequences time limit per test 2 seconds memory limit per test 256 megabytes input standard input output standard output Vasya has a sequence $$$a$$$ consisting of $$$n$$$ integers $$$a_1, a_2, \dots, a_n$$$. Vasya may pefrom the fol

Codeforces 1053 B - Vasya and Good Sequences

B - Vasya and Good Sequences 思路: 满足异或值为0的区间,必须满足一下条件: 1.区间中二进制1的个数和为偶数个; 2.区间二进制1的个数最大值的两倍不超过区间和. 如果区间长度大于128,第二个条件肯定满足,所以我们只要暴力区间长度小于128的就可以了 代码: #pragma GCC optimize(2) #pragma GCC optimize(3) #pragma GCC optimize(4) #include<bits/stdc++.h> using

[Codeforces 1058E] Vasya and Good Sequences

[题目链接] https://codeforces.com/contest/1058/problem/E [算法] 显然 , 我们只需考虑序列中每个数的二进制表示下1的个数即可. 不妨令Ai表示第i个数的二进制表示下1的个数. 一个子序列[L,R]是"好"的当且仅当 : 1. sigma{ Ai }  (L <= i <= R) 为偶数 2. max{ Ai } (L <= i <= R) <= sigma{ Ai } / 2 枚举序列左端点L , 可以用

Codeforces Round #281 (Div. 2) B. Vasya and Wrestling 水题

B. Vasya and Wrestling Vasya has become interested in wrestling. In wrestling wrestlers use techniques for which they are awarded points by judges. The wrestler who gets the most points wins. When the numbers of points of both wrestlers are equal, th

[LeetCode]Repeated DNA Sequences

题目:Repeated DNA Sequences 给定包含A.C.G.T四个字符的字符串找出其中十个字符的重复子串. 思路: 首先,string中只有ACGT四个字符,因此可以将string看成是1,3,7,20这三个数字的组合串: 并且可以发现{ACGT}%5={1,3,2,0};于是可以用两个位就能表示上面的四个字符: 同时,一个子序列有10个字符,一共需要20bit,即int型数据类型就能表示一个子序列: 这样可以使用计数排序的思想来统计重复子序列: 这个思路时间复杂度只有O(n),但是

Codeforces Round #281 (Div. 2) A. Vasya and Football 暴力

A. Vasya and Football Vasya has started watching football games. He has learned that for some fouls the players receive yellow cards, and for some fouls they receive red cards. A player who receives the second yellow card automatically receives a red

Repeated DNA Sequences

package cn.edu.xidian.sselab.hashtable; import java.util.ArrayList;import java.util.HashSet;import java.util.List;import java.util.Set; /** *  * @author zhiyong wang * title: Repeated DNA Sequences * content: *  All DNA is composed of a series of nuc

codeforces 584C Marina and Vasya

C. Marina and Vasya Marina loves strings of the same length and Vasya loves when there is a third string, different from them in exactly t characters. Help Vasya find at least one such string. More formally, you are given two strings s1, s2 of length

Codeforces Round #281 (Div. 2) C. Vasya and Basketball 排序

C. Vasya and Basketball Vasya follows a basketball game and marks the distances from which each team makes a throw. He knows that each successful throw has value of either 2 or 3 points. A throw is worth 2 points if the distance it was made from does