📌  相关文章
📜  计算系列 K、K+1、K+2、K+3、K+4、…、K+N 的所有可能的唯一和

📅  最后修改于: 2021-09-17 07:35:06             🧑  作者: Mango

给定一个数N和任何数 K 形成为K, K + 1, K + 2, K + 3, K + 4, …., K + N 的系列。任务是从N + 1 个整数序列中找出通过将两个或多个整数相加得到的唯一和的数量。

例子:

方法:应进行以下观察:

  • 由于K是数字,它唯一的意义在于大小不同的两个子集不能具有相同的总和。
  • 可以从子集的最小可能总和到具有大小为X的子集的最大可能总和获得唯一和,其中 (2 ≤ X ≤ (N + 1))。

请按照以下步骤解决问题:

  1. 初始化两个数组array fsum[]rsum[] ,每个数组的大小为N + 10
  2. 对于这两种阵列FSUM []和率R sum [],更新FSUM的每个元素[I]AR [I] + FSUM [I – 1]Ar率R sum [I] [I] + FSUM第[i + 1]。
  3. 将变量ans初始化为 1,用于存储给定系列的不同可能总和的计数。
  4. 对于每个可能的子集大小X ,其中 (2 ≤ X ≤ (N + 1)),将值1 + rsum[n + 1 – k] + fsum[k] 添加ans
  5. ans的值是必需的答案,因此返回它。

下面是上述方法的实现:

C++
// C++ program for the above approach
 
#include 
using namespace std;
 
// Function to count the unique sum
int count_unique_sum(int n)
{
 
    int i, ar[n + 1], fsum[n + 1];
    int rsum[n + 1], ans = 1;
 
    // Initialize array fsum[] with 0
    memset(fsum, 0, sizeof fsum);
 
    // Initialize array rsum[] with 0
    memset(rsum, 0, sizeof rsum);
 
    for (i = 0; i <= n; i++) {
        ar[i] = i;
    }
 
    // Set fsum[0] as ar[0]
    fsum[0] = ar[0];
 
    // Set rsum[0] as ar[n]
    rsum[n] = ar[n];
 
    // For each i update fsum[i] with
    // ar[i] + fsum[i - 1]
    for (i = 1; i <= n; i++) {
        fsum[i] = ar[i] + fsum[i - 1];
    }
 
    // For each i from n-1, update
    // rsum[i] with ar[i] + fsum[i + 1]
    for (i = n - 1; i >= 0; i--) {
        rsum[i] = ar[i] + rsum[i + 1];
    }
 
    // K represent size of subset as
    // explained above
    for (int k = 2; k <= n; k++) {
 
        // Using above relation
        ans += 1 + rsum[n + 1 - k]
               - fsum[k - 1];
    }
 
    // Return the result
    return ans;
}
 
// Driver Code
int main()
{
    // Given a number N
    int N = 4;
 
    // Function Call
    cout << count_unique_sum(N);
    return 0;
}


Java
// Java program for the above approach
import java.util.*;
class GFG{
   
// Function to count the unique sum
static int count_unique_sum(int n)
{
    int i;
    int ar[] = new int[n + 1];
    int fsum[] = new int[n + 1];
    int rsum[] = new int[n + 1];
    int ans = 1;
   
    // Initialize array fsum[] with 0
    Arrays.fill(fsum, 0);
  
    // Initialize array rsum[] with 0
    Arrays.fill(rsum, 0);
  
    for (i = 0; i <= n; i++)
    {
        ar[i] = i;
    }
  
    // Set fsum[0] as ar[0]
    fsum[0] = ar[0];
  
    // Set rsum[0] as ar[n]
    rsum[n] = ar[n];
  
    // For each i update fsum[i] with
    // ar[i] + fsum[i - 1]
    for (i = 1; i <= n; i++)
    {
        fsum[i] = ar[i] + fsum[i - 1];
    }
  
    // For each i from n-1, update
    // rsum[i] with ar[i] + fsum[i + 1]
    for (i = n - 1; i >= 0; i--)
    {
        rsum[i] = ar[i] + rsum[i + 1];
    }
  
    // K represent size of subset as
    // explained above
    for (int k = 2; k <= n; k++)
    {
  
        // Using above relation
        ans += 1 + rsum[n + 1 - k] -
                     fsum[k - 1];
    }
  
    // Return the result
    return ans;
}
  
// Driver Code
public static void main(String[] args)
{
    // Given a number N
    int N = 4;
  
    // Function Call
    System.out.print(count_unique_sum(N));
}
}
 
// This code is contributed by rock__cool


Python3
# Python3 program for the above approach
 
# Function to count the unique sum
def count_unique_sum(n):
 
    ar = [0] * (n + 1)
    fsum = [0] * (n + 1)
    rsum = [0] * (n + 1)
    ans = 1
 
    for i in range(0, n + 1):
        ar[i] = i
     
    # Set fsum[0] as ar[0]
    fsum[0] = ar[0]
 
    # Set rsum[0] as ar[n]
    rsum[n] = ar[n]
 
    # For each i update fsum[i] with
    # ar[i] + fsum[i - 1]
    for i in range(1, n + 1):
        fsum[i] = ar[i] + fsum[i - 1]
 
    # For each i from n-1, update
    # rsum[i] with ar[i] + fsum[i + 1]
    for i in range(n - 1, -1, -1):
        rsum[i] = ar[i] + rsum[i + 1]
     
    # K represent size of subset as
    # explained above
    for k in range(2, n + 1):
 
        # Using above relation
        ans += (1 + rsum[n + 1 - k] -
                    fsum[k - 1])
     
    # Return the result
    return ans
 
# Driver Code
 
# Given a number N
N = 4
 
# Function call
print(count_unique_sum(N))
 
# This code is contributed by sanjoy_62


C#
// C# program for the above approach
using System;
class GFG{
   
// Function to count the unique sum
static int count_unique_sum(int n)
{
    int i;
    int []ar = new int[n + 1];
    int []fsum = new int[n + 1];
    int []rsum = new int[n + 1];
    int ans = 1;
  
    for (i = 0; i <= n; i++)
    {
        ar[i] = i;
    }
  
    // Set fsum[0] as ar[0]
    fsum[0] = ar[0];
  
    // Set rsum[0] as ar[n]
    rsum[n] = ar[n];
  
    // For each i update fsum[i] with
    // ar[i] + fsum[i - 1]
    for (i = 1; i <= n; i++)
    {
        fsum[i] = ar[i] + fsum[i - 1];
    }
  
    // For each i from n-1, update
    // rsum[i] with ar[i] + fsum[i + 1]
    for (i = n - 1; i >= 0; i--)
    {
        rsum[i] = ar[i] + rsum[i + 1];
    }
  
    // K represent size of subset as
    // explained above
    for (int k = 2; k <= n; k++)
    {
  
        // Using above relation
        ans += 1 + rsum[n + 1 - k] -
                   fsum[k - 1];
    }
  
    // Return the result
    return ans;
}
  
// Driver Code
public static void Main(String[] args)
{
    // Given a number N
    int N = 4;
  
    // Function Call
    Console.Write(count_unique_sum(N));
}
}
 
// This code is contributed by PrinciRaj1992


Javascript


输出
20

时间复杂度: O(N)
辅助空间: O(N)