BestCoder Round #85 sum
大晚上的更一道下午的水题吧。(虽然WA了好多次= =,但真实情况是我比较水)
描述
Given a sequence, you're asked whether there exists a consecutive subsequence whose sum is divisible by m. output YES, otherwise output NO.
输入
The first line of the input has an integer T (1≤T≤10), which represents the number of test cases. For each test case, there are two lines: 1.The first line contains two positive integers n, m (1≤n≤100000, 1≤m≤5000). 2.The second line contains n positive integers x (1≤x≤100) according to the sequence.
输出
Output T lines, each line print a YES or NO.
样例输入
2
3 3
1 2 3
5 7
6 6 6 6 6
样例输出
YES
NO
代码如下:
#include <cstdio>
#define N 100100 int main()
{
int t,n,m,a[N];
int flag=;
scanf("%d",&t);
while(t--){
scanf("%d %d",&n,&m);
int sum=;
int k=;
for(int j=;j<=n;j++){
scanf("%d",&a[j]);
}
for(int i=;i<=n;i++){
while(k<=n&&sum<m){
sum+=a[k++];
}
if(sum==m){
flag=;
break;
}
sum-=a[i]; }
if(flag==){
printf("YES\n");
}else
printf("NO\n");
flag=;
}
return ;
}
题意:
题意:能不能找一个连续子区间的和是m的倍数。
思路总结:
简单尺取法。既然说找的是m的倍数,那就从头开始加起来。加到数小于m且加起来得到的和是最大的小于m的数。开始判断。如果失败就从头开始减一个,在接着从后加一个。一点点枚举。像尺子一样~~所以叫尺取法~~HOHO~
BestCoder Round #85 sum的更多相关文章
- BestCoder Round #85 hdu5776 sum
sum 题意: 问题描述 给定一个数列,求是否存在连续子列和为m的倍数,存在输出YES,否则输出NO 输入描述 输入文件的第一行有一个正整数T,表示数据组数. 接下去有T组数据,每组数据的第一行有两个 ...
- HDU 5776 sum (BestCoder Round #85 A) 简单前缀判断+水题
分析:就是判断简单的前缀有没有相同,注意下自身是m的倍数,以及vis[0]=true; #include <cstdio> #include <cstdlib> #includ ...
- BestCoder Round #85(ZOJ1569尚未验证)
A题 子序列和啊,就要想到前缀和的差.这个转换一定要!记着!那么i到j的一段子序列和Sij%m == 0就等价于(Sj-Si-1)%m == 0 了,那么什么意思呢?就是如果有两段前缀和%m的模是一 ...
- HDU5780 gcd (BestCoder Round #85 E) 欧拉函数预处理——分块优化
分析(官方题解): 一点感想: 首先上面那个等式成立,然后就是求枚举gcd算贡献就好了,枚举gcd当时赛场上写了一发O(nlogn)的反演,写完过了样例,想交发现结束了 吐槽自己手速慢,但是发了题解后 ...
- BestCoder Round #85
sum Accepts: 640 Submissions: 1744 Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 131072/13107 ...
- BestCoder Round #85 A B C
本来没有写博客的打算,可是看完了题解感觉这三道题这么水,我却只做出来一道,实在不应该,还是写点东西吧…… A.sum 问题描述 给定一个数列,求是否存在连续子列和为m的倍数,存在输出YES,否则输出N ...
- BestCoder Round #85 前三题题解
sum Accepts: 822 Submissions: 1744 Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 131072/13107 ...
- BestCoder Round #85 hdu5778 abs(素数筛+暴力)
abs 题意: 问题描述 给定一个数x,求正整数y,使得满足以下条件: 1.y-x的绝对值最小 2.y的质因数分解式中每个质因数均恰好出现2次. 输入描述 第一行输入一个整数T 每组数据有一行,一个整 ...
- BestCoder Round #85 hdu5777 domino
domino 题意: 问题描述 小白在玩一个游戏.桌子上有n张多米诺骨牌排成一列.它有k次机会,每次可以选一个还没有倒的骨牌,向左或者向右推倒.每个骨 牌倒下的时候,若碰到了未倒下的骨牌,可以把它推倒 ...
随机推荐
- objective-c中点语法的使用
点语法的作用:提供一种访问属性的setter和getter的机制. 用法举例: 1.创建一个Person类,并声明属性.在person.h中. 2.实现一个init方法,并使用点语法等.在pers.m ...
- (转)PQ分区魔术师中文版分区教程
PQ分区魔术师中文版分区的图解,图文并茂很多朋友提到硬盘分区,觉得不敢轻易去尝试,怕得不偿失,深度xp系统下载在此分享下pq分区的图解详见下图: 1)这是用的雨林木风系统的光盘,其他系统盘一样 2)首 ...
- HW4.27
public class Solution { public static void main(String[] args) { int count = 0; for(int i = 2001; i ...
- 开发小技巧:C#逐个输出字符
静态自定义方法: static int counter = 0; static string displayString = "This string will appear one let ...
- 使用.htaccess实现apache URL重定向
一.什么是URL重定向? URL重定向(URL redirection,或称网址重定向或网域名称转址),是指当使用者浏览某个网址时,将他导向到另一个网址的技术. 二.URL重定向怎么配置? 1)首先需 ...
- curl伪造ip
//随机IP function Rand_IP(){ $ip2id= round(rand(, ) / ); //第一种方法,直接生成 $ip3id= round(rand(, ) / ); $ip4 ...
- MySQL 优化Limit分页
很多时候.我们需要选择出从指定位置开始的指定行数.此时.limit笑了 对于limit的定义是: limit x,y 表示从第x行开始选择y条记录 在业务需要 ...
- ActiveForm
ActiveForm要和Model一起使用 我想在你的控制器的action中,至少应该这么写: /*action*/ $model = new Comments(); //实例化 Comments m ...
- spring 最全MAVEN 依赖引入配置
<properties> <spring.version>4.1.6.RELEASE</spring.version> </properties> &l ...
- Karma-Jasmine之安装与实例详解(一)
本文先介绍karma-jasmine的安装.接下来的(二)中主要通过实例利用断言库详解karma-jasmine如何进行单元测试. Karma介绍 首先说一下karma,官方是这样给出的,karma不 ...