📌  相关文章
📜  由取自范围 [L, R] 的 N 个数字形成的不同和的计数

📅  最后修改于: 2022-05-13 01:56:10.011000             🧑  作者: Mango

由取自范围 [L, R] 的 N 个数字形成的不同和的计数

给定三个整数NLR 。任务是计算使用范围[L, R]中的N个数字形成的不同总和,其中任何数字都可以无限次取。

例子:

朴素方法:解决给定问题的最简单方法是从范围 [ L , R] 然后计算这些组合形成的不同总和。

时间复杂度: O((R – L) N )
辅助空间: O(1)

有效的方法: 给定的问题可以通过一些观察和使用一些数学来解决。这里可以使用的最小和最大数字分别是LR。因此,可以形成的最小和最大可能和分别是L*N (所有N个数字都是L)和R*N (所有N个数字都是R),同样,这个范围之间的所有其他和也可以形成。请按照以下步骤解决给定的问题。

  • 初始化一个变量,比如minSum = L*N ,以存储可能的最小总和。
  • 初始化一个变量maxSum = R*N来存储最大可能的总和。
  • 最终答案是[minSum, maxSum]范围内的总数,即(maxSum – minSum + 1)

下面是上述方法的实现:

C++
// C++ program for the above approach
#include 
using namespace std;
 
// Function to find total number of
// different sums of N numbers in
// the range [L, R]
int countDistinctSums(int N, int L, int R)
{
 
    // To store minimum possible sum with
    // N numbers with all as L
    int minSum = L * N;
 
    // To store maximum possible sum with
    // N numbers with all as R
    int maxSum = R * N;
 
    // All other numbers in between maxSum
    // and minSum can also be formed so numbers
    // in this range is the final answer
    return maxSum - minSum + 1;
}
 
// Driver Code
int main()
{
    int N = 2, L = 1, R = 3;
    cout << countDistinctSums(N, L, R);
 
    return 0;
}


Java
// Java program for the above approach
 
import java.util.*;
 
class GFG{
 
// Function to find total number of
// different sums of N numbers in
// the range [L, R]
static int countDistinctSums(int N, int L, int R)
{
 
    // To store minimum possible sum with
    // N numbers with all as L
    int minSum = L * N;
 
    // To store maximum possible sum with
    // N numbers with all as R
    int maxSum = R * N;
 
    // All other numbers in between maxSum
    // and minSum can also be formed so numbers
    // in this range is the final answer
    return maxSum - minSum + 1;
}
 
// Driver Code
public static void main(String[] args)
{
    int N = 2, L = 1, R = 3;
    System.out.print(countDistinctSums(N, L, R));
 
}
}
 
// This code is contributed by 29AjayKumar


Python3
# Python program for the above approach
 
# Function to find total number of
# different sums of N numbers in
# the range [L, R]
def countDistinctSums(N, L, R):
 
    # To store minimum possible sum with
    # N numbers with all as L
    minSum = L * N
 
    # To store maximum possible sum with
    # N numbers with all as R
    maxSum = R * N
 
    # All other numbers in between maxSum
    # and minSum can also be formed so numbers
    # in this range is the final answer
    return maxSum - minSum + 1
 
# Driver Code
if __name__ == "__main__":
    N = 2
    L = 1
    R = 3
    print(countDistinctSums(N, L, R))
 
    # This code is contributed by rakeshsahni


C#
// C# program for the above approach
using System;
using System.Collections.Generic;
 
class GFG{
 
// Function to find total number of
// different sums of N numbers in
// the range [L, R]
static int countDistinctSums(int N, int L, int R)
{
 
    // To store minimum possible sum with
    // N numbers with all as L
    int minSum = L * N;
 
    // To store maximum possible sum with
    // N numbers with all as R
    int maxSum = R * N;
 
    // All other numbers in between maxSum
    // and minSum can also be formed so numbers
    // in this range is the final answer
    return maxSum - minSum + 1;
}
 
// Driver Code
public static void Main()
{
    int N = 2, L = 1, R = 3;
    Console.Write(countDistinctSums(N, L, R));
}
}
 
// This code is contributed by SURENDRA_GANGWAR.


Javascript


输出:
5

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