Counting Subsequences
InterviewTime limit2sMemory limit1024 MB
Count the contiguous subarrays of a sequence whose elements sum to exactly 47.
- Level
Easy3 of 10
- Topics
- Prefix sum, Hash map, Array
- Solved
- No attempts yet
Problem
"47 is the quintessential random number," states the 47 society. And there might be a grain of truth in that.
For example, the first ten digits of Euler's constant are 2 7 1 8 2 8 1 8 2 8.
And what is their sum? It is 47.
Try walking around with your eyes open. You will soon start noticing occurrences of the number 47 everywhere.
You are given a sequence of integers that we saw somewhere in nature. Your task is to compute how strongly this sequence supports the above claims.
We call a continuous subsequence of interesting if the sum of its terms is equal to 47. For example, consider the sequence . It has two interesting continuous subsequences: and .
Given a sequence , find the number of its interesting subsequences.
Input
The first line contains an integer , the number of test cases. Each test case is preceded by a blank line.
The first line of each test case contains the length of a sequence . The second line contains space-separated integers , the elements of the sequence.
Output
For each test case, output a single line containing one integer: the count of interesting subsequences of the given sequence.