Efficient algorithm to calculate the sum of all k-products

前端 未结 6 1933
青春惊慌失措
青春惊慌失措 2021-02-06 04:49

Suppose you are given a list L of n numbers and an integer k. Is there an efficient way to calculate the sum of all products of

6条回答
  •  [愿得一人]
    2021-02-06 05:31

    For k=2,

    let's s = SUM_x_in_L x (sum of the numbers) and sq = SUM_x_in_L x^2 (sum of the squares)

    then it's SUM_x_in_L (s - x) * x / 2 = (s * s - sq) / 2

提交回复
热议问题