problems / 194

Subset Product Matching

easy brute-force bitmask arrays 1000 ms · 256 MB

Subset Product Matching

Given an array of integers and a target product value, count the number of non-empty subsets of the array whose product equals the target.

Input

The first line contains n (1 ≤ n ≤ 15).

The second line contains n integers a_1, a_2, ..., a_n (-10^6 ≤ a_i ≤ 10^6).

The third line contains the target product t (-10^18 ≤ t ≤ 10^18).

Output

Print the number of non-empty subsets whose product equals t.

Notes

A subset is any non-empty collection of distinct array indices. The product of an empty subset is conventionally 1 (not counted here). Two subsets with different index sets are different, even if they produce the same elements.

Example 1

Input

3
2 3 4
6

Output

1

Example 2

Input

3
2 2 3
6

Output

2

Hints · reveal one at a time if you're stuck

Hint 1

With n at most 15 there are only 2^15 subsets, so checking every one is cheap. The danger is the arithmetic: 15 values near 10^6 multiplied together do not fit in 64 bits.

Hint 2

A wrapped overflow can equal t by accident. Since |t| is at most 10^18, a subset with no zero whose running product already exceeds 10^18 in magnitude can never reach t, because multiplying by nonzero integers never shrinks the magnitude. A zero makes the whole product 0, so treat zero-containing subsets separately.

report an issue Prefer your own editor? sooks prep 194 (CLI)
Sign in to run code

Run your code on the examples, or submit it against every test.