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.若存在输出三个顶点(整数). ...
随机推荐
- 基于 Spring Cloud 的微服务架构实践指南(下)
show me the code and talk to me,做的出来更要说的明白 本文源码,请点击learnSpringCloud 我是布尔bl,你的支持是我分享的动力! 一.引入 上回 基于 S ...
- C语言 刷新缓冲区
fflush int fflush(FILE* stream); 用于清空文件缓冲区,如果文件是以写的方式打开 的,则把缓冲区内容写入文件. eg: 1. #include <stdio.h&g ...
- python3(三十四)unitTest
""" 单元测试 """ __author__on__ = 'shaozhiqi 2019/9/23' class Dict(dict): ...
- tf.nn.sigmoid_cross_entropy_with_logits 分类
tf.nn.sigmoid_cross_entropy_with_logits(_sentinel=None,,labels=None,logits=None,name=None) logits和la ...
- shell 数组遍历加引号和不加引号的区别?
前言 shell 是一个比较神奇的国度,里面有太多的坑需要填,今天需要填的坑就是,数组遍历在使用时加了引号和不加引号的区别. 案例 解析: 不加引号,数组中元素间的“空格”就会编程换行符 加引号, ...
- 小波变换在matlab中的使用
对信号进行一层分解 clc; clear; % 获取噪声信号 load('matlab.mat'); sig = M(1,1:1400); SignalLength = length(sig); %使 ...
- 怎么在执行Python脚本时,密码等敏感信息也不让它出现
前言 文的文字及图片来源于网络,仅供学习.交流使用,不具有任何商业用途,版权归原作者所有,如有问题请及时联系我们以作处理. PS:如有需要Python学习资料的小伙伴可以加点击下方链接自行获取http ...
- Gun N' Rose Team Review
一看到这个项目就被他的功能给吸引了.回忆起以前看到一个东西很新奇想去网上查询它是什么,但是又不知道应该怎样去描述它,于是在搜索引擎的输入框中键入.删除.键入.删除的可笑经历的时候,我就越发感觉到这个a ...
- docker环境常用命令
Ubuntu 安装docker及docker-compose 安装: apt-get install docker apt-get install docker-compose 启动docker环境: ...
- Laravel 5.8 RCE 分析
原帖地址 : https://xz.aliyun.com/t/6059 Laravel 代码审计 环境搭建 composer create-project --prefer-dist laravel/ ...