Codeforces Round #512 (Div. 2, based on Technocup 2019 Elimination Round 1) C. Vasya and Golden Ticket

C. Vasya and Golden Ticket

time limit per test

1 second

memory limit per test

256 megabytes

input

standard input

output

standard output

Recently Vasya found a golden ticket — a sequence which consists of nn digits a1a2…ana1a2…an. Vasya considers a ticket to be lucky if it can be divided into two or more non-intersecting segments with equal sums. For example, ticket 350178350178 is lucky since it can be divided into three segments 350350, 1717 and 88: 3+5+0=1+7=83+5+0=1+7=8. Note that each digit of sequence should belong to exactly one segment.

Help Vasya! Tell him if the golden ticket he found is lucky or not.

Input

The first line contains one integer nn (2≤n≤1002≤n≤100) — the number of digits in the ticket.

The second line contains nn digits a1a2…ana1a2…an (0≤ai≤90≤ai≤9) — the golden ticket. Digits are printed without spaces.

Output

If the golden ticket is lucky then print "YES", otherwise print "NO" (both case insensitive).

Examples

input

Copy

573452

output

Copy

YES

input

Copy

41248

output

Copy

NO

Note

In the first example the ticket can be divided into 77, 3434 and 5252: 7=3+4=5+27=3+4=5+2.

In the second example it is impossible to divide ticket into segments with equal sum.

【题意】:

  题意的大概意思是是不是存在两组以上的序列使得他们相等,比如 773434 可以等价于 7 = 7 = 3 + 4 = 3 + 4

【思路】:

  我的思路是先求一次前缀和,然后我们要判断的是存不存在大于2组的序列,那么前缀和sum[n] = x * n;x是满足的序列

那么我们只要假设x,然后翻倍向上求就可以了,翻倍查询是不是存在,x * 1, x * 2, x * 3 -----x * n这样的前缀和,因为

这个的总和很少,所以我们可以开一个足够大的标记数组来标记sum[i],,,,,,sum[n]的数值,这样的话,查询的复杂度就是O(1)了

我们只要枚举sum[i] ----sum[m],如果sum[m] > sum[n] / 2的话就不用判断了,因为肯定不存在答案,大概复杂度是O(n * log n sum[i])

因为数据量不大,所以可以通过

全都是0的情况需要特判

附上代码:

#include <cstdio>
#include <cstring>
#include <iostream>
using namespace std;
int flag[2000];
int math[2000];
int sum[2000];
char str[1005];
int main()
{
    int n;
    while(~scanf("%d",&n))
    {
        memset(flag, 0, sizeof(flag));
        memset(math, 0, sizeof(math));
        memset(sum, 0, sizeof(sum));
        memset(str, 0, sizeof(str));
        scanf("%s",str);
        for(int i = 0; i < n; i ++)
        {
            math[i] = str[i] - ‘0‘;
        }
        sum[0] = math[0];
        flag[sum[0]] = 1;
        for(int i = 1; i < n; i ++)
        {
            sum[i] = sum[i - 1] + math[i];
            flag[sum[i]] ++;
        }
        if(flag[0] >= 2 && sum[n - 1] == 0)
        {
            printf("YES\n");
            continue;
        }
        else
        {
            int nums = sum[n - 1] / 2 + 1;
            int cs = 0;
            for(int i = 0; i < n; i ++)
            {
                if(sum[i] == 0)
                    continue;
                if(cs)
                    break;
                if(sum[i] > nums)
                    break;
                else
                {
                    int summ = 0;
                    int fff = 1;
                    int ff = 0;
                    while(summ < sum[n - 1])
                    {
                        //printf("...\n");
                        summ = summ + sum[i];
                        ff ++;
                        if(flag[summ] >= 1 && summ == sum[n - 1] && ff >= 2)
                        {
                            fff = 0;
                            break;
                        }
                        if(!flag[summ])
                        {
                            fff = 1;
                            break;
                        }
                    }
                    //printf("%d %d\n",ff,fff);
                    if(!fff)
                    {
                        printf("YES\n");
                        cs = 1;
                        break;
                    }
                }
            }
            if(!cs)
                printf("NO\n");
        }
    }
    return 0;
}

原文地址:https://www.cnblogs.com/qq136155330/p/9710882.html

时间: 2025-01-19 22:27:05

Codeforces Round #512 (Div. 2, based on Technocup 2019 Elimination Round 1) C. Vasya and Golden Ticket的相关文章

