BestCoder Round #85 sum

时间:2021-02-09 03:51:53

大晚上的更一道下午的水题吧。(虽然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 (1≤T≤10), 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 (1≤n≤100000, 1≤m≤5000). 2.The second line contains n positive integers x (1≤x≤100) 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

代码如下:

 #include <cstdio>
#define N 100100 int main()
{
int t,n,m,a[N];
int flag=;
scanf("%d",&t);
while(t--){
scanf("%d %d",&n,&m);
int sum=;
int k=;
for(int j=;j<=n;j++){
scanf("%d",&a[j]);
}
for(int i=;i<=n;i++){
while(k<=n&&sum<m){
sum+=a[k++];
}
if(sum==m){
flag=;
break;
}
sum-=a[i]; }
if(flag==){
printf("YES\n");
}else
printf("NO\n");
flag=;
}
return ;
}

题意:

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

思路总结:

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