sum

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 131072/131072 K (Java/Others)
Total Submission(s): 1126    Accepted Submission(s): 494

Problem Description
Given
a sequence, you're asked whether there exists a consecutive subsequence
whose sum is divisible by m. output YES, otherwise output NO
 
Input
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
Output T lines, each line print a YES or NO.
 
Sample Input
2
3 3
1 2 3
5 7
6 6 6 6 6
 
Sample Output
YES
NO
 
Source

思路:当有两个前缀和相等,可以得到这段区间的和整除m;

   余m==0特判;

#include<bits/stdc++.h>
using namespace std;
#define ll long long
#define esp 0.00000000001
const int N=5e3+,M=1e6+,inf=1e9+,mod=;
int flag[N];
int main()
{
int x,y,z,i,t;
int T;
scanf("%d",&T);
while(T--)
{
memset(flag,,sizeof(flag));
scanf("%d%d",&x,&z);
int ans=,sum=;
for(i=;i<x;i++)
{
scanf("%d",&y);
sum+=y;
sum%=z;
if(flag[sum])
ans=;
flag[sum]=;
}
if(ans||flag[])
printf("YES\n");
else
printf("NO\n");
}
return ;
}

hdu 5776 sum 前缀和的更多相关文章

  1. HDU 5776 sum (前缀和)

    题意:给定 n 个数,和 m,问你是不是存在连续的数和是m的倍数. 析:考虑前缀和,如果有两个前缀和取模m相等,那么就是相等的,一定要注意,如果取模为0,就是真的,不要忘记了,我当时就没记得.... ...

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

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

  3. HDU 5776 sum (模拟)

    sum 题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=5776 Description Given a sequence, you're asked ...

  4. HDU 5776 sum (思维题)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5776 题目让你求是否有区间的和是m的倍数. 预处理前缀和,一旦有两个数模m的值相同,说明中间一部分连续 ...

  5. HDU 5776 sum(抽屉原理)

    题目传送:http://acm.hdu.edu.cn/showproblem.php?pid=5776 Problem Description Given a sequence, you're ask ...

  6. HDU 5776 sum

    猜了一下,发现对了.n>m是一定有解的.所以最多m*m暴力,一定能找到.而T较小,所以能过. #pragma comment(linker, "/STACK:1024000000,10 ...

  7. HDU 5776 sum( 鸽巢定理简单题 )

    链接:传送门 题意:给一个长为 n 的串,问是否有子串的和是 m 的倍数. 思路:典型鸽巢定理的应用,但是这里 n,m 的大小关系是不确定的,如果 n >= m 根据定理可以很简单的判定是一定有 ...

  8. HDOJ(HDU).1258 Sum It Up (DFS)

    HDOJ(HDU).1258 Sum It Up (DFS) [从零开始DFS(6)] 点我挑战题目 从零开始DFS HDOJ.1342 Lotto [从零开始DFS(0)] - DFS思想与框架/双 ...

  9. HDU 5776

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5776 求是否有区间的和是m的倍数 预处理前缀和,一旦有两个数模m的值相同,说明中间一部分连续子列可以组 ...

随机推荐

  1. [LintCode] 第k大元素

    基于快速排序: class Solution { public: /* * param k : description of k * param nums : description of array ...

  2. [Algorithms] Counting Sort

    Counting sort is a linear time sorting algorithm. It is used when all the numbers fall in a fixed ra ...

  3. delphi -----TListView的用法

    层次关系: TListView:           ->Columns:           ->Items  : -->TListItems:                   ...

  4. python将图片转base64,前端显示

    https://blog.csdn.net/u013055678/article/details/71406746 <img src='xxxxxxx'> 跟这样的效果是一样的吧?     ...

  5. 【opencv安裝】ubuntu16 opencv安装+测试

    ubuntu16.04 install opencv2.4 to python2 and c++ 四大主流库比较: 对OpenCV的印象:功能十分的强大,而且支持目前先进的图像处理技术,体系十分完善, ...

  6. django_视图层/2.0路由层/虚拟环境

  7. 解决linux下python多版本兼容问题?

    环境:CentOS 7(7下默认安装python2.7) Pyhon3在CentOS中需要依赖一些其他的包,我们一次性用yum安装一下: yum install zlib-devel bzip2-de ...

  8. 关于mysql主从复制的概述与分类

    一.概述: 按照MySQL的同步复制特点,大体上可以分为三种类别: 1.异步复制: 2.半同步复制: 3.完全同步的复制: -------------------------------------- ...

  9. Oracle Shared Pool 原理

    Oracle Shared Pool 原理 由于shared pool中最重要的是library cache,所以本文主要讲解Library cache的结构,library cache latch, ...

  10. Php DOMDocument 中的 formatOutput

    Nicely formats output with indentation and extra space 是否处理 缩进和多余的空白符