Two coins of each kind (3)

Given a number xx and nn different coin values c1cnc_1 \ldots c_n, compute in how many ways it is possible to achieve change xx by using each value at most twice. Here, two coins with the same value are considered equal.

For example, if x=4x = 4 and the available values are 1 and 2, then there are two ways to achieve it: 1+1+21 + 1 + 2 and 2+22 + 2. As another example, if x=5x = 5 and the available values are 1, 2, 3, 4 and 5, then there are five ways: 1+1+31 + 1 + 3, 1+2+21 + 2 + 2, 1+41 + 4, 2+32 + 3 and 5.

Input

Input consists of several cases, with only integer numbers. Every case begins with xx and nn, followed by c1cnc_1 \ldots c_n. Assume 1n151 \le n \le 15, 1cix1061 \le c_i \le x \le 10^6, and that all cic_i are different.

Output

For every case print the number of different ways to achieve change exactly xx by using each value at most twice.

Hint

A simply pruned backtracking should be enough.

Problem information

Author: Unknown
Translator: Albert Atserias

Generation: 2026-01-25T22:55:02.912Z

© Jutge.org, 2006–2026.
https://jutge.org