码迷,mamicode.com
首页 > 其他好文 > 详细

补写:Best Coder #85 1001 Sum(前缀和)

时间:2016-08-06 21:42:42      阅读:221      评论:0      收藏:0      [点我收藏+]

标签:

sum

Accepts: 640
Submissions: 1744
Time Limit: 2000/1000 MS (Java/Others)
Memory Limit: 131072/131072 K (Java/Others)
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≤101 \leq T \leq 101T10), 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≤1000001 \leq n \leq 1000001n100000, 1≤m≤50001 \leq m \leq 50001m5000). 2.The second line contains n positive integers x (1≤x≤1001 \leq x \leq 1001x100) 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
/*第一次来打BC,作为大一的大水B,水出一道,已经很高兴了*/
/*用前缀和来遍历每一个连续数列的和*/
#include<iostream> #include<stdio.h> #include<string.h> #define N 100010 using namespace std; long long a[N],sum[N]; int main() { //freopen("in.txt", "r", stdin); int t; int n,m; scanf("%d",&t); while(t--) { memset(sum,0,sizeof sum); memset(a,0,sizeof a); scanf("%d%d",&n,&m); for(int i=1;i<=n;i++) { scanf("%d",&a[i]); sum[i]=sum[i-1]+a[i]; } int flag=1; for(int i=1;i<=n;i++) { for(int j=i;j<=n;j++) { if((sum[j]-sum[i-1])%m==0) { puts("YES"); flag=0; break; } } if(!flag) break; } if(flag) printf("NO\n"); } return 0; } Close BestCoder Contest System 2.0 Copyright © 2014-2016 HDU ACM

 

补写:Best Coder #85 1001 Sum(前缀和)

标签:

原文地址:http://www.cnblogs.com/wuwangchuxin0924/p/5744838.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!