码迷,mamicode.com
首页 > 其他好文 > 详细

BestCoder Round #85 sum

时间:2016-07-31 01:41:42      阅读:185      评论:0      收藏:0      [点我收藏+]

标签:

大晚上的更一道下午的水题吧。(虽然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~

BestCoder Round #85 sum

标签:

原文地址:http://www.cnblogs.com/xzt6/p/5722138.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!