HDU 5776 sum

来源:互联网 发布:dct水印算法 matlab 编辑:程序博客网 时间:2024/06/05 02:02


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 (1T10), 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 (1n1000001m5000).
2.The second line contains n positive integers x (1x100) according to the sequence.
 

Output
Output T lines, each line print a YES or NO.
 

Sample Input
23 31 2 35 76 6 6 6 6
 

Sample Output
YESNO
把前缀和对m取模,只要有相同的必然可以找到。
#include<set>#include<map>#include<cmath>#include<stack>#include<queue>#include<bitset>#include<cstdio>#include<string>#include<cstring>#include<iostream>#include<algorithm>#include<functional>#define rep(i,j,k) for (int i = j; i <= k; i++)#define per(i,j,k) for (int i = j; i >= k; i--)using namespace std;typedef long long LL;const int low(int x) { return x&-x; }const double eps = 1e-8;const int INF = 0x7FFFFFFF;const int mod = 1e9 + 7;const int N = 1e5 + 10;int T, n, m, x, y, s[N];void solve(){    rep(i, 0, m - 1)    {        if (s[i] > 1)        {            printf("YES\n");            return;        }    }    printf("NO\n");}int main(){    scanf("%d", &T);    while (T--)    {        scanf("%d%d", &n, &m);        memset(s, y = 0, sizeof(s));        s[0] = 1;        rep(i, 1, n)        {            scanf("%d", &x);            s[(y += x) %= m]++;        }        solve();    }    return 0;}

0 0
原创粉丝点击