描述
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
题意
给你n个数字,再给你数字m问这n个数字中有没有一段连续的数字之和能被m整除,输出YES或者NO
#include<bits/stdc++.h>
using namespace std;
#define ll long long
int a[100005];
inline int read()
{
int xx=0,ff=1;
char ch;
ch=getchar();
while(ch>'9'||ch<'0')
{
if(ch=='-') ff=-1;
ch=getchar();
}
while(ch>='0'&&ch<='9')
{
xx=(xx<<3)+(xx<<1)+ch-'0';
ch=getchar();
}
return xx*ff;
}
int main(){
int n,x,y,z;
while(~scanf("%d",&n)){
while(n--){
int flag=0;
x=read(),y=read();
for(int i=0;i<x;i++){
z=read();
if(i!=0){
a[i]=a[i-1]+z;
}else a[i]=z;
}
for(int i=0;i<x;i++){
if(a[i]%y==0){
flag=1;
break;
}
for(int j=0;j<i;j++){
if((a[i]-a[j])%y==0){
flag=1;
break;
}
}
if(flag) break;
}
if(flag) printf("YES\n");
else printf("NO\n");
}
}
}