Codeforces Round 640 (Div. 4)
E. Special Elements
题意:给定元素个数<=8000且元素大小<=8000的数组,问其中可表示为连续子序列的和的元素有多少个
思路:前缀和算出所有连续子序列的和,拿桶数组装着,注意和>8000的就不要装了,没必要并且会越界
#include<iostream>
#include<cstring>
#include<algorithm>
#include<cstring>
using namespace std;
int a[8000 + 10];
int b[8000 + 10];
int vis[8000+10];
int main()
{
int t;
cin >> t;
while (t--) {
for (int i = 0; i <= 8000 + 5; i++) vis[i] = 0;
int n;
cin >> n;
for (int i = 1; i <= n; i++) {
cin >> a[i];
b[i] = b[i - 1] + a[i];
}
for (int l = 1; l < n; l++) {
for (int r = l + 1; r <= n; r++) {
int sum = b[r] - b[l - 1];
if(sum<=n) vis[sum]++;
}
}
int ans = 0;
for (int i = 1; i <= n; i++) if (vis[a[i]]) ans++;
cout << ans << endl;
}
return 0;
}