[Array]Combination Sum III

本文介绍了一种寻找所有可能的k个数之和等于目标数n的组合算法,使用了递归方法实现,限定组合中只能使用1到9之间的数字,并确保每组组合中的数字都是唯一的。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Find all possible combinations of k numbers that add up to a number n, given that only numbers from 1 to 9 can be used and each combination should be a unique set of numbers.

Example 1:

Input: k = 3, n = 7

Output:

[[1,2,4]]

Example 2:

Input: k = 3, n = 9

Output:

[[1,2,6], [1,3,5], [2,3,4]]

方法:递归调用即可

class Solution {
private:
      void recursive(int sum,vector<int>& combination,vector<vector<int>>& res,int now,int target,int layer,int k){

          if(sum==target&&layer==k){
              res.push_back(combination);
              return;
          }
          for(int i=now+1;i<=9;i++){
              if(layer+1>k)
                return;
              combination.push_back(i);
              recursive(sum+i,combination,res,i,target,layer+1,k);
              combination.pop_back();
          }
      }

public:
    vector<vector<int>> combinationSum3(int k, int n) {
        vector<vector<int>> res;
        vector<int> combination;
        recursive(0,combination,res,0,n,0,k);
        return res;

    }
};
### ABC384F Double Sum 2 Problem Explanation and Solution The **Double Sum 2** problem involves calculating a specific sum based on the elements of an array. The goal is to compute two sums: 1. \( S_1 \): The sum of all pairs \( (a_i + a_j)^2 \) where \( i < j \). 2. \( S_2 \): The sum of all triples \( (a_i + a_j + a_k)^2 \) where \( i < j < k \). To solve this efficiently without iterating over every possible pair or triple, one can use mathematical properties and precomputation techniques. #### Efficient Calculation Using Precomputed Values Let's denote: - \( n \) as the length of the input array. - \( A \) as the input array containing integers. Firstly, calculate several intermediate values that will help in computing both \( S_1 \) and \( S_2 \): ```python def double_sum_2(A): n = len(A) # Calculate prefix sums total_sum = sum(A) square_sum = sum(x * x for x in A) # Compute S1 using formula derived from expanding (ai + aj)^2 s1 = ((n - 1) * square_sum + 2 * total_sum * (total_sum - total_sum)) // 2 # Compute S2 using similar expansion logic but more complex due to three terms cubic_terms = sum(x * x * x for x in A) quadratic_cross_terms = 3 * sum(a * b for i, a in enumerate(A) for b in A[i+1:]) s2 = (cubic_terms + 3 * quadratic_cross_terms + 6 * total_sum * (total_sum - A[0]) * (total_sum - A[-1])) // 6 return s1, s2 ``` This approach leverages algebraic identities to reduce complexity significantly compared to brute force methods which would require nested loops with time complexities of O(n^2) for pairs and O(n^3) for triples[^1]. For practical implementation, especially during competitive programming contests, optimizing these calculations further might involve modular arithmetic when dealing with large numbers to prevent overflow issues. --related questions-- 1. How does modular arithmetic apply to preventing integer overflow in such problems? 2. Can you provide examples of other combinatorial summation problems suitable for practice? 3. What are common pitfalls encountered while solving combination-based algorithm challenges? 4. Are there alternative algorithms or data structures useful for handling multiple element combinations?
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值