[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 , 可以用后缀和处理R

时间复杂度 :O(N)

[代码]

#include<bits/stdc++.h>
using namespace std;
const int MAXN = 3e5 + 10;

int n;
int cnt[MAXN][2];
int a[MAXN];
long long ans;

template <typename T> inline void read(T &x)
{
    T f = 1; x = 0;
    char c = getchar();
    for (; !isdigit(c); c = getchar()) if (c == ‘-‘) f = -f;
    for (; isdigit(c); c = getchar()) x = (x << 3) + (x << 1) + c - ‘0‘;
    x *= f;
}

int main()
{

        read(n);
        for (int i = 1; i <= n; i++)
        {
                long long x;
                read(x);
                while (x > 0)
                {
                        a[i] += x & 1;
                        x >>= 1;
                }
        }
        int suf = 0;
        cnt[n + 1][0] = 1;
        for (int i = n; i >= 1; i--)
        {
                int sum = 0 , mx = 0;
                int add = 0;
                for (int j = i; j <= n && j - i < 65; j++)
                {
                        sum += a[j];
                        mx = max(mx,a[j]);
                        if (sum % 2 == 0 && mx > sum - mx) add--;
                }
                suf += a[i];
                add += cnt[i + 1][suf & 1];
                ans += add;
                cnt[i][0] = cnt[i + 1][0];
                cnt[i][1] = cnt[i + 1][1];
                if (suf & 1) cnt[i][1]++;
                else cnt[i][0]++;
        }

        printf("%I64d\n",ans);

        return 0;

}

原文地址:https://www.cnblogs.com/evenbao/p/9726874.html

时间: 2024-08-30 14:13:19

[Codeforces 1058E] 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

[2016-04-13][codeforces][447][C][DZY Loves Sequences]

时间:2016-04-13 23:39:47 星期三 题目编号:[2016-04-13][codeforces][447][C][DZY Loves Sequences] 题目大意:给定一串数字,问改变其中一个数字之和,最长能得到多长的严格增加的子串 分析: 维护每个数字往左和往右能延续多长(严格减,增),然后枚举每个点, 如果这个点已经在一个严格增加的序列中,那么ans =min(n, max(ans , l[i] + r[i] + 1)) 即左右两边延伸之后,改变后面非递增的一个数字 注意这

CodeForces 577C Vasya and Petya&#39;s Game 数学

题意就是给你一个1到n的范围 你每次可以问这个数是否可以被某一个数整除 问你要猜多少数才能确定这个数…… 一开始一点思路也没有 后来查了一下才知道 每个数都可以分为几个质数的整数次幂相乘得到…… 1 #include<stdio.h> 2 #include<iostream> 3 #include<algorithm> 4 #include<math.h> 5 #include<string.h> 6 #include<string>

CodeForces - 837E - Vasya&#39;s Function | Educational Codeforces Round 26

/* CodeForces - 837E - Vasya's Function [ 数论 ] | Educational Codeforces Round 26 题意: f(a, 0) = 0; f(a, b) = 1 + f(a, b-gcd(a, b)); 求 f(a, b) , a,b <= 1e12 分析: b 每次减 gcd(a, b) 等价于 b/gcd(a,b) 每次减 1 减到什么时候呢,就是 b/gcd(a,b)-k 后 不与 a 互质 可先将 a 质因数分解,b能除就除,不能

codeforces#FF(div2) DZY Loves Sequences

n个数,可以任意改变其中一个数,求最长的上升子区间长度 思路:记录一个from[i]表示从位置i的数开始最长的上升区间长度 记录一个to[i]表示到位置i的数所能达到的最长上升区间长度 枚举要改变的数的位置i,此时能达到的长度为to[i - 1] + from[i + 1] + 1,取最大值 //#pragma comment(linker, "/STACK:102400000,102400000") //HEAD #include <cstdio> #include &l

CodeForces 425C Sereja and Two Sequences

题意: 两组数字a和b  如果a[i]等于b[j]  则可将a[i]和b[j]前所有数字删掉  这种操作花费e体力  得到1元钱  或者一次删掉所有数字  这种操作花费等于曾经删除的所有数字个数  做完后得到所有钱  问 一共s体力 可以得到多少钱 思路: dp+二分 由数据可知最多拿到300元钱  因此可以定义  dp[i][j]表示有i元钱时  b串删除到了j处时  a串删到的位置 状态转移为 dp[i][j] = lower_bound ( a[j] , dp[i-1][j-1] + 1

Codeforces 837E Vasya&#39;s Function 数论 找规律

题意:定义F(a,0) = 0,F(a,b) = 1 + F(a,b - GCD(a,b).给定 x 和 y (<=1e12)求F(x,y). 题解:a=A*GCD(a,b) b=B*GCD(a,b),那么b-GCD(a,b) = (B-1)*GCD(a,b),如果此时A和B-1依然互质,那么GCD不变下一次还是要执行b-GCD(a,b).那么GCD什么时候才会变化呢?就是说找到一个最小的S,使得(B-S)%T=0其中T是a的任意一个因子.变形得到:B%T=S于是我们知道S=min(B%T).也

Codeforces 837E Vasya&#39;s Function - 数论

Vasya is studying number theory. He has denoted a function f(a, b) such that: f(a, 0) = 0; f(a, b) = 1 + f(a, b - gcd(a, b)), where gcd(a, b) is the greatest common divisor of a and b. Vasya has two numbers x and y, and he wants to calculate f(x, y).