Codeforces Contest 1030 problem E Vasya and Good Sequences——DP

202 篇文章 6 订阅

Vasya has a sequence a consisting of n integers a1,a2,…,an. Vasya may pefrom the following operation: choose some number from the sequence and swap any pair of bits in its binary representation. For example, Vasya can transform number 6 (…000000001102) into 3 (…000000000112), 12 (…0000000011002), 1026 (…100000000102) and many others. Vasya can use this operation any (possibly zero) number of times on any number from the sequence.

Vasya names a sequence as good one, if, using operation mentioned above, he can obtain the sequence with bitwise exclusive or of all elements equal to 0.

For the given sequence a1,a2,…,an Vasya’d like to calculate number of integer pairs (l,r) such that 1≤l≤r≤n and sequence al,al+1,…,ar is good.

Input
The first line contains a single integer n (1≤n≤3⋅105) — length of the sequence.

The second line contains n integers a1,a2,…,an (1≤ai≤1018) — the sequence a.

Output
Print one integer — the number of pairs (l,r) such that 1≤l≤r≤n and the sequence al,al+1,…,ar is good.

Examples
inputCopy
3
6 7 14
outputCopy
2
inputCopy
4
1 2 1 16
outputCopy
4
Note
In the first example pairs (2,3) and (1,3) are valid. Pair (2,3) is valid since a2=7→11, a3=14→11 and 11⊕11=0, where ⊕ — bitwise exclusive or. Pair (1,3) is valid since a1=6→3, a2=7→13, a3=14→14 and 3⊕13⊕14=0.

In the second example pairs (1,2), (2,3), (3,4) and (1,4) are valid.

题意:

给你一个数组,有一种变换是可以交换这个数上所有的0和1的位置,比如3是00000000011,它可以变成…00100000001,…0000000011000…,问你有哪些区间可以通过异或操作变成0。

题解:

很明显可以得出这样的结论:区间至少有两个数,区间所有1的和是偶数,区间最大值要小于等于区间其他数之和。
那么我们通过dp[i][j]记录当前位置i之前有多少累加为偶数和奇数的区间。
dp[i][0]就是偶数,dp[i][1]就是奇数,
那么状态转移方程就是
if(a[i]%2==0)
dp[i][0]=dp[i-1][0]+1,dp[i][1]=dp[i-1][1];
else
dp[i][0]=dp[i-1][1],dp[i][1]=dp[i-1][0]+1;
当a[i]时偶数的时候,那么就是前面偶数的区间+1,前面奇数的区间保持原样,a[i]是奇数的时候,就是前面奇数的情况+1,前面偶数的情况变成奇数。
但是要注意区间最大值要小于等于区间其他数之和,但是这道题目每个数至少是1,所以只需要找60+的范围就好了,这里面是偶数且最大值超过其他数的区间要剪掉,最后记得输出long long

#include<bits/stdc++.h>
using namespace std;
#define ll long long
const int N=3e5+5;
int a[N],sum[N],dp[N][2];//dp[i][0]:num of even
int main()
{
    int n;
    scanf("%d",&n);
    ll x;
    for(int i=1;i<=n;i++)
    {
        scanf("%lld",&x);
        int num=0;
        while(x)
        {
            if((x&1))
                num++;
            x>>=1;
        }
        a[i]=num;
        sum[i]=sum[i-1]+a[i];
    }
    ll ans=0;
    for(int i=1;i<=n;i++)
    {
        if(a[i]%2==0)
            dp[i][0]=dp[i-1][0]+1,dp[i][1]=dp[i-1][1];
        else
            dp[i][0]=dp[i-1][1],dp[i][1]=dp[i-1][0]+1;
        if(a[i]%2)
            ans+=dp[i-1][1];
        else
            ans+=dp[i-1][0];
        int maxn=a[i];
        for(int j=i-1;j>=max(1,i-70);j--)
        {
            int num=sum[i]-sum[j-1];
            maxn=max(maxn,a[j]);
            if(num%2==0&&maxn*2>num)
                ans--;
        }
    }
    return 0*printf("%lld\n",ans);
}
  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值