Fork me on GitHub

BestCoder Round #85 sum

大晚上的更一道下午的水题吧。(虽然WA了好多次= =,但真实情况是我比较水)

描述

          Given a sequence, you're asked whether there exists a consecutive subsequence whose sum is divisible by m. output YES, otherwise output NO.

输入

           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 T lines, each line print a YES or NO.

样例输入

          

2
3 3
1 2 3
5 7
6 6 6 6 6

 

样例输出

 

YES
NO

 

代码如下:

 1 #include <cstdio>
 2 #define N 100100 
 3 
 4 int main()
 5 {
 6     int t,n,m,a[N];
 7     int flag=0;
 8     scanf("%d",&t);
 9     while(t--){
10         scanf("%d %d",&n,&m);
11         int sum=0;
12         int k=1;
13         for(int j=1;j<=n;j++){
14            scanf("%d",&a[j]);
15         }
16         for(int i=1;i<=n;i++){
17                while(k<=n&&sum<m){
18                   sum+=a[k++];
19                } 
20                if(sum==m){
21                      flag=1;
22                      break;
23                }
24                sum-=a[i];
25                
26         }
27         if(flag==1){
28             printf("YES\n");
29         }else
30             printf("NO\n");
31         flag=0;
32     }
33     return 0;
34 } 

 

 

 

题意:

            题意:能不能找一个连续子区间的和是m的倍数。

思路总结:

            简单尺取法。既然说找的是m的倍数,那就从头开始加起来。加到数小于m且加起来得到的和是最大的小于m的数。开始判断。如果失败就从头开始减一个,在接着从后加一个。一点点枚举。像尺子一样~~所以叫尺取法~~HOHO~

posted @ 2016-07-30 23:58  Jinkelababy  阅读(333)  评论(2编辑  收藏  举报