首页
学习
活动
专区
圈层
工具
发布
社区首页 >专栏 >【HDU】5776 - sum(数论 & 思维)

【HDU】5776 - sum(数论 & 思维)

作者头像
FishWang
发布2025-08-27 09:43:19
发布2025-08-27 09:43:19
1270
举报

点击打开题目

sum

Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 131072/131072 K (Java/Others) Total Submission(s): 1034 Accepted Submission(s): 460

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 ( ), 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 ( , ). 2.The second line contains n positive integers x ( ) according to the sequence.

Output

Output T lines, each line print a YES or NO.

Sample Input

代码语言:javascript
复制
   2
3 3
1 2 3
5 7
6 6 6 6 6

Sample Output

代码语言:javascript
复制
   YES
NO

Source

BestCoder Round #85

题意:问是否有一个子序列的和是k的倍数。

题解:用一个sum数组存从1到当前的和并取余,如果某个余数出现两次或余数为 0 为真,那么输出YES,否则输出NO。

代码如下:

代码语言:javascript
复制
#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;
#define INF 0x3f3f3f3f
#define CLR(a,b) memset(a,b,sizeof(a))
int main()
{
    int u;
    int n,k;
    int ant[5000+11];
    int sum[100000+11];
    int t;
    scanf ("%d",&u);
    while (u--)
    {
        bool ans = false;
        scanf ("%d %d",&n,&k);
        sum[0] = 0;
        CLR(ant,0);
        for (int i = 1 ; i <= n ; i++)
        {
        	scanf ("%d",&t);
			sum[i] = (sum[i-1] + t) % k;
			ant[sum[i]]++;
			if (ant[sum[i]] >= 2)
				ans = true;
		}
		if (ant[0] >= 1)
			ans = true;
        printf (ans ? "YES\n" : "NO\n");
    }
    return 0;
}
本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
原始发表:2025-08-26,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • sum
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档