hdu5776 sum

来源:互联网 发布:阿里云域名过户流程 编辑:程序博客网 时间:2024/05/22 12:54
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 (1T101≤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 (1n1000001≤n≤1000001m50001≤m≤5000). 
2.The second line contains n positive integers x (1x1001≤x≤100) 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
YES

NO

题意:求前缀和和抽屉定理

代码:

#include<iostream>#include<cstring>using namespace std;int a[100005];int b[100005];int vis[100005];int main(){    int t;    cin>>t;    while(t--)    {        memset(a,0,sizeof(a));        memset(b,0,sizeof(b));        memset(vis,0,sizeof(vis));        int n,m;        cin>>n>>m;        for(int i = 0;i<n;i++)        {            cin>>a[i];            b[i] = b[i-1]+a[i];            vis[b[i]%m]++;        }        bool flag = false;        if(vis[0]!=0)            flag = true;        else        {            for(int i = 0;i<n;i++)            {                if(vis[i]>=2)                {                    flag = true;                    break;                }            }        }        if(flag)            cout<<"YES"<<endl;        else cout<<"NO"<<endl;    }}


原创粉丝点击