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代码
#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代码
#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"; } 风骨散人Chiam 认证博客专家 拖更专业户???? 大学僧,考研狗,没上岸,ACM退役选手。名字的含义:希望可以通过努力,能力让家人拥有富足的生活而不是为了生计而到处奔波。“世人慌慌张张,不过是图碎银几两。偏偏这碎银几两,能解世间惆怅,可让父母安康,可护幼子成长 …”Chiam是 -am爱 China中国文章主要内容:Python,C++,C语言,JAVA,C#等语言的教程,ACM题解、模板、算法等,主要是数据结构,数学和图论设计模式,数据库,计算机网络,操作系统,计算机组成原理,Python爬虫、深度学习、机器学学习,计算机系408考研的所有专业课内容。目前还在更新中,博客园,微信公众号同名“风骨散人”,关注公众号可获软件大礼包