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 namespace std;
#define fi first
#define se second
#define pi acos(-1.0)
#define LL long long
//#define mp make_pair
#define pb push_back
#define ls rt<<1, l, m
#define rs rt<<1|1, m+1, r
#define ULL unsigned LL
#define pll pair<LL, LL>
#define pii pair<int, int>
#define piii pair<pii, pii>
#define mem(a, b) memset(a, b, sizeof(a))
#define fio ios::sync_with_stdio(false);cin.tie(0);cout.tie(0);
#define fopen freopen("in.txt", "r", stdin);freopen("out.txt", "w", stout);
//head

const int N = 3e5 + 100;
LL a[N];
int cnt[N], sum[N][2];
int main() {
    int n;
    scanf("%d", &n);
    for (int i = 1; i <= n; i++) scanf("%lld", &a[i]);
    for (int i = 1; i <= n; i++) {
        for (int j = 0; j < 63; j++) {
           if(a[i] & (1LL << j)) cnt[i]++;
        }
    }
    sum[0][0] = 1;
    sum[0][1] = 0;
    int tot = 0;
    for (int i = 1; i <= n; i++) {
        sum[i][0] = sum[i-1][0];
        sum[i][1] = sum[i-1][1];
        tot += cnt[i];
        if(tot&1) sum[i][1]++;
        else sum[i][0]++;
    }
    LL ans = 0;
    for (int l = 1; l <= n; l++) {
        int up = min(l+127, n);
        int mx = -0x3f3f3f3f, tot = 0;
        for (int i = l; i <= up; i++) {
            mx = max(mx, cnt[i]);
            tot += cnt[i];
            if(tot%2 == 0 && tot >= mx*2) ans++;
        }
    }
    tot = 0;
    for (int i = 1; i <= 128; i++) tot += cnt[i];
    for (int i = 129; i <= n; i++) {
        tot += cnt[i];
        if(tot&1) ans += sum[i-129][1];
        else ans += sum[i-129][0];
    }
    printf("%lld\n", ans);
    return 0;
}

原文地址:https://www.cnblogs.com/widsom/p/9737489.html

时间: 2024-11-02 16:20:17

Codeforces 1053 B - 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

[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\)的个

Codeforces #550 (Div3) - G.Two Merged Sequences(dp / 贪心)

Problem  Codeforces #550 (Div3) - G.Two Merged Sequences Time Limit: 2000 mSec Problem Description Two integer sequences existed initially, one of them was strictly increasing, and another one — strictly decreasing. Strictly increasing sequence is a

[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 450B div.2 Jzzhu and Sequences 矩阵快速幂or规律

Jzzhu has invented a kind of sequences, they meet the following property: You are given x and y, please calculate fn modulo 1000000007 (109 + 7). Input The first line contains two integers x and y (|x|, |y| ≤ 109). The second line contains a single i

Codeforces 1082 A. Vasya and Book-题意 (Educational Codeforces Round 55 (Rated for Div. 2))

A. Vasya and Book time limit per test 2 seconds memory limit per test 256 megabytes input standard input output standard output Vasya is reading a e-book. The file of the book consists of nn pages, numbered from 11 to nn. The screen is currently disp

【CodeForces 577C】Vasya and Petya’s Game

链接 某个数x属于[1,n],至少询问哪些数“x是否是它的倍数”才能判断x.找出所有质因数和质因数的幂即可. #include<cstdio> #include<algorithm> #define N 1005 using namespace std; int n,pr[N],ans[N],cnt; int main(){ scanf("%d",&n); for(int i=2;i<=n;i++) if(!pr[i]) for(int j=i*2

codeforces 493 C Vasya and Basketball

题意:给出三分线的值d,分别有两支队伍,如果小于等于d,得2分,如果大于d,得三分,问使得a-b最大时的a,b 一看到题目,就想当然的去二分了----啥都没分出来---55555555 后来才知道不能二分,因为随着d的增大,两个队的得分都会逐渐减少,但是两个队伍的得分的差值的单调性却不知道 是这一篇这样讲的 http://www.cnblogs.com/huangxf/p/4142760.html 然后就依次枚举d的值,维护一个最大值 1 #include<iostream> 2 #inclu

codeforces 493 D Vasya and Chess【 博弈 】

题意:给出n*n的棋盘,白方在(1,1),黑方在(1,n)处,每一步可以上下左右对角线走,哪个先抓到另一个,则它获胜 可以画一下,发现n是奇数的时候,白方先走,无论它怎么走,黑方和它走对称的,黑方都一定能赢 n是偶数的时候,将白方走到(1,2)就变成奇数的情况,白方必胜 1 #include<iostream> 2 #include<cstdio> 3 #include<cstring> 4 #include <cmath> 5 #include<st