You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何计算所有无重复子集的元素乘积总和?竞赛解题代码咨询

Hey there! Let's take a look at your code and figure out why it's only working for some test cases. First, here's your original code for reference:

static long calcMultisSums(int[] arr) { 
    int sum=0, n=arr.length; 
    for(int i=0;i<n;i++) sum+=arr[i]; 
    int mult=0; 
    for(int i=0;i<n;i++){ 
        for(int b=i+1;b<n;b++) mult+=arr[b]*arr[i]; 
    } 
    int mult2=0; 
    if(n>2){ 
        mult2=1; 
        for(int value : arr) { 
            mult2 *= value; 
        } 
    } 
    return mult+sum+mult2; 
}

Key Issues Causing Failures

Your code works for small arrays (length 1, 2, or 3) but breaks for longer ones, plus it runs into overflow problems. Here's why:

1. Missing Intermediate Subset Products

Right now, your code only calculates:

  • Sum of single-element subsets (sum)
  • Sum of two-element subset products (mult)
  • Product of all elements (only if array length > 2, mult2)

But for arrays longer than 3 elements, you're skipping subsets of size 3, 4, ..., n-1. For example, if you have [a,b,c,d], the correct total needs to include the sum of all 3-element subset products (abc+abd+acd+bcd), which your code completely ignores. That's why it fails for arrays with 4+ elements.

2. Integer Overflow

You're using int to store sums and products, but these values can easily exceed the 32-bit integer limit (2^31 - 1 = 2147483647). Even a pair of large numbers (like 10^9 * 10^9) will overflow an int, leading to silent, incorrect results.


Fixed Solution

We can solve both problems with a dynamic programming approach that calculates the sum of products for all non-empty subsets in one pass, using long to avoid overflow. Here's how it works:

We maintain a running total (dp) where dp represents the sum of products of all subsets we've processed so far. For each new element num, the new subsets added are:

  • All existing subsets multiplied by num
  • The subset containing just num itself

This translates to the update rule: dp = dp + num * (dp + 1)

Here's the corrected code:

static long calcMultisSums(int[] arr) {
    long dp = 0;
    for (int num : arr) {
        dp = dp + num * (dp + 1);
    }
    return dp;
}

Quick Test to Verify

Let's use [a,b,c] to confirm:

  1. Start with dp = 0
  2. After processing a: dp = 0 + a*(0+1) = a (only subset {a})
  3. After processing b: dp = a + b*(a+1) = a + ab + b (subsets {a}, {b}, {a,b})
  4. After processing c: dp = (a+b+ab) + c*(a+b+ab+1) = a+b+c+ab+ac+bc+abc (all non-empty subsets)

This approach handles every subset size, avoids overflow, and runs in O(n) time — perfect for programming competition constraints.

内容的提问来源于stack exchange,提问作者Waseem

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.26 08:34:42