Read problems statements in Mandarin Chinese, Russian and Vietnamese as well. 

Chef likes problems involving arrays. Unfortunately, the last one he tried to solve didn't quite get solved.

Chef has an array A of N positive numbers. He wants to find the number of subarrays for which the sum and product of elements are equal.

Please help Chef find this number.

------ Input ------ 

The first line of input contains an integer T denoting the number of test cases. T test cases follow. The first line of each test contains the integer N. The next line contains N integers — A_{1}, A_{2}, ..., A_{N} — denoting the array.

------ Output ------ 

For each test case, output a single line with the answer for the instance.

------ Constraints ------ 

$1 ≤ T ≤ 50$
$1 ≤ n ≤ 50$
$1 ≤ A_{i} ≤ 10^{9}$
$A_{1} * A_{2} * ... * A_{n} ≤ 10^{9}$

----- Sample Input 1 ------ 
3
3
1 3 2
4
4 1 2 1
6
1 2 2 2 2 1
----- Sample Output 1 ------ 
4
5
9
----- explanation 1 ------ 

Example case 1. There are 4 such subarrays: A[1..1], A[2..2], A[3..3], A[1..3]. Consider A[1..3], sum = 1 + 3 + 2 = 6, product = 1 * 3 * 2 = 6.

Source: codechef | Difficulty: EASY
Tags: Implementation

Complete search


Solutions:
a = int(input())
for _ in range(a):
	b = int(input())
	lis = list(map(int, input().split()))
	c = 0
	for i in range(b):
		p = 1
		s = 0
		for i in range(i, b):
			s = s + lis[i]
			p = p * lis[i]
			if s == p:
				c = c + 1
	print(c)


import math

def count_subarrays(n, arr):
	count = 0
	for i in range(n):
		prod = 1
		for j in range(i, n):
			prod *= arr[j]
			if prod == sum(arr[i:j + 1]):
				count += 1
	return count
t = int(input())
for _ in range(t):
	n = int(input())
	arr = list(map(int, input().split()))
	count = count_subarrays(n, arr)
	print(count)


for i in range(int(input())):
	cntr = 0
	n = int(input())
	nlst = list(map(int, input().split(' ')))
	for a in range(n):
		hue = nlst[a]
		hue2 = nlst[a]
		for b in range(a + 1, n):
			hue += nlst[b]
			hue2 *= nlst[b]
			if hue == hue2:
				cntr += 1
	print(cntr + n)


for _ in range(int(input())):
	n = int(input())
	a = list(map(int, input().split()))
	c = 0
	for i in range(len(a)):
		s = 0
		p = 1
		for j in range(i, -1, -1):
			s += a[j]
			p *= a[j]
			if s == p:
				c += 1
	print(c)


for _ in range(int(input())):
	n = int(input())
	c = 0
	a = [int(i) for i in input().split()]
	for i in range(n):
		(m, s) = (1, 0)
		for j in range(i, n):
			m *= a[j]
			s += a[j]
			if m == s:
				c += 1
	print(c)


for i in range(int(input())):
	b = int(input())
	l = list(map(int, input().split()))
	c = 0
	for j in range(b):
		p = 1
		s = 0
		for j in range(j, b):
			s = s + l[j]
			p = p * l[j]
			if s == p:
				c = c + 1
	print(c)


for _ in range(int(input())):
	n = int(input())
	arr = list(map(int, input().split()))
	new = []
	c = 0
	for i in range(len(arr) + 1):
		for j in range(i + 1, len(arr) + 1):
			new.append(arr[i:j])
	for x in new:
		s = 0
		p = 1
		for y in x:
			s += y
			p *= y
		if s == p:
			c += 1
	print(c)


t = int(input())
for i in range(t):
	a = int(input())
	l = list(map(int, input().split()))
	c = 0
	for i in range(a):
		d = 1
		e = 0
		for i in range(i, a):
			d = d * l[i]
			e = e + l[i]
			if d == e:
				c = c + 1
	print(c)


t = int(input())
for i in range(t):
	n = int(input())
	a = list(map(int, input().split()))
	ans = 0
	for i in range(n):
		s = 0
		p = 1
		for i in range(i, n):
			s = s + a[i]
			p = p * a[i]
			if s == p:
				ans = ans + 1
	print(ans)


t = 0
t = int(input())
while t > 0:
	t = t - 1
	n = int(input())
	arr = list(map(int, input().strip().split()))
	finans = 0
	i = 0
	while i < n:
		dans = 0
		prod = 1
		z = 0
		for j in arr:
			if z >= i:
				dans = dans + j
				prod = prod * j
				if dans == prod:
					finans = finans + 1
			z = z + 1
		i = i + 1
	print(finans)


a = int(input())
for _ in range(a):
	x = int(input())
	lis = list(map(int, input().split()))
	y = 0
	for i in range(x):
		p = 1
		q = 0
		for i in range(i, x):
			q = q + lis[i]
			p = p * lis[i]
			if q == p:
				y = y + 1
	print(y)


for i in range(int(input())):
	n = int(input())
	l = list(map(int, input().split()))
	ct = 0
	for i in range(n):
		s = 0
		prod = 1
		for i in range(i, n):
			s = s + l[i]
			prod = prod * l[i]
			if s == prod:
				ct = ct + 1
	print(ct)


def sub_lists(l):
	lists = [[]]
	for i in range(len(l) + 1):
		for j in range(i):
			lists.append(l[j:i])
	return lists

def product(arr):
	prod = 1
	for i in arr:
		prod *= i
	return prod
t = int(input())
for _ in range(t):
	n = int(input())
	arr = list(map(int, input().split()))
	lists = sub_lists(arr)
	count = 0
	for l in lists:
		list_sum = sum(l)
		list_product = product(l)
		if list_sum == list_product:
			count += 1
	print(count)


t = int(input())
while t:
	n = int(input())
	lis = list(map(int, input().split()))
	cnt = 0
	for i in range(n):
		product = 1
		sum = 0
		for i in range(i, n):
			sum = sum + lis[i]
			product = product * lis[i]
			if sum == product:
				cnt = cnt + 1
	print(cnt)
	t = t - 1


ank = int(input())
for _ in range(ank):
	bnk = int(input())
	lis = list(map(int, input().split()))
	cnk = 0
	for i in range(bnk):
		p = 1
		s = 0
		for i in range(i, bnk):
			s = s + lis[i]
			p = p * lis[i]
			if s == p:
				cnk = cnk + 1
	print(cnk)