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.若存在输出三个顶点(整数). ...
随机推荐
- Linux下修改efi启动项
Linux下有一个efibootmgr工具可以编辑efi启动项,十分方便,简单介绍如下 直接运行efibootmgr会显示出当前所有efi启动项,每个启动项前都有相应编号, 可以使用efibootmg ...
- python部署-Flask+uwsgi+Nginx
一.Flask部分(app.py) flask即Python代码:部分参考代码如下,相信很多人如果看到这篇文章一定有flask的代码能力. from app import create_app fro ...
- Docker 常用命令(.NET Core示例)
Docker安装 CentOS Docker 安装 安装 Docker Desktop for Mac.Docker Desktop for Windows 设置docker仓库镜像加速器 迁移Doc ...
- CSS 布局水平 & 垂直对齐
元素居中对齐 margin: auto; 文本居中对齐 text-align: center; 图片居中对齐 要让图片居中对齐, 可以使用 margin: auto; 并将它放到 块 元素中 左右对齐 ...
- 使用docker搭建selenium grid 分布式环境
本文章只做docker搭建selenium grid 分布式环境步骤说明,对于selenium grid中的参数.流程.原理等不做说明.selenium grid的详细情况可查看官方文档https:/ ...
- bat中的特殊字符,以及需要在bat中当做字符如何处理
bat中的特殊字符,以及需要在bat中当做字符如何处理 (2014-02-27 21:16:55) 转载▼ 标签: bat 特殊字符 分类: develop bat中的特殊字符,以及需要在bat中当做 ...
- Linux终端命令格式
01.终端命令格式 command [-options] [parameter] 说明: command:命令名,响应功能的英文单词或单词的缩写 [-options]:选项,可用来对命令进行控制,也可 ...
- d3.js v4曲线图的拖拽功能实现Zoom
zoom缩放案例 源码:https://github.com/HK-Kevin/d...:demo:https://hk-kevin.github.io/d3...: 原理:通过zoom事件来重新绘制 ...
- Python logging日志打印
1.logging常用函数Logger.setLevel():设置日志级别Logger.addHandler()和Logger.removeHandler():添加和删除一个handlerLogger ...
- 计算机视觉中的对象检测,Python用几段代码就能实现
目前计算机视觉(CV)与自然语言处理(NLP)及语音识别并列为人工智能三大热点方向,而计算机视觉中的对象检测(objectdetection)应用非常广泛,比如自动驾驶.视频监控.工业质检.医疗诊断等 ...