sum(简单题)

该博客讨论了一道编程题目,要求判断给定序列中是否存在连续子序列的和可以被m整除。输入包括测试用例数量T,每个测试用例包含序列长度n、整数m及n个正整数x。输出对于每个测试用例是YES(存在符合条件的子序列)或NO(不存在)。样例输入和输出分别给出了两个测试用例的情况。
摘要由CSDN通过智能技术生成

描述

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");
        }
    }

}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值