是否有任何“技巧”来加速非常大的背包组合类型概率的采样?

时间:2012-05-16 02:01:08

标签: java algorithm

更新:我已经意识到由于涉及大量数据(15k +项目),下面的问题无法以当前形式回答。我刚刚发现,小组我试图让它运行一个月然后终止它以使用结果(这就是为什么他们希望在更快的时间内获得更多结果)。这对我来说似乎很疯狂,因为他们只使用前几组数据(大型列表中的最后一项永远不会被使用)。因此,我正在修改此问题以获取预期输出的样本(解决方案的近似值不是完全解决方案)。在较短的时间内完成此操作的最佳方法是什么?他们似乎想要一个多样化的结果样本,是遗传算法工作还是某种采样技术?问题的其余部分保持不变(相同的输入/输出),但我现在没有寻找完整的解决方案集(因为它在一生中永远不会完成,但我希望各种解决方案的各种解决方案都可以)。 / em>


我的问题不仅仅是背包问题,而是非常接近。基本上,我试图找到等于特定值的X项的每个组合。我从我的一个朋友那里听说过这个问题,他在一个小型的学校研究实验室工作,这个过程已经运行,大约需要25天才能完成。看起来非常可怕,所以我提出了帮助(对我来说是好的,我可以学习并帮助一些非常好的人),所以我想出了如何通过多线程来扩展它(我将包括下面的代码)这减少了他们处理时间的几天,但我仍然不满意,所以我只是将我的代码移植到GPU上工作,但我感到不满意(尽管他们很高兴,因为它更快,我捐赠了我的旧显卡)因为我只是在利用硬件而不是任何算法。现在,我只是通过检查值是否等于总数来强制执行结果,如果确实如此,则保存结果,如果它没有继续处理它。

所以在这样的背景下,我能做些什么来加速算法?我的直觉告诉我不,因为他们需要每一个组合,从逻辑上看,计算机必须处理每个组合(数十亿),但我以前在这里看到过惊人的事情,即使是一个小的加速也可以在几天内发挥作用处理。

我喜欢超过10个版本的代码,但这里是一个使用多线程的Java版本(但是这个和gpu之间的逻辑几乎相同)。

基本逻辑:

for (int c = 100; c >= 0; c--) {
    if (c * x_k == current.sum) { //if result is correct then save
        solutions.add(new Context(0, 0, newcoeff));
        continue;
     } else if (current.k > 0) { //if result is not equal but not end of list then send to queue
         contexts.add(new Context(current.k - 1, current.sum - c * x_k, newcoeff));
     }
 }

完整代码:

import java.util.Arrays;
import java.util.ArrayDeque;
import java.util.Queue;
import java.util.concurrent.ConcurrentLinkedQueue;
import java.util.concurrent.LinkedBlockingDeque;
import java.util.concurrent.Callable;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;

public class MixedParallel
{
    // pre-requisite: sorted values !!
    private static final int[] data = new int[] { -5,10,20,30,35 };

    // Context to store intermediate computation or a solution
    static class Context {
        int k;
        int sum;
        int[] coeff;
        Context(int k, int sum, int[] coeff) {
            this.k = k;
            this.sum = sum;
            this.coeff = coeff;
        }
    }

    // Thread pool for parallel execution
    private static ExecutorService executor;
    // Queue to collect solutions
    private static Queue<Context> solutions;

    static {
        final int numberOfThreads = 2;
        executor =
            new ThreadPoolExecutor(numberOfThreads, numberOfThreads, 1000, TimeUnit.SECONDS,
                                   new LinkedBlockingDeque<Runnable>());
        // concurrent because of multi-threaded insertions
        solutions = new ConcurrentLinkedQueue<Context>();
    }


    public static void main(String[] args)
    {
        System.out.println("starting..");
        int target_sum = 100;
        // result vector, init to 0
        int[] coeff = new int[data.length];
        Arrays.fill(coeff, 0);
        mixedPartialSum(data.length - 1, target_sum, coeff);

        executor.shutdown();
        // System.out.println("Over. Dumping results");
        while(!solutions.isEmpty()) {
            Context s = solutions.poll();
            printResult(s.coeff);
        }
    }

    private static void printResult(int[] coeff) {
        StringBuffer sb = new StringBuffer();
        for (int i = coeff.length - 1; i >= 0; i--) {
            if (coeff[i] > 0) {
                sb.append(data[i]).append(" * ").append(coeff[i]).append(" + ");
            }
        }
        System.out.println(sb);
    }

    private static void mixedPartialSum(int k, int sum, int[] coeff) {
        int x_k = data[k];
        for (int c = 0; c <= 100; c++) {
            coeff[k] = c;
            int[] newcoeff = Arrays.copyOf(coeff, coeff.length);
            if (c * x_k == sum) {
                //printResult(newcoeff);
                solutions.add(new Context(0, 0, newcoeff));
                continue;
            } else if (k > 0) {
                if (data.length - k < 2) {
                    mixedPartialSum(k - 1, sum - c * x_k, newcoeff);
                    // for loop on "c" goes on with previous coeff content
                } else {
                    // no longer recursive. delegate to thread pool
                    executor.submit(new ComputePartialSum(new Context(k - 1, sum - c * x_k, newcoeff)));
                }
            }
        }
    }

