zoukankan      html  css  js  c++  java
  • Hdu 5776 sum

    sum

    Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 131072/131072 K (Java/Others)
    Total Submission(s): 3295    Accepted Submission(s): 1210


    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 (1n100000, 1m5000 ).
    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
     
    Source
     
    询问是否有一段区间和%m为 0
     
     1 /*
     2     前缀和取模 
     3     对于前缀和sum[1-i]%m==k
     4               sum[i-j]%m==k
     5     一定有sum[i+1-j]%m==0 
     6 */
     7 #include <cctype>
     8 #include <cstdio>
     9 #include <cstring>
    10 
    11 const int MAXN=100010;
    12 
    13 int T,n,m;
    14 
    15 int sum[MAXN],cur[MAXN];
    16 
    17 inline void read(int&x) {
    18     int f=1;register char c=getchar();
    19     for(x=0;!isdigit(c);c=='-'&&(f=-1),c=getchar());
    20     for(;isdigit(c);x=x*10+c-48,c=getchar());
    21     x=x*f;
    22 }
    23 
    24 int hh() {
    25     read(T);
    26     while(T--) {
    27         read(n);read(m);
    28         bool flag=false;
    29         sum[0]=0;
    30         memset(cur,0,sizeof cur);
    31         for(int i=1;i<=n;++i) {
    32             read(sum[i]);
    33               sum[i]=(sum[i]+sum[i-1])%m;
    34               ++cur[sum[i]];
    35               if(cur[sum[i]]>1||sum[i]==0) flag=true;
    36         }
    37         if(flag) printf("YES
    ");
    38         else printf("NO
    ");
    39     }
    40     return 0;
    41 }
    42 
    43 int sb=hh();
    44 int main(int argc,char**argv) {;}
    代码
  • 相关阅读:
    POJ 2585 Window Pains 拓扑排序
    hrbust 2069 萌萌哒十五酱的衣服~ stl
    CodeForces 785D Anton and School
    CodeForces 816C Karen and Game
    CodeForces 758C Unfair Poll 模拟
    CodeForces 746D Green and Black Tea 有坑
    CodeForces 811C Vladik and Memorable Trip dp
    栈 队列 (面向对象列表实现)
    员工信息表 信息检索(模糊查询)
    员工信息表 查询 周末写(很简单)
  • 原文地址:https://www.cnblogs.com/whistle13326/p/7517839.html
Copyright © 2011-2022 走看看