E. Special Elements
题意
给定元素个数<=8000且元素大小<=8000的数组,问其中可表示为连续子序列的和的元素有多少个
题解
求出所有子序列和然后再匹配元素,和大于8000就不需要了
#include <iostream>
#include<cstring>
using namespace std;
int visit[8100]; //用桶数组来装所有的子序列和
int a[8100];
int b[8100];
int main() {
int t, n;
cin >> t;
while(t--) {
memset(visit, 0, sizeof(visit)); //数组清零
cin >> n;
for(int i = 1; i <= n; i++) {
cin >> a[i];
b[i] = b[i-1] + a[i]; // b[i]代表着前i项和
}
for(int i = 1; i < n; i++) {
for(int j = i + 1; j <= n; j++) {
int sum = b[j] - b[i-1]; //代表着i到j的子序列和
if(sum <= n)
visit[sum]++;//用桶数组装住
}
}
int ans = 0;
for(int i = 1; i <= n; i++) {
if(visit[a[i]])//如果有则答案数加一
ans++;
}
cout << ans << endl;
}
return 0;
}