#P9769. Minimum Operations to Reach Target Number

    ID: 22915 Type: Default 1000ms 256MiB

Minimum Operations to Reach Target Number

Minimum Operations to Reach Target Number

JokerShaco has a number \(x = 0\), and he wants to change it to \(y\). He is given two sets: \(A\) and \(B\). Set \(A\) contains all positive integers from \(1\) to \(n\), i.e. \(A = \{1, 2, \dots, n\}\). Set \(B\) contains \(m\) integers provided in the input. In each operation, he may perform one of the following moves:

  • Choose an element \(a \in A\) and update \(x \leftarrow x + a\).
  • Choose an element \(b \in B\) and update \(x \leftarrow x \times b\).

The goal is to determine the minimum number of operations required to transform \(x\) from \(0\) to \(y\). If it is impossible, output \(-1\).

inputFormat

The first line contains three integers: \(y\), \(n\), and \(m\), where \(y\) is the target number, \(n\) defines the set \(A = \{1, 2, \dots, n\}\), and \(m\) is the number of elements in set \(B\). The second line contains \(m\) space-separated integers representing the elements of set \(B\>.

outputFormat

Output a single integer representing the minimum number of operations required such that the number \(x\) becomes \(y\). If it is impossible, output \(-1\).

sample

10 3 2
2 3
3