📜  LCM为N的最大非重复数之和

📅  最后修改于: 2021-04-24 14:52:46             🧑  作者: Mango

给定数字N,任务是找出不同数字的最大和,以使所有这些数字的最小公倍数为N。

Input  : N = 12
Output : 20
Maximum sum which we can achieve is,
1 + 2 + 3 + 4 + 6 + 12 = 28

Input  : N = 15
Output : 24 

我们可以通过观察某些情况来解决该问题,因为N必须是所有数字的LCM,所以它们都将是N的除数,但是由于一个数字只能相加一次,因此所有被取数字应该是不同的。这个想法是将N的每个除数求和一次,以使结果最大化。
我们怎么能说我们得到的和是最大和?原因是,我们将N的所有除数都纳入了总和,现在,如果我们再将一个非N的除数的和加入一个总和,则总和将增加,但LCM属性将不会被所有这些整数所保持。因此,除了N的所有除数之外,不可能再增加一个数,因此,给定N个所有除数的总和,我们的问题归结为这一点,这可以在O(sqrt(N))时间内解决。
因此,解决方案的总时间复杂度将为O(sqrt(N)),其中O(1)为额外空间。
下面是上述概念的代码。请参阅此帖子以查找数字的所有除数。

C++
// C/C++ program to get maximum sum of Numbers
// with condition that their LCM should be N
#include 
using namespace std;
 
// Method returns maximum sum f distinct
// number whose LCM is N
int getMaximumSumWithLCMN(int N)
{
    int sum = 0;
    int LIM = sqrt(N);
 
    // find all divisors which divides 'N'
    for (int i = 1; i <= LIM; i++) {
        // if 'i' is divisor of 'N'
        if (N % i == 0) {
            // if both divisors are same then add
            // it only once else add both
            if (i == (N / i))
                sum += i;
            else
                sum += (i + N / i);
        }
    }
 
    return sum;
}
 
// Driver code to test above methods
int main()
{
    int N = 12;
    cout << getMaximumSumWithLCMN(N) << endl;
    return 0;
}


Java
// Java program to get
// maximum sum of Numbers
// with condition that
// their LCM should be N
 
class GFG {
    // Method returns maximum
    // sum f distinct number
    // whose LCM is N
    static int getMaximumSumWithLCMN(int N)
    {
        int sum = 0;
        int LIM = (int)Math.sqrt(N);
 
        // find all divisors which divides 'N'
        for (int i = 1; i <= LIM; i++) {
            // if 'i' is divisor of 'N'
            if (N % i == 0) {
                // if both divisors are same then add
                // it only once else add both
                if (i == (N / i))
                    sum += i;
                else
                    sum += (i + N / i);
            }
        }
 
        return sum;
    }
 
    // Driver code
    public static void main(String[] args)
    {
        int N = 12;
        System.out.println(getMaximumSumWithLCMN(N));
    }
}
 
// This code is contributed by Anant Agarwal.


Python3
# Python program to get
# maximum sum of Numbers
# with condition that
# their LCM should be N
 
import math
 
# Method returns maximum sum f distinct
# number whose LCM is N
def getMaximumSumWithLCMN(N):
 
    sum = 0
    LIM = int(math.sqrt(N))
  
    # find all divisors which divides 'N'
    for i in range(1, LIM + 1):
 
        # if 'i' is divisor of 'N'
        if (N % i == 0):
 
            # if both divisors are same then add
            # it only once else add both
            if (i == (N // i)):
                sum = sum + i
            else:
                sum = sum + (i + N // i)
      
    return sum
 
# driver code
 
N = 12
print(getMaximumSumWithLCMN(N))
 
# This code is contributed
# by Anant Agarwal.


C#
// C# program to get maximum sum
// of Numbers with condition that
// their LCM should be N
using System;
 
class GFG {
     
    // Method returns maximum sum f
    // distinct number whose LCM is N
    static int getMaximumSumWithLCMN(int N)
    {
        int sum = 0;
        int LIM = (int)Math.Sqrt(N);
 
        // Find all divisors which divides 'N'
        for (int i = 1; i <= LIM; i++) {
             
            // if 'i' is divisor of 'N'
            if (N % i == 0) {
                 
                // if both divisors are same then
                // add it only once else add both
                if (i == (N / i))
                    sum += i;
                else
                    sum += (i + N / i);
            }
        }
 
        return sum;
    }
 
    // Driver code
    public static void Main()
    {
        int N = 12;
        Console.Write(getMaximumSumWithLCMN(N));
    }
}
 
// This code is contributed by nitin mittal


PHP


Javascript


输出:

28