Codeforces 1053 B - Vasya and Good Sequences

Posted widsom

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了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
", ans);
    return 0;
}

 

以上是关于Codeforces 1053 B - Vasya and Good Sequences的主要内容,如果未能解决你的问题,请参考以下文章

Codeforces 837E. Vasya's Function

Codeforces 837E Vasya's Function - 数论

Codeforces 837E Vasya's Function 数论 找规律

Codeforces Round #354 (Div. 2) C. Vasya and String

CodeForces 1107F. Vasya and Endless Credits

Educational Codeforces Round 26 E - Vasya's Function