题目传送:http://acm.hdu.edu.cn/showproblem.php?pid=5776

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
 
直接拿这道代码改了一下,两道题一个意思http://www.cnblogs.com/Annetree/p/7095096.html
 
 #include<cstdio>
#include<iostream>
#include<algorithm>
#include<cstring>
#include<string.h>
#include<cmath>
using namespace std;
#define N 100005 struct node
{
int num,r;
}k[N]; bool cmp(node a,node b)
{
if(a.r==b.r)
return a.num<b.num;
return a.r<b.r;
} int main()
{
long long sum,a;
int n,m;
bool flag;
int T;
scanf("%d",&T);
while(T--)
{
scanf("%d%d",&n,&m);
flag=false;
if(n>m)
flag=true;
sum=;
k[].num=;
for(int i=;i<=n;i++)
{
scanf("%lld",&a);
sum+=a;
k[i].r=sum%m;
k[i].num=i;
if(k[i].r==&&flag==false)
{
flag=true;
}
}
if(!flag)
{
sort(k+,k++n,cmp);
for(int i=;i<=n;i++)
{
if(k[i].r==k[i-].r)
{
flag=true;
break;
}
}
}
if(!flag)
printf("NO\n");
else
printf("YES\n");
}
return ;
}
 

HDU 5776 sum(抽屉原理)的更多相关文章

  1. Codeforces Round #319 (Div. 2) B. Modulo Sum 抽屉原理+01背包

    B. Modulo Sum time limit per test 2 seconds memory limit per test 256 megabytes input standard input ...

  2. HDU 5776 sum (模拟)

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

  3. HDU 5776 sum (思维题)

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

  4. HDU 5776 sum (前缀和)

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

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

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

  6. HDU 5776 sum

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

  7. HDU 4704 Sum (隔板原理 + 费马小定理)

    Sum Time Limit : 2000/1000ms (Java/Other)   Memory Limit : 131072/131072K (Java/Other) Total Submiss ...

  8. hdu 5776 sum 前缀和

    sum Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 131072/131072 K (Java/Others)Total Submi ...

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

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

随机推荐

  1. CF-413E-线段树

    http://codeforces.com/problemset/problem/413/E 给出一个2*N的格子图,每个格子要么是障碍要么是空地,M次询问(A,B)之间的最短距离. 采用分治的思想, ...

  2. ADG配置(主备库环境)

    @font-face { font-family: "Courier New"; }@font-face { font-family: "宋体"; }@font ...

  3. JS实现下拉单的二级联动

    因工作需要,做了一个下拉单的二级联动. 第一级是固定的选项,有A.B两个选项,第二级的选项随着第一级选项的变化而变化. 一开始是这样的: HTML代码 <html> <head> ...

  4. HttpClient4 警告: Invalid cookie header 的问题解决(转)

    原文地址:HttpClient4 警告: Invalid cookie header 的问题解决 最近使用HttpClient4的时候出现如下警告信息 org.apache.http.client.p ...

  5. time_wait 和 close_wait

    tcp 四次握手状态图: 使用以下命令统计 tcp 连接信息: netstat -n |awk '/^tcp/ {++S[$NF]} END {for (a in S) print a, S[a]}' ...

  6. view的clickable属性和点击background颜色改变

    drawable可以是color(color只能是color) android:background=drawable或者color 当一个view(iamge/text view都可以)的andro ...

  7. vs 编译库文件

    vs编译的库文件 静态库  debug和release版本 需要分开编译,我编译和实践的结果. 但是我也发现有的debug release都用同一个(搞不清楚). 然后添加到工程应用. 静态库  附件 ...

  8. 小程序使用wxParse解析html

    转载至:http://blog.csdn.net/tang05709/article/details/54924675 这个是需要循环出显示的https://github.com/tonyzhan/c ...

  9. win10系统office2010每次打开总是出现配置进度

  10. 转Generative Model 与 Discriminative Model

    没有完全看懂,以后再看,特别是hmm,CRF那里,以及生成模型产生的数据是序列还是一个值,hmm应该是序列,和图像的关系是什么. [摘要]    - 生成模型(Generative Model) :无 ...