Two coins of each kind (1) P62113


Statement
 

pdf   zip

Given a natural 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 different.

For example, if x=4x = 4 and the available values are 11 and 22, then there are three ways to achieve it: 1+1+21 + 1' + 2, 1+1+21 + 1' + 2', and also 2+22 + 2'.

Input

Input consists of several cases. Every case begins with xx and nn, followed by c1cnc_1 \ldots c_n. Assume 1n201 \le n \le 20, 1cix10001 \le c_i \le x \le 1000, and that all cic_i are different.

Output

For every case print, in lexicographic order, all possible ways to exactly achieve change xx by using each value at most twice. Print every solution with its values sorted from small to big. In doing that, assume 1<1<2<2<1 < 1' < 2 < 2' < \ldots. Use “1p” to print 11', etcetera. Print a line with 10 dashes at the end of every case.

Hint

A simply pruned backtracking should be enough.

Public test cases
  • Input

    4 2  1 2
    400 1  200
    400 1  300
    5 3  4 2 1
    5 5  1 2 3 4 5
    

    Output

    4 = 1 + 1p + 2
    4 = 1 + 1p + 2p
    4 = 2 + 2p
    ----------
    400 = 200 + 200p
    ----------
    ----------
    5 = 1 + 2 + 2p
    5 = 1 + 4
    5 = 1 + 4p
    5 = 1p + 2 + 2p
    5 = 1p + 4
    5 = 1p + 4p
    ----------
    5 = 1 + 1p + 3
    5 = 1 + 1p + 3p
    5 = 1 + 2 + 2p
    5 = 1 + 4
    5 = 1 + 4p
    5 = 1p + 2 + 2p
    5 = 1p + 4
    5 = 1p + 4p
    5 = 2 + 3
    5 = 2 + 3p
    5 = 2p + 3
    5 = 2p + 3p
    5 = 5
    5 = 5p
    ----------
    
  • Information
    Author
    Salvador Roura
    Language
    English
    Translator
    Albert Atserias
    Original language
    Catalan
    Other languages
    Catalan
    Official solutions
    C++
    User solutions
    C++