Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>You can consider a table as list of values. Then, if you have N tables, your problem is to find lists of N integers (each one taken from one of the N tables) whose product is equal to a value p. You can solve the problem recursively:</p> <ul> <li>given a non-empty list of tables <code>{t1, t2, t3, ...}</code></li> <li>given a product value <code>p</code> that you look for</li> <li>for every value <code>v</code> in <code>t1</code> you must look for solutions of the sub-problem with a product value <code>p / v</code> and and tables <code>{t2, t3, ...}</code> (this assumes that <code>p % v == 0</code>, because we're dealing with integers</li> <li>etc.</li> </ul> <p>Some java code below:</p> <pre><code>public class SO6026472 { public static void main(String[] args) { // define individual tables Integer[] t1 = new Integer[] {2,-2,4,7}; Integer[] t2 = new Integer[] {3,-3,-8,0}; Integer[] t3 = new Integer[] {-1,-4,-7,6}; Integer[] t4 = new Integer[] {1,5}; // build list of tables List&lt;List&lt;Integer&gt;&gt; tables = new ArrayList&lt;List&lt;Integer&gt;&gt;(); tables.add(Arrays.asList(t1)); tables.add(Arrays.asList(t2)); tables.add(Arrays.asList(t3)); tables.add(Arrays.asList(t4)); // find solutions SO6026472 c = new SO6026472(); List&lt;List&lt;Integer&gt;&gt; solutions = c.find(36, tables); for (List&lt;Integer&gt; solution : solutions) { System.out.println( Arrays.toString(solution.toArray(new Integer[0]))); } } /** * Computes the ways of computing p as a product of elements taken from * every table in tables. * * @param p the target product value * @param tables the list of tables * @return the list of combinations of elements (one from each table) whose * product is equal to p */ public List&lt;List&lt;Integer&gt;&gt; find(int p, List&lt;List&lt;Integer&gt;&gt; tables) { List&lt;List&lt;Integer&gt;&gt; solutions = new ArrayList&lt;List&lt;Integer&gt;&gt;(); // if we have no tables, then we are done if (tables.size() == 0) return solutions; // if we have just one table, then we just have to check if it contains p if (tables.size() == 1) { if (tables.get(0).contains(p)) { List&lt;Integer&gt; solution = new ArrayList&lt;Integer&gt;(); solution.add(p); solutions.add(solution); return solutions; } else return solutions; } // if we have several tables, then we take the first table T, and for // every value v in T we search for (p / v) in the rest of the tables; // we do this only if p % v is equal to 0, because we're dealing with // ints List&lt;Integer&gt; table = tables.remove(0); for (Integer value : table) { if (value != 0 &amp;&amp; p % value == 0) { List&lt;List&lt;Integer&gt;&gt; subSolutions = find(p / value, tables); if (! subSolutions.isEmpty()) { for (List&lt;Integer&gt; subSolution : subSolutions) { subSolution.add(0, value); } solutions.addAll(subSolutions); } } } tables.add(0, table); return solutions; } } </code></pre> <p>The code prints solutions for a slightly modified version of your example:</p> <pre><code>[2, 3, 6, 1] [-2, -3, 6, 1] </code></pre> <p>The solutions work for any number of tables. There are ways to improve the algorithm, for example by using memoization and dynamic programming. But I think that the recursive solution is more clear.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload