HDU5776 sum【前缀和+模除】
来源:互联网 发布:租用临时备案域名 编辑:程序博客网 时间:2024/05/22 11:46
sum
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 131072/131072 K (Java/Others)Total Submission(s): 2530 Accepted Submission(s): 966
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
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.
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 (
Output T lines, each line print a YES or NO.
Sample Input
23 31 2 35 76 6 6 6 6
YESNO
BestCoder Round #85
问题链接:HDU5776 sum。
题意简述:(略)
问题分析:
这是一个连续子段和能否被整除的问题。
连续子段和可以由前缀和的差得到,所有将问题转换为求前缀和的问题。
即使得到前缀和,求两个前缀和的差(子段和)其计算复杂度为O(n*n),也是比较难以接受的。
对所有前缀和模除m,得到其余数放在数组中,如果有两个以上相同的余数,说明子段和能够被m整除。
由于最大的m比较小(1<=m<=5000),所有时间复杂度上看还是比较快的。
原始数据和前缀和是没有必要存储的,只需要存储前缀和的余数的计数就够用了,可以省去许多存储。
一种特殊的情况是,如果有前缀和除以m余数为0的话,说明存在连续的子段能够被m整除。这种情形,程序中需要做特殊的处理。
程序说明:(略)
AC的C++语言程序如下:
/* HDU5776 sum */#include <iostream>#include <string.h>using namespace std;const int M = 5000;int prefixsum, remainder[M];int main(){ int t, n, m, a; cin >> t; while(t--) { cin >> n >> m; memset(remainder, 0, sizeof(remainder)); prefixsum = 0; remainder[0] = 1; for(int i=1; i<=n; i++) { cin >> a; prefixsum += a; prefixsum %= m; remainder[prefixsum]++; } bool flag = false; for(int i=0; i<m; i++) if(remainder[i] >= 2) { flag = true; break; } cout << (flag ? "YES" : "NO") << endl; } return 0;}
阅读全文
0 0
- HDU5776 sum【前缀和+模除】
- hdu5776 sum
- hdu5776 sum
- hdu5776——sum
- HDU 5776 sum(前缀和+取模)
- hdu5776
- hdu5776
- HDU 5776 (sum 前缀和)
- XTOJ1264 Partial Sum 【前缀和】
- Hdu5776 sum 抽屉原理+同余定理
- hdu5776——sum(抽屉原理)
- hdu5776 sum (鸽巢原理)
- hdu 5776 sum(前缀和取模)
- HDU 5776 sum (数组前缀和+取模)
- hdu 5776 Sum【前缀和相关】
- XTUOJ1264:Partial Sum(前缀和)
- 2017 CCPC Partial Sum 前缀和
- 523. Continuous Subarray Sum (前缀和)
- tieba.py
- 算法课第16周第1题—— 215. Kth Largest Element in an Array
- MY SQL中''与null的区别
- netty中的粘包和拆包问题处理
- 使用Mweb生成博客到csdn
- HDU5776 sum【前缀和+模除】
- 【Unity优化】Shader优化
- 38 WebGL针对单独的顶点坐标绘制组成模型
- Ubuntu14.04上安装ROS
- 后端框架_数据库技术mysql
- HashSet哈希值
- 字符数组、String类、StringBuffer三者的相互转换
- SpringMVC之浅析上下文初始化(二)
- MyFlag Step11 :menupath的移植工作