hunnu11461—数组求和问题(前缀和)
2017-06-04 22:51
288 查看
题目链接:传送门
解题思路:这里如果间隔p只取1,想必问题变得相当简单,求前缀和rec
再判断rec[i]-rec[i-n]+data[i-n]是否等于s。现在p取任意数,依旧是求前缀和,只是数之间有个间隔p。
数组求和问题 |
Time Limit: 1000ms, Special Time Limit:2500ms, Memory Limit:32768KB |
Total submit users: 101, Accepted users: 90 |
Problem 11461 : No special judgement |
Problem description |
给定一个长度为n的数组,从这个数组里面随机从前往后取m个数,使得这m个数的和是S,并且这m个数每相邻的两个数的下标相差为p。 |
Input |
首先是一个整数t,表示有t组数据,每组数据首先是四个整数,n(1<=n<=100000),m(1<=m<=n),S(0<=S<=10^9), p(1<=p<=100000)意思如题目描述,接下来一行有n个非负数,都小于10^9,表示数组元素的值。 |
Output |
对于每组数据,首先输出”case #:”,#从一开始,接下来一个数就输出方案总数。输出格式详见样例。 |
Sample Input |
3 10 2 5 2 1 2 1 3 1 3 2 2 3 2 5 2 10 1 5 5 5 5 5 6 3 10 2 4 2 4 4 4 4 |
Sample Output |
case 1:3 case 2:4 case 3:1 |
再判断rec[i]-rec[i-n]+data[i-n]是否等于s。现在p取任意数,依旧是求前缀和,只是数之间有个间隔p。
#include <cstdio> #include <cstring> #include <cmath> #include <iostream> #include <queue> #include <set> #include <string> #include <stack> #include <algorithm> #include <map> using namespace std; typedef __int64 ll; const int N = 100080; const int M = 5007; const int INF = 0x3fffffff; const int mod = 1e9+7; const double Pi = acos(-1.0); const double sm = 1e-9; ll data ,rec ; int main() { int T,tot = 0; scanf("%d",&T); while( T-- ){ int n,m,p;ll s; scanf("%d%d%d%d",&n,&m,&s,&p); for( int i = 1 ; i <= n ; ++i ){ scanf("%I64d",&data[i]); } for( int i = 1 ; i <= p ; ++i ){ rec[i] = data[i]; } for( int i = 1 ; i+p <= n ; ++i ){ rec[i+p] = data[i+p]+rec[i]; } int ans = 0; for( int i = 1+m*p-p ; i <= n ; ++i ){ if( (rec[i]-rec[i-(m-1)*p]+data[i-(m-1)*p]) == s ) ++ans; } printf("case %d:%d\n",++tot,ans); } return 0; }
相关文章推荐
- 问题:如何用递归实现数组求和?
- 遇到的比较有意思的问题(1)去掉重复数字(2)提取数组的子数组,求和最大的子数组、最大的子数组和
- NOIP--组合数问题(数位DP+二维前缀和数组优化)
- 不定数组求和问题
- 数组求和问题
- 树状数组解决区间求和问题(模板)
- 树状数组求不带修改前缀最值问题 51NOD 1272 最大距离
- 数组求和问题
- 两个有序数组,从中各取一个组成pair,求和最小的前K个pair(杨氏矩阵top k问题)
- 软件工程结对开发——一维最大子数组求和溢出问题
- hdu1556区间问题(前缀和,树状数组,线段树)
- 2017.8.20------二维树状数组的求和问题
- 一个n数组求和问题
- 结对开发——数组求和大数溢出问题
- 树状数组关于区间修改区间求和的问题
- 组合数打表+二维前缀数组优化——组合数问题
- 结对开发-- 一维数组求和最大的子数组的大数溢出问题
- CUDA规约前缀求和问题
- 数组求和问题
- Uva 507 - Jill Rides Again(最大子数组求和问题)