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 (1≤T≤101≤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≤1000001≤n≤100000, 1≤m≤50001≤m≤5000).
2.The second line contains n positive integers x (1≤x≤1001≤x≤100) 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
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <cmath>
#include <iostream>
#include <algorithm>
#include <map>
#include <queue>
#include <vector>
using namespace std;
typedef long long LL;
const int N = 1e5+10;
int sum[N],n,m,T;
bool vis[5005];
int main(){
scanf("%d",&T);
while(T--){
scanf("%d%d",&n,&m);
memset(vis,0,sizeof(vis));
bool flag=0;
vis[0]=true;
for(int i=1;i<=n;++i){
scanf("%d",&sum[i]);
if(sum[i]%m==0)flag=true;
sum[i]=(sum[i]+sum[i-1])%m;
if(vis[sum[i]])flag=true;
vis[sum[i]]=true;
}
if(flag)printf("YES\n");
else printf("NO\n");
}
return 0;
}
这篇博客介绍了一个编程问题,给定一个整数序列,判断是否存在连续子序列其和可以被特定整数m整除。通过输入样例展示了如何使用C++实现并判断输出YES或NO。

7632

被折叠的 条评论
为什么被折叠?



