前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >CodeForces 1058C C. Vasya and Golden Ticket

CodeForces 1058C C. Vasya and Golden Ticket

作者头像
风骨散人Chiam
发布2020-10-28 10:37:38
3270
发布2020-10-28 10:37:38
举报
文章被收录于专栏:CSDN旧文CSDN旧文

C. Vasya and Golden Ticket time limit per test1 second memory limit per test256 megabytes inputstandard input outputstandard output Recently Vasya found a golden ticket — a sequence which consists of n digits a1a2…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 350178 is lucky since it can be divided into three segments 350, 17 and 8: 3+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 n (2≤n≤100) — the number of digits in the ticket.

The second line contains n digits a1a2…an (0≤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 inputCopy 5 73452 outputCopy YES inputCopy 4 1248 outputCopy NO Note In the first example the ticket can be divided into 7, 34 and 52: 7=3+4=5+2.

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

这个题求插板之后分成的区间,能否使区间和相等,我第一时间是想2分,但是后来发现实现不了。我跟队友同时开题,我想到了他们的区间和一定是区间总和的因子,然后开始做。最后卡在了55组数,队友直接全部暴力枚举过了。可以研究我的代码,要是过了给我留个言。 不AC代码

代码语言:javascript
复制
#include<cstring>
#include<iostream>
#include<cmath>
#include<set>
#include<cstdio>
#include<algorithm>
using namespace std;
void fj(int a);
    int a[120],rq[1000],num;
    bool flag;
    char c;
    int sum1=0,w;
int main()
{
    rq[0]=1;
    int n,i,sum=0;
    cin>>n;
    for(i=1;i<=n;i++)
    {
        scanf(" %c",&c);
        a[i]=c-'0';
        sum=sum+a[i];
    }
    if(sum==0) {
        cout<<"YES"<<endl;
        return 0;
    }
    fj(sum);
    sort(rq,rq+num+1);
    //for(int k=0;k<=num;k++) cout<<rq[k]<<endl;
    for(int k=0;k<=num;k++)
    {
        sum1=0;
        flag=1;
        for(i=1;i<=n;i++)
        {
           sum1+=a[i];
           if(sum1>rq[k])
           {
               flag=0;
               break;
           }
           else if(sum1==rq[k])
           {
               sum1=0;
           }
        }
        if(sum1!=0) flag=0;
        if(flag==1) break;
    }
    if(flag==1) cout<<"YES"<<endl;
    else cout<<"NO"<<endl;
return 0;
}
void fj(int a)
{
    int k=1;
    for(int i=2;i<a;i++)
    {
        if(a%i==0) rq[k++]=i;
    }
    num=k-1;
}

AC代码

代码语言:javascript
复制
#include<bits/stdc++.h>
using namespace std;
char a[105];
int n,sum=0;
bool judge(){
    bool flag=0;
    for(int i=0;i<=sum/2;i++){
        int s=0;
        if(flag) break;
        for(int j=0;j<n;j++)
            {   s+=a[j]-'0';
                if((a[j]-'0')>i||s>i) break;

                if(s==i) {
                    flag=1;
                    s=0;

                }


            }
        if(s!=0) flag=0;
    }
    return flag;
}
int main(){
    cin>>n;
    scanf("%s",a);
    for(int i=0;i<n;i++)
            sum+=(a[i]-'0');
    if(judge()) cout<<"YES";
    else cout<<"NO";

    }
本文参与 腾讯云自媒体分享计划,分享自作者个人站点/博客。
原始发表:2019-05-23 ,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档