题目链接:传送门
数组求和问题 |
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 |
解题思路:这里如果间隔p只取1,想必问题变得相当简单,求前缀和rec[n]
再判断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[N],rec[N];
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;
}