hdu-5776 sum(同余)

题目链接:

sum

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 (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
 
2
3 3
1 2 3
5 7
6 6 6 6 6
 
Sample Output
 
YES
NO
 
题意:
 
问是否存在字串的和为m倍数;
 
思路:
 
sum[r]-sum[l]=k*m;sum[r]=sum[l](modm);
前缀和同余就存在;
 
AC代码:
 
/************************************************ 
┆  ┏┓   ┏┓ ┆    
┆┏┛┻━━━┛┻┓ ┆ 
┆┃       ┃ ┆ 
┆┃   ━   ┃ ┆ 
┆┃ ┳┛ ┗┳ ┃ ┆ 
┆┃       ┃ ┆  
┆┃   ┻   ┃ ┆ 
┆┗━┓    ┏━┛ ┆ 
┆  ┃    ┃  ┆       
┆  ┃    ┗━━━┓ ┆ 
┆  ┃  AC代马   ┣┓┆ 
┆  ┃           ┏┛┆ 
┆  ┗┓┓┏━┳┓┏┛ ┆ 
┆   ┃┫┫ ┃┫┫ ┆ 
┆   ┗┻┛ ┗┻┛ ┆       
************************************************ */  


#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <cmath>
#include <bits/stdc++.h>
#include <stack>

using namespace std;

#define For(i,j,n) for(int i=j;i<=n;i++)
#define mst(ss,b) memset(ss,b,sizeof(ss));

typedef  long long LL;

template<class T> void read(T&num) {
    char CH; bool F=false;
    for(CH=getchar();CH<'0'||CH>'9';F= CH=='-',CH=getchar());
    for(num=0;CH>='0'&&CH<='9';num=num*10+CH-'0',CH=getchar());
    F && (num=-num);
}
int stk[70], tp;
template<class T> inline void print(T p) {
    if(!p) { puts("0"); return; }
    while(p) stk[++ tp] = p%10, p/=10;
    while(tp) putchar(stk[tp--] + '0');
    putchar('
');
}

const LL mod=1e9+7;
const double PI=acos(-1.0);
const int inf=1e9;
const int N=1e5+10;
const int maxn=(1<<8);
const double eps=1e-8;

int vis[5600];

int main()
{       
        
        int t;
        read(t);
        while(t--)
        {
            mst(vis,0);
            int n,m,sum=0,flag=0,x;
            read(n);read(m);
            vis[0]=1;
            For(i,1,n)
            {
                read(x);
                sum+=x;
                int temp=sum%m;
                if(vis[temp])flag=1;
                vis[temp]=1;
            }
            if(flag)cout<<"YES
";
            else cout<<"NO
";
        }

        return 0;
}

  

原文地址:https://www.cnblogs.com/zhangchengc919/p/5722131.html