Unacknowledged scientist Victor conducts a pseudoscientific research of the relation between integers that cross his mind and the integer that comes into his assistant’s mind. He wrote the integers a 1, …, a n which had crossed his mind. Then it turned up that the integer s had come into his assistant’s mind. Victor wants to determine how many consecutive non-empty sets of integers a l, a l + 1, …, a r ( l ≤ r) have the sum a l + a l + 1 + … + a r = s.
Input
The first line contains two integers separated by space: n and s (1 ≤ n ≤ 200000, - 2·1014 ≤ s ≤ 2·1014) — the number of integers which crossed Victor’s mind and the integer that came into his assistant’s mind.
The second line contains n integers separated by space: a i ( - 109 ≤ a i ≤ 109) — the integers which crossed Victor’s mind.
Output
Output the only integer — the number of consecutive non-empty sets of integers which have the sum s.
Examples
Input
5 2
-1 1 2 -1 1
Output
5
Input
6 3
3 -2 1 -1 1 2
Output
3
题意:给出n个数,求有多少个区间的和正好等于S。
int main()
{
sum[0] = 0;
mp.clear();
scanf("%d%lld",&n,&s);
for(int i=1;i<=n;i++)
{
scanf("%lld",&a[i]);
sum[i] = sum[i-1]+a[i];
mp[sum[i]]++;
}
ans = 0;
for(int i=1;i<=n;i++)
{
ans += mp[s+sum[i-1]];
mp[sum[i]]--;
}
printf("%lld\n",ans);
return 0;
}
区间和等于S的个数
本文探讨了一个算法问题,即在给定一系列整数的情况下,如何找出所有连续子序列的和等于特定目标值S的方法。通过使用前缀和与哈希映射,文章提供了一种高效解决方案。