    static class ComputePartialSum implements Callable<Void> {
        // queue with contexts to process
        private Queue<Context> contexts;

        ComputePartialSum(Context request) {
            contexts = new ArrayDeque<Context>();
            contexts.add(request);
        }

        public Void call() {
            while(!contexts.isEmpty()) {
                Context current = contexts.poll();
                int x_k = data[current.k];
                for (int c = 0; c <= 100; c++) {
                    current.coeff[current.k] = c;
                    int[] newcoeff = Arrays.copyOf(current.coeff, current.coeff.length);
                    if (c * x_k == current.sum) {
                        //printResult(newcoeff);
                        solutions.add(new Context(0, 0, newcoeff));
                        continue;
                    } else if (current.k > 0) {
                        contexts.add(new Context(current.k - 1, current.sum - c * x_k, newcoeff));
                    }
                }
            }
            return null;
        }
    }
}

以下是数据/方法的一些特征:

  • 所有数字都是短裤(没有数字似乎超过+/- 200的数值)
  • 有重复项(但没有零值)
  • for循环将系数限制为100(这是一个很难的数字和 告诉它不会改变)。这限制了结果
  • 项目数量有限但其变量由决定 我的朋友实验室。我一直在测试2对组合但我的 朋友告诉我他们使用30-35对(它不涉及的组合 整个数据集)。这也限制了失控的结果
  • 我的朋友提到他们所做的后期处理涉及删除包含少于30个系数或超过35的所有结果。在我当前的代码中,如果newcoeff变量超过一个数字(在这种情况下为35),我会中断但是也许有一种方法甚至不能处理低于30的结果。这可能是减少处理时间的一个重要领域。现在它们似乎生成了很多无用的数据来处理那些他们想要。
  • 他们的数据集是10k-15k项目(负面/正面)
  • 我只收到3个项目,两个列表(一个数据和一个ID号码) 识别数据)和目标总和。然后我保存了一个包含所有文件的文件 该列表中的数据组合。
  • 我愿意在这里提供帮助,因为这部分耗时最长, 在数据传到我之前,他们会对它做些什么(尽管他们这样做了 不自己生成数据)一旦我发送文件他们 将自己的逻辑应用于它并处理它。因此,我唯一的重点是 取3输入并生成输出文件。
  • 使用线程和GPU减少了在一个内部完成的问题 一周,但我在这里寻找的是改进算法的想法 所以我可以利用软件而不仅仅是硬件gpu 增速。从代码中可以看出,它只是蛮力 马上。理想情况下,我希望建议是可线程的。

