大晚上的更一道下午的水题吧。(虽然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的更多相关文章

  1. BestCoder Round #85 hdu5776 sum

    sum 题意: 问题描述 给定一个数列,求是否存在连续子列和为m的倍数,存在输出YES,否则输出NO 输入描述 输入文件的第一行有一个正整数T,表示数据组数. 接下去有T组数据,每组数据的第一行有两个 ...

  2. HDU 5776 sum (BestCoder Round #85 A) 简单前缀判断+水题

    分析:就是判断简单的前缀有没有相同,注意下自身是m的倍数,以及vis[0]=true; #include <cstdio> #include <cstdlib> #includ ...

  3. BestCoder Round #85(ZOJ1569尚未验证)

    A题 子序列和啊,就要想到前缀和的差.这个转换一定要!记着!那么i到j的一段子序列和Sij%m ==  0就等价于(Sj-Si-1)%m == 0 了,那么什么意思呢?就是如果有两段前缀和%m的模是一 ...

  4. HDU5780 gcd (BestCoder Round #85 E) 欧拉函数预处理——分块优化

    分析(官方题解): 一点感想: 首先上面那个等式成立,然后就是求枚举gcd算贡献就好了,枚举gcd当时赛场上写了一发O(nlogn)的反演,写完过了样例,想交发现结束了 吐槽自己手速慢,但是发了题解后 ...

  5. BestCoder Round #85

    sum Accepts: 640 Submissions: 1744 Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 131072/13107 ...

  6. BestCoder Round #85 A B C

    本来没有写博客的打算,可是看完了题解感觉这三道题这么水,我却只做出来一道,实在不应该,还是写点东西吧…… A.sum 问题描述 给定一个数列,求是否存在连续子列和为m的倍数,存在输出YES,否则输出N ...

  7. BestCoder Round #85 前三题题解

    sum Accepts: 822 Submissions: 1744 Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 131072/13107 ...

  8. BestCoder Round #85 hdu5778 abs(素数筛+暴力)

    abs 题意: 问题描述 给定一个数x,求正整数y,使得满足以下条件: 1.y-x的绝对值最小 2.y的质因数分解式中每个质因数均恰好出现2次. 输入描述 第一行输入一个整数T 每组数据有一行,一个整 ...

  9. BestCoder Round #85 hdu5777 domino

    domino 题意: 问题描述 小白在玩一个游戏.桌子上有n张多米诺骨牌排成一列.它有k次机会,每次可以选一个还没有倒的骨牌,向左或者向右推倒.每个骨 牌倒下的时候,若碰到了未倒下的骨牌,可以把它推倒 ...

随机推荐

  1. 【原】现有市场上H264 IPCamerad的功能

    网络: 1.内置Web Server,通过IE实现远程监看.控制.设置等操作: 2.支持UPnP路由器,自动配置端口映射: 3.支持DDNS(动态域名解析).PPPoE拨号.DHCP网络协议: 4.支 ...

  2. MVC视图中ViewStart/RenderSection/Layout/Partial

    1.母板页_Layout.cshtml 2.部分视图 3.默认Layout引用的使用(_ViewStart.cshtml) Layout = "~/Views/Shared/_Layout. ...

  3. UVA 11624 Fire! (bfs)

    算法指南白书 分别求一次人和火到达各个点的最短时间 #include<cstdio> #include<cstring> #include<queue> #incl ...

  4. Linux I2C设备驱动编写(三)-实例分析AM3359

    TI-AM3359 I2C适配器实例分析 I2C Spec简述 特性: 兼容飞利浦I2C 2.1版本规格 支持标准模式(100K bits/s)和快速模式(400K bits/s) 多路接收.发送模式 ...

  5. light oj 1019【最短路模板】

    1019 - Brush (V) PDF (English) Statistics Forum Time Limit: 2 second(s) Memory Limit: 32 MB Tanvir r ...

  6. canvas 时钟

    <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...

  7. tomcat多域名配置

    1.找到你的tomcat然后在conf文件中找到server.xml打开: <Server port="8005" shutdown="SHUTDOWN" ...

  8. SQL SERVER 查询死锁

    USE mastergo CREATE PROCEDURE [dbo].[sp_who_lock]AS     BEGIN        DECLARE @spid INT ,             ...

  9. 使用Parse内付费服务出现的Error Domain=Parse Code=146 "The operation couldn’t be completed. (Parse error 146.)

    因为开发一个应用有个内付费去广告功能,介于苹果官方提供的方法没用过,感觉有些复杂,于是选用了第三方组件Parse来解决这个问题,简单易操作: Parse简化苹果官方内付费问题,使用方法分厂简单只有两个 ...

  10. 基于HBase0.98.13搭建HBase HA分布式集群

    在hadoop2.6.0分布式集群上搭建hbase ha分布式集群.搭建hadoop2.6.0分布式集群,请参考“基于hadoop2.6.0搭建5个节点的分布式集群”.下面我们开始啦 1.规划 1.主 ...