zoukankan      html  css  js  c++  java
  • 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;
    }
    

      

  • 相关阅读:
    02-Java 数组和排序算法
    Spring Security 入门
    mysql外键理解
    redis能否对set数据的每个member设置过期时间
    Redis sortedset实现元素自动过期
    mysql之触发器trigger
    一篇很棒的 MySQL 触发器学习教程
    mysql触发器
    云游戏
    mysql触发器个人实战
  • 原文地址:https://www.cnblogs.com/zhangchengc919/p/5722131.html
Copyright © 2011-2022 走看看