Update2:我认为这个问题本身很简单/常见,但问题是大规模运行,所以这里是我们进行测试时得到的真实数据(它没有达到的那么大,但它的关于如果你想测试你不需要自己生成的3000件物品:

private static final int target_sum = 5 * 1000;
private static final List<Integer> data = Arrays.asList( -193, -138, -92, -80, -77, -70, -63, -61, -60, -56, -56, -55, -54, -54, -51, -50, -50, -50, -49, -49, -48, -46, -45, -44, -43, -43, -42, -42, -42, -42, -41, -41, -40, -40, -39, -38, -38, -38, -37, -37, -37, -37, -37, -36, -36, -36, -35, -34, -34, -34, -34, -34, -34, -34, -33, -33, -33, -32, -32, -32, -32, -32, -32, -32, -32, -31, -31, -31, -31, -31, -31, -31, -30, -30, -30, -30, -30, -29, -29, -29, -29, -29, -29, -29, -29, -29, -28, -28, -28, -28, -27, -27, -27, -27, -26, -26, -26, -26, -26, -26, -25, -25, -25, -25, -25, -25, -25, -25, -24, -24, -24, -24, -24, -24, -24, -24, -24, -24, -23, -23, -23, -23, -23, -23, -23, -23, -22, -22, -22, -22, -22, -22, -22, -22, -22, -21, -21, -21, -21, -21, -21, -21, -20, -20, -20, -20, -20, -20, -20, -19, -19, -19, -19, -19, -19, -19, -19, -19, -19, -19, -19, -19, -19, -18, -18, -18, -18, -18, -18, -18, -18, -18, -18, -18, -18, -18, -18, -18, -18, -18, -17, -17, -17, -17, -17, -17, -17, -17, -17, -17, -17, -17, -17, -17, -17, -17, -17, -16, -16, -16, -16, -16, -16, -16, -16, -16, -16, -16, -16, -16, -16, -16, -15, -15, -15, -15, -15, -15, -15, -15, -15, -15, -15, -15, -15, -15, -15, -15, -15, -15, -15, -15, -15, -15, -15, -15, -15, -15, -15, -15, -15, -15, -15, -14, -14, -14, -14, -14, -14, -14, -14, -14, -14, -14, -14, -14, -14, -14, -14, -14, -14, -14, -14, -13, -13, -13, -13, -13, -13, -13, -13, -13, -13, -13, -13, -13, -13, -13, -13, -13, -13, -13, -13, -13, -13, -13, -13, -13, -13, -13, -13, -13, -13, -13, -13, -13, -13, -12, -12, -12, -12, -12, -12, -12, -12, -12, -12, -12, -12, -12, -12, -12, -12, -12, -12, -12, -12, -12, -12, -12, -12, -12, -12, -12, -11, -11, -11, -11, -11, -11, -11, -11, -11, -11, -11, -11, -11, -11, -11, -11, -11, -11, -11, -11, -11, -11, -11, -11, -11, -11, -11, -11, -11, -11, -11, -11, -11, -10, -10, -10, -10, -10, -10, -10, -10, -10, -10, -10, -10, -10, -10, -10, -10, -10, -10, -10, -10, -10, -10, -10, -10, -10, -10, -10, -10, -10, -10, -10, -10, -10, -9, -9, -9, -9, -9, -9, -9, -9, -9, -9, -9, -9, -9, -9, -9, -9, -9, -9, -9, -9, -9, -9, -9, -9, -9, -9, -9, -9, -9, -9, -9, -9, -9, -9, -9, -9, -9, -9, -9, -9, -9, -9, -9, -9, -9, -9, -9, -9, -9, -9, -9, -9, -9, -9, -9, -9, -9, -9, -9, -8, -8, -8, -8, -8, -8, -8, -8, -8, -8, -8, -8, -8, -8, -8, -8, -8, -8, -8, -8, -8, -8, -8, -8, -8, -8, -8, -8, -8, -8, -8, -8, -8, -8, -8, -8, -8, -8, -8, -8, -8, -8, -8, -8, -8, -8, -8, -8, -8, -8, -7, -7, -7, -7, -7, -7, -7, -7, -7, -7, -7, -7, -7, -7, -7, -7, -7, -7, -7, -7, -7, -7, -7, -7, -7, -7, -7, -7, -7, -7, -7, -7, -7, -7, -7, -7, -7, -7, -7, -7, -7, -7, -7, -7, -7, -7, -7, -7, -7, -7, -7, -7, -7, -7, -7, -7, -7, -7, -7, -7, -7, -7, -7, -7, -7, -7, -7, -7, -7, -7, -7, -7, -6, -6, -6, -6, -6, -6, -6, -6, -6, -6, -6, -6, -6, -6, -6, -6, -6, -6, -6, -6, -6, -6, -6, -6, -6, -6, -6, -6, -6, -6, -6, -6, -6, -6, -6, -6, -6, -6, -6, -6, -6, -6, -6, -6, -6, -6, -6, -6, -6, -6, -6, -6, -6, -6, -6, -6, -6, -6, -6, -6, -6, -6, -6, -6, -6, -6, -6, -6, -6, -6, -6, -6, -6, -6, -6, -6, -6, -6, -6, -5, -5, -5, -5, -5, -5, -5, -5, -5, -5, -5, -5, -5, -5, -5, -5, -5, -5, -5, -5, -5, -5, -5, -5, -5, -5, -5, -5, -5, -5, -5, -5, -5, -5, -5, -5, -5, -5, -5, -5, -5, -5, -5, -5, -5, -5, -5, -5, -5, -5, -5, -5, -5, -5, -5, -5, -5, -5, -5, -5, -5, -5, -5, -5, -5, -5, -5, -5, -5, -5, -5, -5, -5, -5, -5, -5, -5, -5, -5, -5, -5, -5, -5, -5, -5, -5, -4, -4, -4, -4, -4, -4, -4, -4, -4, -4, -4, -4, -4, -4, -4, -4, -4, -4, -4, -4, -4, -4, -4, -4, -4, -4, -4, -4, -4, -4, -4, -4, -4, -4, -4, -4, -4, -4, -4, -4, -4, -4, -4, -4, -4, -4, -4, -4, -4, -4, -4, -4, -4, -4, -4, -4, -4, -4, -4, -4, -4, -4, -4, -4, -4, -4, -4, -4, -4, -4, -4, -4, -4, -4, -4, -4, -4, -4, -4, -4, -4, -4, -4, -4, -4, -4, -4, -4, -4, -4, -4, -4, -4, -4, -4, -4, -4, -4, -4, -4, -4, -4, -4, -4, -4, -4, -4, -4, -4, -4, -4, -4, -4, -4, -4, -4, -4, -4, -4, -4, -4, -3, -3, -3, -3, -3, -3, -3, -3, -3, -3, -3, -3, -3, -3, -3, -3, -3, -3, -3, -3, -3, -3, -3, -3, -3, -3, -3, -3, -3, -3, -3, -3, -3, -3, -3, -3, -3, -3, -3, -3, -3, -3, -3, -3, -3, -3, -3, -3, -3, -3, -3, -3, -3, -3, -3, -3, -3, -3, -3, -3, -3, -3, -3, -3, -3, -3, -3, -3, -3, -3, -3, -3, -3, -3, -3, -3, -3, -3, -3, -3, -3, -3, -3, -3, -3, -3, -3, -3, -3, -3, -3, -3, -3, -3, -3, -3, -3, -3, -3, -3, -3, -3, -3, -3, -3, -3, -3, -3, -3, -3, -3, -3, -3, -3, -3, -3, -3, -3, -3, -3, -3, -3, -3, -3, -3, -3, -3, -3, -3, -3, -3, -3, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1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, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 18, 18, 18, 18, 18, 18, 18, 18, 18, 18, 18, 18, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 20, 20, 20, 20, 20, 20, 20, 20, 21, 21, 21, 21, 21, 21, 21, 21, 21, 21, 21, 21, 21, 21, 21, 22, 22, 22, 22, 22, 22, 22, 22, 22, 22, 22, 22, 22, 23, 23, 23, 23, 23, 23, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 25, 25, 25, 25, 25, 25, 25, 25, 25, 25, 25, 25, 26, 26, 26, 26, 26, 26, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 28, 28, 28, 28, 28, 28, 29, 29, 29, 29, 30, 30, 30, 30, 30, 30, 30, 31, 31, 31, 31, 31, 32, 32, 32, 32, 33, 33, 33, 33, 34, 34, 34, 34, 34, 34, 34, 35, 35, 35, 35, 36, 36, 36, 36, 37, 37, 38, 39, 39, 39, 40, 41, 41, 41, 41, 41, 42, 42, 43, 43, 44, 45, 45, 46, 47, 47, 48, 48, 49, 49, 50, 54, 54, 54, 55, 55, 56, 56, 57, 57, 57, 57, 57, 58, 58, 58, 59, 60, 66, 67, 68, 70, 72, 73, 73, 84, 84, 86, 92, 98, 99, 105, 114, 118, 120, 121, 125, 156);

我正在学习编程和算法,所以如果我错过任何事情或某些事情没有意义,请告诉我。请注意我明白,这似乎是不可能的,因为大数据,但它不是(我已经看到它运行,它的变化已运行多年)。请不要让比例分散注意力,如果你使用10个变量,比我的10变量暴力快10%,那么我确信它在更大的数据上会更快。我不打算把灯吹灭,我正在寻找小改进(或更大的设计改进),提供更快的结果(我会研究每一个建议)。如果有任何需要放松的假设,请告诉我。

谢谢!

9 个答案:

答案 0 :(得分:8)

这使用动态编程来解决您在示例中给出的相同问题。它已被更新以通过跟踪值的索引而不是其值来处理重复值,并更正忽略某些解决方案的错误。

public class TurboAdder {
    private static final int[] data = new int[] { 5, 10, 20, 25, 40, 50 };

    private static class Node {
        public final int index;
        public final int count;
        public final Node prevInList;
        public final int prevSum;
        public Node(int index, int count, Node prevInList, int prevSum) {
            this.index = index;
            this.count = count;
            this.prevInList = prevInList;
            this.prevSum = prevSum;
        }
    }

    private static int target = 100;
    private static Node sums[] = new Node[target+1];

    // Only for use by printString.
    private static boolean forbiddenValues[] = new boolean[data.length];

    public static void printString(String prev, Node n) {
        if (n == null) {
            System.out.println(prev);
        } else {
            while (n != null) {
                int idx = n.index;
                // We prevent recursion on a value already seen.
                if (!forbiddenValues[idx]) {
                    forbiddenValues[idx] = true;
                    printString((prev == null ? "" : (prev+" + "))+data[idx]+"*"+n.count, sums[n.prevSum]);
                    forbiddenValues[idx] = false;
                }
                n = n.prevInList;
            }
        }
    }

    public static void main(String[] args) {
        for (int i = 0; i < data.length; i++) {
            int value = data[i];
            for (int count = 1, sum = value; count <= 100 && sum <= target; count++, sum += value) {
                for (int newsum = sum+1; newsum <= target; newsum++) {
                    if (sums[newsum - sum] != null) {
                        sums[newsum] = new Node(i, count, sums[newsum], newsum - sum);
                    }
                }
            }
            for (int count = 1, sum = value; count <= 100 && sum <= target; count++, sum += value) {
                sums[sum] = new Node(i, count, sums[sum], 0);
            }
        }
        printString(null, sums[target]);

    }
}

答案 1 :(得分:3)

您尝试解决的问题称为number partitioning。这是背包问题的一个特例。如果值都是整数并且您试图获得值M,那么您可以在O(n * M)时间内找到单个解。列举所有组合可能是指数级的,因为可能存在指数级的解决方案。

答案 2 :(得分:2)

你可能想检查“Dynamic Programming”的概念,动态编程主要是节省了大量的时间,不像正常的递归;因为它通过以2D数组的形式保存它来避免重新计算值,所以tutorial可能会帮助你

注意:背包问题被认为是动态编程的引入问题,搜索“背包动态编程”会对你有所帮助

答案 3 :(得分:2)

要通过动态编程解决这个问题,所有成本都需要是非负整数,并且只要您尝试实现的总成本,就需要一个数组 - 数组的每个元素都对应于代表成本的解决方案通过它在数组中的偏移量。由于您需要所有解决方案,因此数组的每个元素都应该是解决方案的最后组件的列表。您可以通过要求解决方案的最后一个组件的成本至少与解决方案的任何其他组件一样多来减小此列表的大小。

鉴于此,一旦填充了长度为N的数组,就可以通过考虑每100个多重性中的每个可能项来填充条目N + 1。对于每个此类项目,您从N + 1中减去(多次乘以成本)并看到为了获得总成本N + 1,您可以使用此项目加上任何成本N + 1-thisCost的解决方案。所以你看看数组 - 回到你已经填写的一个条目 - 看看是否有N + 1-thisCost的解决方案,如果有的话,以及当前成本*多样性是否至少与某个项目一样高在数组[N + 1-thisCost]中,您可以为项目添加一个条目,在偏移量N + 1处添加多重性。

一旦你将阵列扩展到你的目标成本,你可以从数组[finalCost]处理后备词,查看那里的答案并减去它们的成本,找出要查看的数组[finalCost - costOfAnswerHere]找到完整的解决方案。

此解决方案没有明显的并行版本,但有时动态编程的加速比足够好,可能仍然更快 - 在这种情况下,很大程度上取决于最终成本的大小。

这与普通的动态编程有点不同,因为你想要每个答案 - 希望它仍然会给你一些优势。想一想,最好在数组中简单地说一个可能/不可能的标志,说明是否存在该数组偏移的解决方案,然后在追溯时重复检查可能的组合。

答案 4 :(得分:2)

如果您不需要详尽而精确的解决方案,可以尝试近似解决问题。然后程序将以伪多项式或甚至多项式时间运行。

请参阅http://en.m.wikipedia.org/wiki/Knapsack_problem#Approximation_algorithms

答案 5 :(得分:2)

给出的样本数据中有137个唯一值(忽略重复)。

如果你承认通过调整系数,几乎可以将随机抽取数据的30个不同值的任何组合按摩到至少一个有效解,那么必须至少有C(137,30)= 1.54E30解正好30个条款(另有5.31E30,31个条款,1.76E31个32个条款,5.60E31个33个条款等)。

所以,如果你的目标只是一个有效解决方案的抽样,而不是一个不可能的详尽列表,那么我认为一个理想的方法是随机选择你的目标数量,然后调整它们的系数达到目标值生产单个样品,并重复所需数量的样品。

以下是应用此技术的程序。在我相当适中的笔记本电脑(1.3Ghz AMD E-300)上,我在2.249秒内制作了15000个独特的解决方案,每个样本32个术语。

import java.util.Arrays;
import java.util.HashMap;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Random;
import java.util.TreeMap;

public class ComboGen {

    private static final int[] data = { -193, -138, -92, -80, -77, -70, -63, -61, -60, -56, -56, -55, -54, -54, -51, -50, -50,
            -50, -49, -49, -48, -46, -45, -44, -43, -43, -42, -42, -42, -42, -41, -41, -40, -40, -39, -38, -38, -38, -37, -37,
            -37, -37, -37, -36, -36, -36, -35, -34, -34, -34, -34, -34, -34, -34, -33, -33, -33, -32, -32, -32, -32, -32, -32,
            -32, -32, -31, -31, -31, -31, -31, -31, -31, -30, -30, -30, -30, -30, -29, -29, -29, -29, -29, -29, -29, -29, -29,
            -28, -28, -28, -28, -27, -27, -27, -27, -26, -26, -26, -26, -26, -26, -25, -25, -25, -25, -25, -25, -25, -25, -24,
            -24, -24, -24, -24, -24, -24, -24, -24, -24, -23, -23, -23, -23, -23, -23, -23, -23, -22, -22, -22, -22, -22, -22,
            -22, -22, -22, -21, -21, -21, -21, -21, -21, -21, -20, -20, -20, -20, -20, -20, -20, -19, -19, -19, -19, -19, -19,
            -19, -19, -19, -19, -19, -19, -19, -19, -18, -18, -18, -18, -18, -18, -18, -18, -18, -18, -18, -18, -18, -18, -18,
            -18, -18, -17, -17, -17, -17, -17, -17, -17, -17, -17, -17, -17, -17, -17, -17, -17, -17, -17, -16, -16, -16, -16,
            -16, -16, -16, -16, -16, -16, -16, -16, -16, -16, -16, -15, -15, -15, -15, -15, -15, -15, -15, -15, -15, -15, -15,
            -15, -15, -15, -15, -15, -15, -15, -15, -15, -15, -15, -15, -15, -15, -15, -15, -15, -15, -15, -14, -14, -14, -14,
            -14, -14, -14, -14, -14, -14, -14, -14, -14, -14, -14, -14, -14, -14, -14, -14, -13, -13, -13, -13, -13, -13, -13,
            -13, -13, -13, -13, -13, -13, -13, -13, -13, -13, -13, -13, -13, -13, -13, -13, -13, -13, -13, -13, -13, -13, -13,
            -13, -13, -13, -13, -12, -12, -12, -12, -12, -12, -12, -12, -12, -12, -12, -12, -12, -12, -12, -12, -12, -12, -12,
            -12, -12, -12, -12, -12, -12, -12, -12, -11, -11, -11, -11, -11, -11, -11, -11, -11, -11, -11, -11, -11, -11, -11,
            -11, -11, -11, -11, -11, -11, -11, -11, -11, -11, -11, -11, -11, -11, -11, -11, -11, -11, -10, -10, -10, -10, -10,
            -10, -10, -10, -10, -10, -10, -10, -10, -10, -10, -10, -10, -10, -10, -10, -10, -10, -10, -10, -10, -10, -10, -10,
            -10, -10, -10, -10, -10, -9, -9, -9, -9, -9, -9, -9, -9, -9, -9, -9, -9, -9, -9, -9, -9, -9, -9, -9, -9, -9, -9, -9,
            -9, -9, -9, -9, -9, -9, -9, -9, -9, -9, -9, -9, -9, -9, -9, -9, -9, -9, -9, -9, -9, -9, -9, -9, -9, -9, -9, -9, -9,
            -9, -9, -9, -9, -9, -9, -9, -8, -8, -8, -8, -8, -8, -8, -8, -8, -8, -8, -8, -8, -8, -8, -8, -8, -8, -8, -8, -8, -8,
            -8, -8, -8, -8, -8, -8, -8, -8, -8, -8, -8, -8, -8, -8, -8, -8, -8, -8, -8, -8, -8, -8, -8, -8, -8, -8, -8, -8, -7,
            -7, -7, -7, -7, -7, -7, -7, -7, -7, -7, -7, -7, -7, -7, -7, -7, -7, -7, -7, -7, -7, -7, -7, -7, -7, -7, -7, -7, -7,
            -7, -7, -7, -7, -7, -7, -7, -7, -7, -7, -7, -7, -7, -7, -7, -7, -7, -7, -7, -7, -7, -7, -7, -7, -7, -7, -7, -7, -7,
            -7, -7, -7, -7, -7, -7, -7, -7, -7, -7, -7, -7, -7, -6, -6, -6, -6, -6, -6, -6, -6, -6, -6, -6, -6, -6, -6, -6, -6,
            -6, -6, -6, -6, -6, -6, -6, -6, -6, -6, -6, -6, -6, -6, -6, -6, -6, -6, -6, -6, -6, -6, -6, -6, -6, -6, -6, -6, -6,
            -6, -6, -6, -6, -6, -6, -6, -6, -6, -6, -6, -6, -6, -6, -6, -6, -6, -6, -6, -6, -6, -6, -6, -6, -6, -6, -6, -6, -6,
            -6, -6, -6, -6, -6, -5, -5, -5, -5, -5, -5, -5, -5, -5, -5, -5, -5, -5, -5, -5, -5, -5, -5, -5, -5, -5, -5, -5, -5,
            -5, -5, -5, -5, -5, -5, -5, -5, -5, -5, -5, -5, -5, -5, -5, -5, -5, -5, -5, -5, -5, -5, -5, -5, -5, -5, -5, -5, -5,
            -5, -5, -5, -5, -5, -5, -5, -5, -5, -5, -5, -5, -5, -5, -5, -5, -5, -5, -5, -5, -5, -5, -5, -5, -5, -5, -5, -5, -5,
            -5, -5, -5, -5, -4, -4, -4, -4, -4, -4, -4, -4, -4, -4, -4, -4, -4, -4, -4, -4, -4, -4, -4, -4, -4, -4, -4, -4, -4,
            -4, -4, -4, -4, -4, -4, -4, -4, -4, -4, -4, -4, -4, -4, -4, -4, -4, -4, -4, -4, -4, -4, -4, -4, -4, -4, -4, -4, -4,
            -4, -4, -4, -4, -4, -4, -4, -4, -4, -4, -4, -4, -4, -4, -4, -4, -4, -4, -4, -4, -4, -4, -4, -4, -4, -4, -4, -4, -4,
            -4, -4, -4, -4, -4, -4, -4, -4, -4, -4, -4, -4, -4, -4, -4, -4, -4, -4, -4, -4, -4, -4, -4, -4, -4, -4, -4, -4, -4,
            -4, -4, -4, -4, -4, -4, -4, -4, -4, -3, -3, -3, -3, -3, -3, -3, -3, -3, -3, -3, -3, -3, -3, -3, -3, -3, -3, -3, -3,
            -3, -3, -3, -3, -3, -3, -3, -3, -3, -3, -3, -3, -3, -3, -3, -3, -3, -3, -3, -3, -3, -3, -3, -3, -3, -3, -3, -3, -3,
            -3, -3, -3, -3, -3, -3, -3, -3, -3, -3, -3, -3, -3, -3, -3, -3, -3, -3, -3, -3, -3, -3, -3, -3, -3, -3, -3, -3, -3,
            -3, -3, -3, -3, -3, -3, -3, -3, -3, -3, -3, -3, -3, -3, -3, -3, -3, -3, -3, -3, -3, -3, -3, -3, -3, -3, -3, -3, -3,
            -3, -3, -3, -3, -3, -3, -3, -3, -3, -3, -3, -3, -3, -3, -3, -3, -3, -3, -3, -3, -3, -3, -3, -3, -3, -2, -2, -2, -2,
            -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2,
            -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2,
            -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2,
            -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2,
            -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -1, -1, -1, -1, -1, -1,
            -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
            -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
            -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
            -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
            -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, 1, 1, 1, 1, 1, 1,
            1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
            1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
            1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
            1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 2, 2, 2, 2,
            2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
            2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,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 8, 8,
            8, 8, 8, 8, 8, 8, 8, 8, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9,
            9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 10, 10, 10, 10, 10, 10, 10,
            10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10,
            10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11,
            11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 12, 12, 12, 12, 12, 12, 12, 12,
            12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 13, 13, 13, 13, 13, 13, 13, 13, 13,
            13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14,
            14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15,
            15, 15, 15, 15, 15, 15, 15, 15, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16,
            16, 16, 16, 16, 16, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 18, 18, 18, 18, 18, 18, 18, 18, 18, 18,
            18, 18, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 20, 20, 20, 20, 20, 20, 20, 20, 21, 21, 21, 21, 21, 21,
            21, 21, 21, 21, 21, 21, 21, 21, 21, 22, 22, 22, 22, 22, 22, 22, 22, 22, 22, 22, 22, 22, 23, 23, 23, 23, 23, 23, 24,
            24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 25, 25, 25, 25, 25, 25, 25, 25, 25, 25, 25, 25, 26, 26,
            26, 26, 26, 26, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 28, 28, 28, 28, 28, 28, 29, 29, 29, 29, 30, 30, 30, 30, 30,
            30, 30, 31, 31, 31, 31, 31, 32, 32, 32, 32, 33, 33, 33, 33, 34, 34, 34, 34, 34, 34, 34, 35, 35, 35, 35, 36, 36, 36,
            36, 37, 37, 38, 39, 39, 39, 40, 41, 41, 41, 41, 41, 42, 42, 43, 43, 44, 45, 45, 46, 47, 47, 48, 48, 49, 49, 50, 54,
            54, 54, 55, 55, 56, 56, 57, 57, 57, 57, 57, 58, 58, 58, 59, 60, 66, 67, 68, 70, 72, 73, 73, 84, 84, 86, 92, 98, 99,
            105, 114, 118, 120, 121, 125, 156 };

    private static Map<Integer, Integer> buildCounts(int[] rawData) {
        Map<Integer, Integer> buckets = new TreeMap<Integer, Integer>();
        int i = 0;
        for (i = 0; i < rawData.length; i++) {
            if (buckets.containsKey(rawData[i])) {
                buckets.put(rawData[i], buckets.get(rawData[i]) + 1);
            } else {
                buckets.put(rawData[i], 1);
            }
        }
        weights = new int[buckets.size()];
        counts = new int[buckets.size()];
        i = 0;
        for (Entry<Integer, Integer> entry : buckets.entrySet()) {
            weights[i] = entry.getKey();
            counts[i] = entry.getValue();
            i++;
        }
        return buckets;
    }

    private static int[] weights;
    private static int[] counts;
    private static Random random = new Random(System.nanoTime());

    private static int[] placeChips(int[] chips, int targetPairs) {
        int unplaced = targetPairs;
        int[] placements = new int[unplaced];
        Arrays.fill(chips, 0);
        if (unplaced > chips.length) {
            throw new IllegalStateException("Coefficient pairs must not exceed unique data values.");
        }
        while (unplaced > 0) {
            int idx = random.nextInt(counts.length);
            if (chips[idx] == 0) {
                chips[idx] = 1 + random.nextInt(100);
                unplaced--;
            }
        }
        int ppos = 0;
        for (int cpos = 0; cpos < chips.length; cpos++) {
            if (chips[cpos] > 0) {
                placements[ppos++] = cpos;
            }
        }
        return placements;
    }

    static int sum(int[] chips) {
        int sum = 0;
        for (int i = 0; i < chips.length; i++) {
            sum += weights[i] * chips[i];
        }
        return sum;
    }

    public static void adjustFactors(int[] chips, int[] placements, int target) {
        int sum = sum(chips);
        Map<Integer, Integer> weightIdx = new HashMap<Integer, Integer>();
        for (int placement : placements) {
            weightIdx.put(weights[placement], placement);
        }
        while (sum != target) {
            // System.out.print(sum + ",");
            int idx = 0;
            if ((sum > target) && weightIdx.containsKey(sum - target) && (chips[weightIdx.get(sum - target)] > 1)) {
                idx = weightIdx.get(sum - target);
            } else if ((sum < target) && weightIdx.containsKey(sum - target) && (chips[weightIdx.get(sum - target)] > 1)) {
                idx = weightIdx.get(sum - target);
            } else if ((sum > target) && weightIdx.containsKey(target - sum) && chips[weightIdx.get(target - sum)] < 100) {
                idx = weightIdx.get(target - sum);
            } else if ((sum < target) && weightIdx.containsKey(target - sum) && chips[weightIdx.get(target - sum)] < 100) {
                idx = weightIdx.get(target - sum);
            } else {
                idx = placements[random.nextInt(placements.length)];
            }
            int weight = weights[idx];
            if (sum < target) {
                if (weight > 0 && chips[idx] < 100) {
                    chips[idx]++;
                    sum += weight;
                } else if (weight < 0 && chips[idx] > 1) {
                    chips[idx]--;
                    sum -= weight;
                }
            } else {
                if (weight > 0 && chips[idx] > 1) {
                    chips[idx]--;
                    sum -= weight;
                } else if (weight < 0 && chips[idx] < 100) {
                    chips[idx]++;
                    sum += weight;
                }
            }
        }
    }

    private static String oneRandomSet(int targetSum, int targetPairs) {
        int[] chips = new int[counts.length];
        int[] placements = placeChips(chips, targetPairs);
        adjustFactors(chips, placements, targetSum);
        int sum = sum(chips);
        StringBuffer sb = new StringBuffer();
        for (int placement : placements) {
            sb.append(weights[placement]);
            sb.append(" * ");
            sb.append(chips[placement]);
            sb.append(" + ");
        }
        sb.setLength(sb.length() - 2);
        sb.append(" = ");
        sb.append(sum);
        sb.append("\n");
        return sb.toString();
    }

    public static void main(String[] ARGV) {
        int targetSum = 5000;
        int targetPairs = 32;
        int targetResults = 15000; // Produce this many solutions
        buildCounts(data);
        StringBuffer sb = new StringBuffer();
        long timer = System.nanoTime();
        for (int i = 0; i < targetResults; i++) {
            sb.append(oneRandomSet(targetSum, targetPairs));
        }
        double seconds = (System.nanoTime() - timer) / 1000000000d;
        double millisPerSol = 1000 * seconds / targetResults;
        System.out.println(sb.toString());
        System.out.println(String.format("%d solutions in %1.3f seconds @ %1.3f millis per sol", targetResults, seconds,
                millisPerSol));
    }

}

答案 6 :(得分:1)

编辑:我保留这个答案(至少现在)保留评论帖子

好的,我很困惑,但我会发布我的想法,如果我错了,可以稍后编辑/删除它们。如果我真的很不合适,你可以这样说,我会删除整个答案。

首先,在我看来,由于零是一个有效的数据值而零在所有位置都有效,因此您在计算所有这些组合时遇到了额外的麻烦。更糟糕的是,在我看来,你的算法有一个实际的错误,因为它会错过一些组合,其中列表开头的项目组合总和为零,因为一旦找到项目的组合,你就会终止调查线程产生目标总和的列表的结尾。

接下来,在我看来,对于列表中的每个项目,您正在尝试100个(实际上是101个)不同的值:x * 100,x * 99,...,x * 0。如果我是对的,那么问题空间的大小是100 ^ n,其中n是数据元素的数量。没有可能的方法来检查n = 100,更不用说n = 10,000。你的程序甚至可以终止的唯一方法是你在列表末尾找到总和并终止那些调查线程。 (哦,对了,现在你告诉我,当非零系数的元素数超过3,没有50,没有60,没有一些变量数时,终止线程。问题空间仍然太大。)

事实上,根据我的统计,你的测试数据有283个零。因此,您可以将这些数据元素的时间[1,100]的100 ^ 283组合添加到您获得的任何其他答案中。鉴于宇宙中的粒子数量估计为10 ^ 80,100 ^ 283组合将无法在纸上打印。

<击>

或者我出错了。如果是这样,请告诉我。

答案 7 :(得分:1)

您的代码与您的问题陈述不符,因此不清楚如何继续。

您说数据列表包含负值并包含重复项。你举一个两个都做的例子。实际上,这些值仅限于[-200,200]范围内的非零整数,但数据列表至少为2,000,通常为10,000或更多,因此必须重复。

让我们回顾一下你的“基本逻辑”:

for (int c = 100; c >= 0; c--) {
    if (c * x_k == current.sum) { //if result is correct then save
        solutions.add(new Context(0, 0, newcoeff));
        continue;
     } else if (current.k > 0) { // recurse with next data element
         contexts.add(new Context(current.k - 1, current.sum - c * x_k, newcoeff));
     }
}

在其他地方,您声明数据必须按数字顺序排序,并且您从列表的尾部开始,k = n -1(因为零索引),所以首先从最大的开始。 then子句终止递归。虽然这对你正在解决的问题可能没问题,但这并不是你所描述的问题,因为它忽略了总和为零的较小数据值的所有组合。

另一方面,将包括总和为零的更大值的所有组合。

例如,让我们看一下示例列表中的最后一项156,目标总和为5000.

156 * 100 = 15600因此在您进入负数之前它将与目标总和不匹配。当然

(100 * -100) + (100 * -6) + (100 * 156) = 5000

这个组合有效。 (你的样本数据集不包括-100,但它确实有两个-40和一个-20,所以如果你想对数据集合并而不是组合它们。我使用-100来保持示例简单因为你说数据集可能包含-100。)

但当然

(100 * -100) + (100 * -6) + (c * -1) + (c * 1) + (100 * 156) = 5000 

对于任何c,所以在输出中你将有100个这样的组合(1&lt; = c&lt; = 100)。但是你的数据集中有50个。当你达到100 * 50 = 5000时,你终止了递归,所以你永远不会得到

(c * -1) + (c * 1) + (100 * 50) = 5000 

因此,您的代码或问题陈述都是错误的。可能两者,因为即使不考虑系数,一次取60个项目的10,000个项目也会产生大约10 ^ 158个组合,但除了提前终止递归之外,我认为没有什么可以阻止你不得不测试其值所有这些组合的总和,即使计算价值的成本为零,也无法做那么多的比较。

答案 8 :(得分:1)

我可能错了,但这可能被视为一个整数分区(是三角数,但我还记得我错误记得)的问题?

如果是这种情况,每个条目都会有一个给定总和的一组结果的成员资格。预先计算和缓存给定总和的成员资格结果(是一个巨大的表)可以形成一个非常快速的解决方案。

我可能会这样做,但我需要一个大型数据集。有意思的是......

咨询大师Knuth http://cs.utsa.edu/~wagner/knuth/fasc3b.pdf