Hdu5776 sum 抽屉原理+同余定理

Problem DescriptionGiven a sequence, you're asked whether there exists a consecutive subsequence whose sum is divisible by m. output YES, otherwise output NO InputThe first line of the input
摘要由CSDN通过智能技术生成

Problem Description
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 ( 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
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
Source
Recommend
wange2014

用前缀和去处理就好了,用 a[ i ] 去记录前 i 项之和对 m 的模。那么只要有两项是相同的,就存在所要求的连续串。

将 a[ 0 ]设为0可避免连续串从第一项开始的情况。

此外,由抽屉原理可得,n >= m 时一定存在。


#include <iostream>
#include <cstdio>
#include <map>
#include <set>
#include <vector>
#include <queue>
#include <stack>
#include <cmath>
#include <algorithm>
#include <cstring>
#include <string>
using namespace std;
#define INF 0x3f3f3f3f
typedef long long LL;

int main()
{
    int t;
    int n,m;
    int a[100005];
    scanf("%d",&t);
    while(t--){
        scanf("%d%d",&n,&m);
        int t;
        a[0]=0;
        for(int i=1;i<=n;i++){
            scanf("%d",&t);
            a[i]=(a[i-1]+t)%m;
        }
        if(n>=m){
            printf("YES\n");
        }else{
            sort(a+1,a+n+1);
            int flag=1;
            for(int i=1;i<=n;i++){
                if(a[i]==a[i-1]){
                    flag=0;
                    break;
                }
            }
            if(!flag){
                printf("YES\n");
            }else{
                printf("NO\n");
            }
        }
    }
    return 0;
}


  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 3
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值