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

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

足夠 scan property ++ inter digits pos 通過 題意

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 a1a2ana1a2…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 (2n1002≤n≤100) — the number of digits in the ticket.

The second line contains nn digits a1a2ana1a2…an (0ai90≤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
5
73452
output Copy
YES
input Copy
4
1248
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;
}

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