[Codeforces Round #522 (Div. 2, based on Technocup 2019 Elimination Round 3)][C. Playing Piano]

http://codeforces.com/contest/1079/problem/C 题目大意:给出一个数列a[n],让构造一个满足下列条件的数列b[n]:如果a[i]>a[i-1]那么b[i]>b[i-1],如果a[i]<a[i-1]那么b[i]<b[i-1],如果a[i]==a[i-1],那么b[i]!=b[i-1].其中1<=b[i]<=5  1<=a[i]<=2*1e5. 题解:dp[i][j]表示在位置i处取j时是否成立,如果成立则dp[i][

Codeforces Round #602 (Div. 2, based on Technocup 2020 Elimination Round 3

A,有多个线段,求一条最短的线段长度,能过覆盖到所又线段,例如(2,4)和(5,6) 那么我们需要4 5连起来,长度为1,例如(2,10)(3,11),用(3,10) 思路:我们想一下如果题目说的是最长我们肯定是取最小x和最大的y连起来就完事. 但是要求长度最小又得覆盖,那么可以这样想,我们需要把最小的x不断右移到这条线段的y, 最大的左移到x,所以就是最大x-最小y完事 #include <bits/stdc++.h> using namespace std; #define ll long

【cf比赛记录】Codeforces Round #606 (Div. 2, based on Technocup 2020 Elimination Round 4)

比赛传送门 只能说当晚状态不佳吧,有点头疼感冒的症状.也跟脑子没转过来有关系,A题最后一步爆搜没能立即想出来,B题搜索没有用好STL,C题也因为前面两题弄崩了心态,最后,果然掉分了. A:简单数学 B:数学+排序 C:字符串搜索 A // https://codeforces.com/contest/1277/problem/A /* 题意: 给出一个数,求不大于该数的完美整数的个数(完美整数指全是一个数组成的数字,如:111, 333333, 444444, 9, 8888 ...) 分析:

【模拟】 Codeforces Round #434 (Div. 1, based on Technocup 2018 Elimination Round 1) C. Tests Renumeration

题意:有一堆数据,某些是样例数据(假设X个),某些是大数据(假设Y个),但这些数据文件的命名非常混乱.要你给它们一个一个地重命名,保证任意时刻没有重名文件的前提之下,使得样例数据命名为1~X,大数据命名为X+1~X+Y. 先把未使用的名字压进两个栈. 分为三轮:第一轮把占用了对方名字的样例数据以及占用了对方名字的大数据放进两个队列,然后不断反复尝试对这两个队列进行出队操作,每次将占用对方名字的改成一个未被使用的正确名字(从栈里取出),然后将占用的名字压进另一个栈.由于每个数据只会出队一次,所以是

cf 20190307 Codeforces Round #543 (Div. 2, based on Technocup 2019 Final Round)

B. Mike and Children time limit per test 2 seconds memory limit per test 256 megabytes input standard input output standard output Mike decided to teach programming to children in an elementary school. He knows that it is not an easy task to interest

Codeforces Round #591 (Div. 2, based on Technocup 2020 Elimination Round 1) 题解

A..B略 C 对当前的值排序,再二分答案,然后对于(i%x==0 && i%y==0)放入大的,再放其他的贪心解决即可. #include<iostream> #include<stdio.h> #include<string.h> #include<algorithm> #include<map> #define LL long long #define lson rt<<1 #define rson rt<

Codeforces Round #596 (Div. 2, based on Technocup 2020 Elimination Round 2)

B2 - TV Subscriptions (Hard Version) 遍历,维护一个set和set<pair<int,int>>即可 1 #include<bits/stdc++.h> 2 using namespace std; 3 typedef long long ll; 4 const int maxn = 1e6+7; 5 const ll mod = 1e9 + 9; 6 #define afdafafafdafaf y1; 7 int ar[maxn]

Codeforces Round #602 (Div. 2, based on Technocup 2020 Elimination Round 3) C. Messy 构造

C. Messy You are fed up with your messy room, so you decided to clean it up. Your room is a bracket sequence s=s1s2-sn of length n. Each character of this string is either an opening bracket '(' or a closing bracket ')'. In one operation you can choo

Codeforces Round #602 (Div. 2, based on Technocup 2020 Elimination Round 3) B. Box 贪心

B. Box Permutation p is a sequence of integers p=[p1,p2,-,pn], consisting of n distinct (unique) positive integers between 1 and n, inclusive. For example, the following sequences are permutations: [3,4,1,2], [1], [1,2]. The following sequences are n