CodeForces 1058C C. Vasya and Golden Ticket
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";
}
CodeForces 1058C C. Vasya and Golden Ticket的更多相关文章
- Codeforces Round #512 (Div. 2, based on Technocup 2019 Elimination Round 1) C. Vasya and Golden Ticket 【。。。】
任意门:http://codeforces.com/contest/1058/problem/C C. Vasya and Golden Ticket time limit per test 1 se ...
- 【codeforces1058】Vasya and Golden Ticket 枚举+暴力+模拟
#点击传送 题目描述 Recently Vasya found a golden ticket - a sequence which consists of nn digits a1a2-ana1a2 ...
- cf#512 C. Vasya and Golden Ticket
题目链接 http://codeforces.com/contest/1058/problem/C 这题还是暴力最方便,和的情况最多有n*a[i] 900种把每种都试一遍 #include<b ...
- Codeforces Round #585 (Div. 2) D. Ticket Game
链接: https://codeforces.com/contest/1215/problem/D 题意: Monocarp and Bicarp live in Berland, where eve ...
- codeforces 676C C. Vasya and String(二分)
题目链接: C. Vasya and String time limit per test 1 second memory limit per test 256 megabytes input sta ...
- Codeforces 1107 E - Vasya and Binary String
E - Vasya and Binary String 思路:区间dp + 记忆化搜索 转移方程看上一篇博客. 代码: #pragma GCC optimize(2) #pragma GCC opti ...
- Codeforces 1076 E - Vasya and a Tree
E - Vasya and a Tree 思路: dfs动态维护关于深度树状数组 返回时将当前节点的所有操作删除就能保证每次访问这个节点时只进行过根节点到当前节点这条路径上的操作 代码: #pragm ...
- Codeforces 1053 B - Vasya and Good Sequences
B - Vasya and Good Sequences 思路: 满足异或值为0的区间,必须满足一下条件: 1.区间中二进制1的个数和为偶数个; 2.区间二进制1的个数最大值的两倍不超过区间和. 如果 ...
- Codeforces 1058 D. Vasya and Triangle(分解因子)
题目:http://codeforces.com/contest/1058/problem/D 题意:有一个大小为N*M的矩阵内,构造一个三角形,使面积为(n*m)/k.若存在输出三个顶点(整数). ...
随机推荐
- javascript入门 之 ztree(四 自定义Icon)
<!DOCTYPE html> <HTML> <HEAD> <TITLE> ZTREE DEMO - Standard Data </TITLE& ...
- web系统安全运营之基础- 基于DFA算法的高性能的敏感词,脏词的检测过滤算法类(c#).
[概述]做好一个web系统的安全运维,除了常规的防注入,防入侵等,还有一个检测并过滤敏感词,脏词.. 这件事做得不好,轻则导致一场投诉或纠纷,重则导致产品被勒令关闭停运. 废话少说,先看下代码,可以 ...
- python3(十六) sorted
# sorted()函数list进行排序: L = sorted([36, 5, -12, 9, -21]) print(L) # [-21, -12, 5, 9, 36] # 可以看到默认是按照升序 ...
- hadoop(十)hdfs上传删除文件(完全分布式七)|12
集群测试 上传小文件到集群,随便选择一个小文件上传到hdfs的根目录 [shaozhiqi@hadoop102 hadoop-3.1.2]$ bin/hdfs dfs -put wcinput/wc. ...
- leetcode-0001 两数之和
题目地址:https://leetcode-cn.com/problems/two-sum/ 1.暴力解法 直接双重循环,枚举出所有可能的解,时间复杂度为O(n^2),空间复杂度为O(1) var t ...
- 熬夜整理出来的干货:Python+Pycharm+PyQT5可视化程序设计入门
前言 文的文字及图片来源于网络,仅供学习.交流使用,不具有任何商业用途,版权归原作者所有,如有问题请及时联系我们以作处理. 作者:朱淑强 PS:如有需要Python学习资料的小伙伴可以加点击下方链接自 ...
- Extended Traffic LightOJ - 1074 (经典SPFA问题)
题目大意:每一个城市都有一定的繁荣度,然后给出m条有向边i->j,定义这条边的权值为pow(arr[j]-arr[i],3),然后给你q个询问,每个询问输入一个x. 然后问你点1到x的距离,如果 ...
- C#开发BIMFACE系列39 网页集成开发3:审图系统中三维模型比对
系列目录 [已更新最新开发文章,点击查看详细] 在建筑施工图审查系统中,设计单位提交设计完成的模型/图纸,审查专家审查模型/图纸.审查过程中如果发现不符合规范的地方,则流程退回到设计单位,设计 ...
- jmeter5.1.1 生成html报告
1.首先需要准备好 .jmx 脚本 2.修改jmeter.properties文件(把注解去掉,报告中才能展示所需信息) jmeter.save.saveservice.output_format=x ...
- 深度剖析前端JavaScript中的原型(JS的对象原型)
这张图片有点劝退了,哈哈哈~ 通过原型机制,JavaScript 中的对象从其他对象继承功能特性:这种继承机制与经典的面向对象编程语言的继承机制不同.本文将探讨这些差别,解释原型链如 ...