由取自范围 [L, R] 的 N 个数字形成的不同和的计数
给定三个整数N 、 L和R 。任务是计算使用范围[L, R]中的N个数字形成的不同总和,其中任何数字都可以无限次取。
例子:
Input: N = 2, L = 1, R = 3
Output: 5
Explanation: Generating all distinct combinations of 2 numbers taken from the range [1, 3]
{1, 1} => sum = 2
{1, 2} => sum = 3
{1, 3} => sum = 4
{2, 2} => sum = 4
{2, 3} => sum = 5
{3, 3} => sum = 6
Therefore, there are 5 (2, 3, 4, 5, 6) different sums possible with 2 numbers taken from range [1, 3].
Input: N = 3, L = 1, R = 9
Output: 10
朴素方法:解决给定问题的最简单方法是从范围 [ L , R]和 然后计算这些组合形成的不同总和。
时间复杂度: O((R – L) N )
辅助空间: O(1)
有效的方法: 给定的问题可以通过一些观察和使用一些数学来解决。这里可以使用的最小和最大数字分别是L和R。因此,可以形成的最小和最大可能和分别是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)