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.若存在输出三个顶点(整数). ...
随机推荐
- 痞子衡嵌入式:走进二维码(QR Code)的世界(2)- 初体验(PyQt5.11+MyQR2.3+ZXing+OpenCV4.2.0)
大家好,我是痞子衡,是正经搞技术的痞子.今天痞子衡给大家分享的是走进二维码(QR Code)的世界专题之初体验. 接上篇 <走进二维码(QR Code)的世界(1)- 引言> 继续更文,在 ...
- 中阶d01-- web前端 html css js bootstrap
html 页面骨架结构css 页面优化js(脚本语言) 页面和用户互动 bootstrap 前端框架,主要实现不同设备直接打开页面时播放比例设置(全屏暂时,不要滚动条)
- ConcurrentHashMap和 CopyOnWriteArrayList提供线程安全性和可伸缩性 以及 同步的集合类 Hashtable 和 Vector Collections.synchronizedMap 和 Collections.synchronizedList 区别缺点
ConcurrentHashMap和 CopyOnWriteArrayList提供线程安全性和可伸缩性 DougLea的 util.concurrent 包除了包含许多其他有用的并发构造块之外,还包含 ...
- PHP 5.6连接MySQL 8.0版本遇到的坑
一.数据库失败Warning: mysqli_connect(): The server requested authentication method unknown to t... <?ph ...
- AJ学IOS(26)UI之iOS抽屉效果小Demo
AJ分享,必须精品 先看效果 实现过程 第一步,把三个view设置好,还有颜色 #warning 第一步 - (void)addChildView { // left UIView *leftView ...
- AJ学IOS(12)UI之UITableView学习(上)LOL英雄联盟练习
AJ分享,必须精品 先看效果图 源代码 NYViewController的代码 #import "NYViewController.h" #import "NYHero. ...
- DPK
一.概念 dpk文件是Delphi的包文件,有dpk文件的组件安装比较方便.一般来说,支持不同版本Delphi的组件会有不同的dpk文件,一般以7结尾的dpk文件是支持Delphi 7的.如果没有支持 ...
- docker 概览 (1)
Docker Docker 是一个开源的应用容器引擎,让开发者可以打包他们的应用以及依赖包到一个可移植的镜像中,然后发布到任何流行的 Linux或Windows 机器上,也可以实现虚拟化.容器是完全使 ...
- L9循环神经网络进阶 ModernRNN
GRU RNN存在的问题:梯度较容易出现衰减或爆炸(BPTT) ⻔控循环神经⽹络:捕捉时间序列中时间步距离较⼤的依赖关系 RNN: Ht=ϕ(XtWxh+Ht−1Whh+bh) H_{t} = ϕ(X ...
- ELK(日志审计系统)
ELk简介及工作流程 ELK即(Elasticsearch + Logstash + Kibana) 下载安装包 系统环境:Contos7.0 Java环境:Portal(这是历史下载地址,我的是 j ...