📌  相关文章
📜  最大化四个数的乘积

📅  最后修改于: 2021-04-24 21:17:59             🧑  作者: Mango

给定整数N,任务是找到A,B,C,D的最大乘积,以便满足以下条件:

如果不存在解决方案,请打印“ -1”(不带引号)。

例子:

Input: N = 8
Output: 16
The divisors of 8 are {1, 2, 4, 8}. 
The maximized product can be formed 
by multiplying 2*2*2*2 = 16 as 2+2+2+2 = 8.

Input: N = 4 
Output: 1
The divisors of 4 are {1, 2, 4}. 
The maximized product can be formed
by multiplying 1*1*1*1 = 1 as 1+1+1+1 =4.

天真的方法:

  1. 找出给定数量的因素,并在名为因素向量存储在O(开方(N))的复杂性。
  2. 初始化积= -1,然后遍历因子向量以应用蛮力方法找到最大化的积。

下面是上述方法的实现:

C++
// C++ implementation of above approach
#include 
using namespace std;
  
// Declare the vector of factors for storing the
vector factors;
  
// function to find out the factors of a number
void findFactors(int n)
{
    // Loop until the i reaches the sqrt(n)
    for (int i = 1; i * i <= n; i++) {
  
        // Check if i is a factor of n
        if (n % i == 0) {
  
            // if both the factors are same
            // we only push one factor
            if ((n / i) == i)
                factors.push_back(i);
  
            else {
  
                // factor1 is pushed
                factors.push_back(n / i);
  
                // factor2 is pushed
                factors.push_back(i);
            }
        }
    }
}
  
// Function to find the maximum product
int findProduct(int n)
{
    // Initialize the product with -1
    int product = -1;
    int si = factors.size();
    for (int i = 0; i < si; i++)
        for (int j = 0; j < si; j++)
            for (int k = 0; k < si; k++)
                for (int l = 0; l < si; l++) {
  
                    // Find the sum of factors and store it in s
                    int s = factors[i] + factors[j]
                            + factors[k] + factors[l];
  
                    // Compare whether it is equal to the n
                    if (s == n) {
  
                        // product of factors
                        int p = factors[i] * factors[j] * factors[k] * factors[l];
  
                        // Check whether we have a better
                        // p now if yes update
                        if (p > product)
                            product = p;
                    }
                }
  
    return product;
}
  
// Driver code
int main()
{
    int n = 10;
  
    // initializes the vectors with the divisors of n
    findFactors(n);
  
    // prints out the maximised product.
    cout << findProduct(n);
  
    return 0;
}


Java
// Java implementation of above approach 
  
import java.util.ArrayList;
import java.util.List;
  
public class GFG {
  
// Declare the ArrayList of factors for storing the 
    static List< Integer> factors = new ArrayList<>(10);
  
// function to find out the factors of a number 
    static void findFactors(int n) {
        // Loop until the i reaches the sqrt(n) 
        for (int i = 1; i * i <= n; i++) {
  
            // Check if i is a factor of n 
            if (n % i == 0) {
                // if both the factors are same 
                // we only push one factor 
                if ((n / i) == i) {
                    factors.add(factors.size(), i);
                } else {
  
                    // factor1 is pushed 
                    factors.add(factors.size(), n / i);
  
                    // factor2 is pushed 
                    factors.add(factors.size(), i);
                }
            }
        }
    }
  
// Function to find the maximum product 
    static int findProduct(int n) {
        // Initialize the product with -1 
  
        int product = -1;
        int si = factors.size();
        for (int i = 0; i < si; i++) {
            for (int j = 0; j < si; j++) {
                for (int k = 0; k < si; k++) {
                    for (int l = 0; l < si; l++) {
  
                        // Find the sum of factors and store it in s 
                        int s = factors.get(i) + factors.get(j)
                                + factors.get(k) + factors.get(l);
  
                        // Compare whether it is equal to the n 
                        if (s == n) {
  
                            // product of factors 
                            int p = factors.get(i) * factors.get(j) * factors.get(k) * 
                                               factors.get(l);
  
                            // Check whether we have a better 
                            // p now if yes update 
                            if (p > product) {
                                product = p;
                            }
                        }
                    }
                }
            }
        }
  
        return product;
    }
  
    // Driver Code 
    public static void main(String[] args) {
        int n = 10;
  
        // intializes the List with the divisors of n 
        findFactors(n);
        // prints out the maximised product. 
        System.out.println(findProduct(n));
    }
}


Python3
# Python 3 implementation of above approach
from math import sqrt
  
# Declare the vector of factors 
# for storing the
factors = []
  
# function to find out the factors 
# of a number
def findFactors(n):
      
    # Loop until the i reaches the sqrt(n)
    for i in range(1, int(sqrt(n)) + 1, 1):
          
        # Check if i is a factor of n
        if (n % i == 0):
              
            # if both the factors are same
            # we only push one factor
            if ((n / i) == i):
                factors.append(i)
  
            else:
                  
                # factor1 is pushed
                factors.append(n / i)
  
                # factor2 is pushed
                factors.append(i)
          
# Function to find the maximum product
def findProduct(n):
      
    # Initialize the product with -1
    product = -1
    si = len(factors)
    for i in range(si):
        for j in range(si):
            for k in range(si):
                for l in range(si):
                      
                    # Find the sum of factors and store it in s
                    s = (factors[i] + factors[j] + 
                         factors[k] + factors[l])
  
                    # Compare whether it is equal to the n
                    if (s == n):
                          
                        # product of factors
                        p = (factors[i] * factors[j] * 
                             factors[k] * factors[l])
  
                        # Check whether we have a better
                        # p now if yes update
                        if (p > product):
                            product = p
                      
    return product
  
# Driver code
if __name__ == '__main__':
    n = 10
  
    # initializes the vectors with
    # the divisors of n
    findFactors(n)
  
    # prints out the maximised product.
    print(int(findProduct(n)))
  
# This code is contributed by
# Sanjit_Prasad


C#
// C# implementation of above approach 
using System;
using System.Collections.Generic; 
  
class GFG 
{
  
// Declare the ArrayList of factors for storing the 
static List factors = new List(10);
  
// function to find out the factors of a number 
static void findFactors(int n)
{
    // Loop until the i reaches the sqrt(n) 
    for (int i = 1; i * i <= n; i++) 
    {
  
        // Check if i is a factor of n 
        if (n % i == 0) 
        {
            // if both the factors are same 
            // we only push one factor 
            if ((n / i) == i)
            {
                factors.Insert(factors.Count, i);
            } 
            else
            {
  
                // factor1 is pushed 
                factors.Insert(factors.Count, n / i);
  
                // factor2 is pushed 
                factors.Insert(factors.Count, i);
            }
        }
    }
}
  
// Function to find the maximum product 
static int findProduct(int n)
{
      
    // Initialize the product with -1 
    int product = -1;
    int si = factors.Count;
    for (int i = 0; i < si; i++) 
    {
        for (int j = 0; j < si; j++) 
        {
            for (int k = 0; k < si; k++) 
            {
                for (int l = 0; l < si; l++) 
                {
  
                    // Find the sum of factors and store it in s 
                    int s = factors[i] + factors[j] + 
                            factors[k] + factors[l];
  
                    // Compare whether it is equal to the n 
                    if (s == n)
                    {
  
                        // product of factors 
                        int p = factors[i] * factors[j] * 
                                factors[k] * factors[l];
  
                        // Check whether we have a better 
                        // p now if yes update 
                        if (p > product)
                        {
                            product = p;
                        }
                    }
                }
            }
        }
    }
    return product;
}
  
// Driver Code 
public static void Main(String[] args) 
{
    int n = 10;
  
    // intializes the List with the divisors of n 
    findFactors(n);
      
    // prints out the maximised product. 
    Console.WriteLine(findProduct(n));
}
}
  
// This code is contributed by Rajput-Ji


C++
// C++ implementation of above approach
#include 
using namespace std;
  
// We declare the vector of factors for storing the
vector factors;
  
// function to find out the factors of a number
void findFactors(int n)
{
    // we loop until the i reaches the sqrt(n)
    for (int i = 1; i * i <= n; i++) {
  
        // we check if i is a factor of n
        if (n % i == 0) {
  
            // if both the factors are same
            // only push one factor
            if ((n / i) == i) {
                factors.push_back(i);
            }
            else {
  
                // factor1 is pushed
                factors.push_back(n / i);
  
                // factor2 is pushed
                factors.push_back(i);
            }
        }
    }
}
  
int findProduct(int n)
{
    // initialise the product with -1
    int product = -1;
    int si = factors.size();
  
    for (int i = 0; i < si; i++)
        for (int j = 0; j < si; j++)
            for (int k = 0; k < si; k++) {
  
                // we find the sum of factors
                // and store it in s
                int s = factors[i] + factors[j] + factors[k];
  
                // we check whether the fourth
                // factor exists or not
                if (binary_search(factors.begin(), factors.end(), n - s)) {
                    // product of factors
                    int p = factors[i] * factors[j] * factors[k] * (n - s);
  
                    // we check whether we have a better
                    // p now if yes update
                    if (p > product)
                        product = p;
                }
            }
  
    return product;
}
  
// Driver code
int main()
{
    int n = 10;
  
    // initializes the vectors with the divisors of n
    findFactors(n);
  
    // sorts the factors vector
    sort(factors.begin(), factors.end());
  
    // prints out the maximised product.
    cout << findProduct(n);
    return 0;
}


Java
// Java implementation of above approach 
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
  
public class GFG {
  
// Declare the ArrayList of factors for storing the 
    static List< Integer> factors = new ArrayList<>();
  
// function to find out the factors of a number 
    static void findFactors(int n) {
        // we loop until the i reaches the sqrt(n) 
        for (int i = 1; i * i <= n; i++) {
  
            // we check if i is a factor of n 
            if (n % i == 0) {
  
                // if both the factors are same 
                // only push one factor 
                if ((n / i) == i) {
                    factors.add(factors.size(), i);
                } else {
  
                    // factor1 is pushed 
                    factors.add(factors.size(), n/i);
  
                    // factor2 is pushed 
                    factors.add(factors.size(), i);
                }
            }
        }
    }
  
// Function to find the maximum product 
    static int findProduct(int n) {
// initialise the product with -1 
        int product = -1;
        int si = factors.size();
  
        for (int i = 0; i < si; i++) {
            for (int j = 0; j < si; j++) {
                for (int k = 0; k < si; k++) {
  
                    // we find the sum of factors 
                    // and store it in s 
                    int s = factors.get(i) + factors.get(j) + factors.get(k);
  
                    // we check whether the fourth 
                    // factor exists or not 
                    if (Collections.binarySearch(factors, n - s) >= 0) {
                        // product of factors 
                        int p = factors.get(i) * factors.get(j) * factors.get(k) * (n - s);
  
                        // we check whether we have a better 
                        // p now if yes update 
                        if (p > product) {
                            product = p;
                        }
                    }
                }
            }
        }
  
        return product;
    }
  
    // Driver Code 
    public static void main(String[] args) {
        int n = 10;
  
        // intializes the vectors with the divisors of n 
        findFactors(n);
  
        // sorts the factors vector 
        Collections.sort(factors);
  
        // prints out the maximised product. 
        System.out.println(findProduct(n));
    }
}


Python3
# Python3 implementation of above approach
  
# We declare the vector of factors for storing the
factors = []
  
# function to find out the factors of a number
def findFactors(n):
      
    # we loop until the i reaches the sqrt(n)
    for i in range(1, n + 1):
        if i * i > n:
            break
  
        # we check if i is a factor of n
        if (n % i == 0):
  
            # if both the factors are same
            # only push one factor
            if ((n / i) == i):
                factors.append(i)
            else:
  
                # factor1 is pushed
                factors.append(n // i)
  
                # factor2 is pushed
                factors.append(i)
  
def findProduct(n):
      
    # initialise the product with -1
    product = -1
    si = len(factors)
  
    for i in range(si):
        for j in range(si):
            for k in range(si):
  
                # we find the sum of factors
                # and store it in s
                s = factors[i] + factors[j] + factors[k]
  
                # we check whether the fourth
                # factor exists or not
                if ((n - s) in factors):
                      
                    # product of factors
                    p = factors[i] * factors[j] * \
                        factors[k] * (n - s)
  
                    # we check whether we have a better
                    # p now if yes update
                    if (p > product):
                        product = p
  
    return product
  
# Driver code
n = 10
  
# initializes the vectors
# with the divisors of n
findFactors(n)
  
# sorts the factors vector
factors = sorted(factors)
  
# prints out the maximized product.
print(findProduct(n))
  
# This code is contributed by Mohit Kumar


C#
// C# implementation of above approach 
using System;
using System.Collections.Generic;
  
class GFG 
{
      
// Declare the ArrayList of factors for storing the 
static List< int> factors = new List();
  
// function to find out the factors of a number 
static void findFactors(int n)
{
    // we loop until the i reaches the sqrt(n) 
    for (int i = 1; i * i <= n; i++) 
    {
  
        // we check if i is a factor of n 
        if (n % i == 0) 
        {
  
            // if both the factors are same 
            // only push one factor 
            if ((n / i) == i) 
            {
                factors.Insert(factors.Count, i);
            } 
            else 
            {
  
                // factor1 is pushed 
                factors.Insert(factors.Count, n / i);
  
                // factor2 is pushed 
                factors.Insert(factors.Count, i);
            }
        }
    }
}
  
// Function to find the maximum product 
static int findProduct(int n) 
{
      
    // initialise the product with -1 
    int product = -1;
    int si = factors.Count;
  
    for (int i = 0; i < si; i++) 
    {
        for (int j = 0; j < si; j++)
        {
            for (int k = 0; k < si; k++) 
            {
  
                // we find the sum of factors 
                // and store it in s 
                int s = factors[i] + factors[j] + factors[k];
  
                // we check whether the fourth 
                // factor exists or not 
                if (factors.BinarySearch(n - s) >= 0)
                {
                    // product of factors 
                    int p = factors[i] * factors[j] * 
                            factors[k] * (n - s);
  
                    // we check whether we have a better 
                    // p now if yes update 
                    if (p > product) 
                    {
                        product = p;
                    }
                }
            }
        }
    }
    return product;
}
  
// Driver Code 
public static void Main(String[] args) 
{
    int n = 10;
  
    // intializes the vectors with the divisors of n 
    findFactors(n);
  
    // sorts the factors vector 
    factors.Sort();
  
    // prints out the maximised product. 
    Console.WriteLine(findProduct(n));
}
}
  
// This code is contributed by Princi Singh


C++
// C++ implementation of above approach
#include 
using namespace std;
  
// For calculation of a^b
int modExp(int a, int b)
{
    int result = 1;
    while (b > 0) {
        if (b & 1)
            result = result * a;
        a = a * a;
        b /= 2;
    }
  
    return result;
}
  
// Function to check
int check(int num)
{
    // every odd and number less than 3.
    if (num & 1 || num < 3)
        return -1;
  
    // every number divisible by 4.
    else if (num % 4 == 0)
        return modExp(num / 4, 4);
  
    // every number divisible by 6.
    else if (num % 6 == 0)
        return modExp(num / 3, 2) * modExp(num / 6, 2);
  
    // every number divisible by 10.
    else if (num % 10 == 0)
        return modExp(num / 5, 2) * (num / 10) * (num / 2);
  
    // for every even number which is not
    // divisible by above values.
    else
        return -1;
}
  
// Driver code
int main()
{
    int num = 10;
    cout << check(num);
  
    return 0;
}


Java
// Java implementation of above approach
import java.io.*;
import java.util.*;
import java.lang.*;
  
class GFG
{
// For calculation of a^b
static int modExp(int a, int b)
{
    int result = 1;
    while (b > 0) 
    {
        if (b == 1)
            result = result * a;
        a = a * a;
        b /= 2;
    }
  
    return result;
}
  
// Function to check
static int check(int num)
{
    // every odd and number less than 3.
    if (num == 1 || num < 3)
        return -1;
  
    // every number divisible by 4.
    else if (num % 4 == 0)
        return modExp(num / 4, 4);
  
    // every number divisible by 6.
    else if (num % 6 == 0)
        return modExp(num / 3, 2) * modExp(num / 6, 2);
  
    // every number divisible by 10.
    else if (num % 10 == 0)
        return modExp(num / 5, 2) * (num / 10) * (num / 2);
  
    // for every even number which is not
    // divisible by above values.
    else
        return -1;
}
  
// Driver code
public static void main(String[] args)
{
    int num = 10;
    System.out.print(check(num));
}
}
  
// This code is contributed 
// by Akanksha Rai(Abby_akku)


Python 3
# Python3 implementation of above approach
  
  
# For calculation of a^b
def modExp( a, b):
  
    result = 1
    while (b > 0): 
        if (int(b) & 1):
            result = result * a
        a = a * a
        b /= 2
      
  
    return result
  
  
# Function to check
def check( num):
  
    # every odd and number less than 3.
    if (num & 1 or num < 3):
        return -1
  
    # every number divisible by 4.
    elif (num % 4 == 0):
        return modExp(num / 4, 4)
  
    # every number divisible by 6.
    elif (num % 6 == 0):
        return modExp(num / 3, 2) * modExp(num / 6, 2)
  
    # every number divisible by 10.
    elif (num % 10 == 0):
        return modExp(num / 5, 2) * (num / 10) * (num / 2)
  
    # for every even number which is not
    # divisible by above values.
    else:
        return -1
  
  
# Driver code
if __name__=='__main__':
    num = 10
    print(int(check(num)))
  
# This code is contributed by ash264


C#
// C#  implementation of above approach 
using System;
  
public class GFG{
    // For calculation of a^b 
static int modExp(int a, int b) 
{ 
    int result = 1; 
    while (b > 0) 
    { 
        if (b == 1) 
            result = result * a; 
        a = a * a; 
        b /= 2; 
    } 
  
    return result; 
} 
  
// Function to check 
static int check(int num) 
{ 
    // every odd and number less than 3. 
    if (num == 1 || num < 3) 
        return -1; 
  
    // every number divisible by 4. 
    else if (num % 4 == 0) 
        return modExp(num / 4, 4); 
  
    // every number divisible by 6. 
    else if (num % 6 == 0) 
        return modExp(num / 3, 2) * modExp(num / 6, 2); 
  
    // every number divisible by 10. 
    else if (num % 10 == 0) 
        return modExp(num / 5, 2) * (num / 10) * (num / 2); 
  
    // for every even number which is not 
    // divisible by above values. 
    else
        return -1; 
} 
  
// Driver code 
static public void Main (){
          
    int num = 10; 
    Console.WriteLine(check(num)); 
    } 
}


PHP
 0) 
    { 
        if ($b & 1) 
            $result = $result * $a; 
        $a = $a * $a; 
        $b /= 2; 
    } 
  
    return $result; 
} 
  
// Function to check 
function check($num) 
{ 
    // every odd and number less than 3. 
    if ($num & 1 || $num < 3) 
        return -1; 
  
    // every number divisible by 4. 
    else if ($num % 4 == 0) 
        return modExp($num / 4, 4); 
  
    // every number divisible by 6. 
    else if ($num % 6 == 0) 
        return modExp($num / 3, 2) * 
               modExp($num / 6, 2); 
  
    // every number divisible by 10. 
    else if ($num % 10 == 0) 
        return modExp($num / 5, 2) * 
                     ($num / 10) * ($num / 2); 
  
    // for every even number which is not 
    // divisible by above values. 
    else
        return -1; 
} 
  
// Driver code 
  
$num = 10; 
echo check($num); 
      
// This code is contributed 
// by Shivi_Aggarwal
?>


输出:
20

时间复杂度: O((除数的数量)^ 4))

更好的方法:通过从上述代码中删除第4个循环,而使用二进制搜索来查找第四个因子,可以稍微降低复杂度。由于二进制搜索仅在列表排序时才有效。因此,我们需要对因子向量进行排序,以便可以对问题进行二元搜索。

下面是上述方法的实现:

C++

// C++ implementation of above approach
#include 
using namespace std;
  
// We declare the vector of factors for storing the
vector factors;
  
// function to find out the factors of a number
void findFactors(int n)
{
    // we loop until the i reaches the sqrt(n)
    for (int i = 1; i * i <= n; i++) {
  
        // we check if i is a factor of n
        if (n % i == 0) {
  
            // if both the factors are same
            // only push one factor
            if ((n / i) == i) {
                factors.push_back(i);
            }
            else {
  
                // factor1 is pushed
                factors.push_back(n / i);
  
                // factor2 is pushed
                factors.push_back(i);
            }
        }
    }
}
  
int findProduct(int n)
{
    // initialise the product with -1
    int product = -1;
    int si = factors.size();
  
    for (int i = 0; i < si; i++)
        for (int j = 0; j < si; j++)
            for (int k = 0; k < si; k++) {
  
                // we find the sum of factors
                // and store it in s
                int s = factors[i] + factors[j] + factors[k];
  
                // we check whether the fourth
                // factor exists or not
                if (binary_search(factors.begin(), factors.end(), n - s)) {
                    // product of factors
                    int p = factors[i] * factors[j] * factors[k] * (n - s);
  
                    // we check whether we have a better
                    // p now if yes update
                    if (p > product)
                        product = p;
                }
            }
  
    return product;
}
  
// Driver code
int main()
{
    int n = 10;
  
    // initializes the vectors with the divisors of n
    findFactors(n);
  
    // sorts the factors vector
    sort(factors.begin(), factors.end());
  
    // prints out the maximised product.
    cout << findProduct(n);
    return 0;
}

Java

// Java implementation of above approach 
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
  
public class GFG {
  
// Declare the ArrayList of factors for storing the 
    static List< Integer> factors = new ArrayList<>();
  
// function to find out the factors of a number 
    static void findFactors(int n) {
        // we loop until the i reaches the sqrt(n) 
        for (int i = 1; i * i <= n; i++) {
  
            // we check if i is a factor of n 
            if (n % i == 0) {
  
                // if both the factors are same 
                // only push one factor 
                if ((n / i) == i) {
                    factors.add(factors.size(), i);
                } else {
  
                    // factor1 is pushed 
                    factors.add(factors.size(), n/i);
  
                    // factor2 is pushed 
                    factors.add(factors.size(), i);
                }
            }
        }
    }
  
// Function to find the maximum product 
    static int findProduct(int n) {
// initialise the product with -1 
        int product = -1;
        int si = factors.size();
  
        for (int i = 0; i < si; i++) {
            for (int j = 0; j < si; j++) {
                for (int k = 0; k < si; k++) {
  
                    // we find the sum of factors 
                    // and store it in s 
                    int s = factors.get(i) + factors.get(j) + factors.get(k);
  
                    // we check whether the fourth 
                    // factor exists or not 
                    if (Collections.binarySearch(factors, n - s) >= 0) {
                        // product of factors 
                        int p = factors.get(i) * factors.get(j) * factors.get(k) * (n - s);
  
                        // we check whether we have a better 
                        // p now if yes update 
                        if (p > product) {
                            product = p;
                        }
                    }
                }
            }
        }
  
        return product;
    }
  
    // Driver Code 
    public static void main(String[] args) {
        int n = 10;
  
        // intializes the vectors with the divisors of n 
        findFactors(n);
  
        // sorts the factors vector 
        Collections.sort(factors);
  
        // prints out the maximised product. 
        System.out.println(findProduct(n));
    }
}

Python3

# Python3 implementation of above approach
  
# We declare the vector of factors for storing the
factors = []
  
# function to find out the factors of a number
def findFactors(n):
      
    # we loop until the i reaches the sqrt(n)
    for i in range(1, n + 1):
        if i * i > n:
            break
  
        # we check if i is a factor of n
        if (n % i == 0):
  
            # if both the factors are same
            # only push one factor
            if ((n / i) == i):
                factors.append(i)
            else:
  
                # factor1 is pushed
                factors.append(n // i)
  
                # factor2 is pushed
                factors.append(i)
  
def findProduct(n):
      
    # initialise the product with -1
    product = -1
    si = len(factors)
  
    for i in range(si):
        for j in range(si):
            for k in range(si):
  
                # we find the sum of factors
                # and store it in s
                s = factors[i] + factors[j] + factors[k]
  
                # we check whether the fourth
                # factor exists or not
                if ((n - s) in factors):
                      
                    # product of factors
                    p = factors[i] * factors[j] * \
                        factors[k] * (n - s)
  
                    # we check whether we have a better
                    # p now if yes update
                    if (p > product):
                        product = p
  
    return product
  
# Driver code
n = 10
  
# initializes the vectors
# with the divisors of n
findFactors(n)
  
# sorts the factors vector
factors = sorted(factors)
  
# prints out the maximized product.
print(findProduct(n))
  
# This code is contributed by Mohit Kumar

C#

// C# implementation of above approach 
using System;
using System.Collections.Generic;
  
class GFG 
{
      
// Declare the ArrayList of factors for storing the 
static List< int> factors = new List();
  
// function to find out the factors of a number 
static void findFactors(int n)
{
    // we loop until the i reaches the sqrt(n) 
    for (int i = 1; i * i <= n; i++) 
    {
  
        // we check if i is a factor of n 
        if (n % i == 0) 
        {
  
            // if both the factors are same 
            // only push one factor 
            if ((n / i) == i) 
            {
                factors.Insert(factors.Count, i);
            } 
            else 
            {
  
                // factor1 is pushed 
                factors.Insert(factors.Count, n / i);
  
                // factor2 is pushed 
                factors.Insert(factors.Count, i);
            }
        }
    }
}
  
// Function to find the maximum product 
static int findProduct(int n) 
{
      
    // initialise the product with -1 
    int product = -1;
    int si = factors.Count;
  
    for (int i = 0; i < si; i++) 
    {
        for (int j = 0; j < si; j++)
        {
            for (int k = 0; k < si; k++) 
            {
  
                // we find the sum of factors 
                // and store it in s 
                int s = factors[i] + factors[j] + factors[k];
  
                // we check whether the fourth 
                // factor exists or not 
                if (factors.BinarySearch(n - s) >= 0)
                {
                    // product of factors 
                    int p = factors[i] * factors[j] * 
                            factors[k] * (n - s);
  
                    // we check whether we have a better 
                    // p now if yes update 
                    if (p > product) 
                    {
                        product = p;
                    }
                }
            }
        }
    }
    return product;
}
  
// Driver Code 
public static void Main(String[] args) 
{
    int n = 10;
  
    // intializes the vectors with the divisors of n 
    findFactors(n);
  
    // sorts the factors vector 
    factors.Sort();
  
    // prints out the maximised product. 
    Console.WriteLine(findProduct(n));
}
}
  
// This code is contributed by Princi Singh
输出:
20

时间复杂度: O((除数的数量)^ 3 * log(除数的数量))

高效方法:这是返回的最大乘积,直到前40个自然数。

如果我们仔细观察该模式,我们可以发现该模式在某些情况下是很常见的。
例如:

  1. 对于每个奇数,都没有解,因此返回-1。
  2. 小于4的数字没有解决方案。
  3. 对于每个可被4整除的数字,解决方案始终为(n / 4)^ 4形式。
    • 像4这样具有因子{1,2,4}解的数字是1并且(4/4)^ 4 = 1。
    • 具有因子{1,2,4,8}解的8之类的数字是16 ans(8/4)^ 4 = 16。
  4. 对于每个可被6整除的数字,解的形式始终为(n / 3)^ 2 *(n / 6)^ 2
    • 具有因子{1,2,3,6}解的6之类的数字是4,而(6/3)^ 2 *(6/6)^ 2 = 4。
    • 具有因子{1,2,3,6,9,18}解的像18这样的数字是324,而(18/3)^ 2 *(18/6)^ 2 = 324。
  5. 对于每个可被10整除的数字,解的形式始终为(n / 10)*(n / 5)^ 2 *(n / 2)
    • 像10这样具有{1,2,5,10}解的因子的数字是20,并且(10/10)*(10/5)^ 2 *(10/2)= 20。
    • 像20这样具有{1、2、5、10、25、50}解的因子的数字是12500,而(50/10)*(50/5)^ 2 *(50/2)= 12500。
  6. 对于其他每个偶数,都无法解决像14和22这样的数字。

注意:我们考虑将数字最先除法的最低因子,并且为了计算最大乘积,我们将忽略下一个连续的除数。例如,12可被4和6整除。但是,我们考虑了最小的计算因素,因此我们将4作为其乘积的除数。

下面是上述方法的实现:

C++

// C++ implementation of above approach
#include 
using namespace std;
  
// For calculation of a^b
int modExp(int a, int b)
{
    int result = 1;
    while (b > 0) {
        if (b & 1)
            result = result * a;
        a = a * a;
        b /= 2;
    }
  
    return result;
}
  
// Function to check
int check(int num)
{
    // every odd and number less than 3.
    if (num & 1 || num < 3)
        return -1;
  
    // every number divisible by 4.
    else if (num % 4 == 0)
        return modExp(num / 4, 4);
  
    // every number divisible by 6.
    else if (num % 6 == 0)
        return modExp(num / 3, 2) * modExp(num / 6, 2);
  
    // every number divisible by 10.
    else if (num % 10 == 0)
        return modExp(num / 5, 2) * (num / 10) * (num / 2);
  
    // for every even number which is not
    // divisible by above values.
    else
        return -1;
}
  
// Driver code
int main()
{
    int num = 10;
    cout << check(num);
  
    return 0;
}

Java

// Java implementation of above approach
import java.io.*;
import java.util.*;
import java.lang.*;
  
class GFG
{
// For calculation of a^b
static int modExp(int a, int b)
{
    int result = 1;
    while (b > 0) 
    {
        if (b == 1)
            result = result * a;
        a = a * a;
        b /= 2;
    }
  
    return result;
}
  
// Function to check
static int check(int num)
{
    // every odd and number less than 3.
    if (num == 1 || num < 3)
        return -1;
  
    // every number divisible by 4.
    else if (num % 4 == 0)
        return modExp(num / 4, 4);
  
    // every number divisible by 6.
    else if (num % 6 == 0)
        return modExp(num / 3, 2) * modExp(num / 6, 2);
  
    // every number divisible by 10.
    else if (num % 10 == 0)
        return modExp(num / 5, 2) * (num / 10) * (num / 2);
  
    // for every even number which is not
    // divisible by above values.
    else
        return -1;
}
  
// Driver code
public static void main(String[] args)
{
    int num = 10;
    System.out.print(check(num));
}
}
  
// This code is contributed 
// by Akanksha Rai(Abby_akku)

的Python 3

# Python3 implementation of above approach
  
  
# For calculation of a^b
def modExp( a, b):
  
    result = 1
    while (b > 0): 
        if (int(b) & 1):
            result = result * a
        a = a * a
        b /= 2
      
  
    return result
  
  
# Function to check
def check( num):
  
    # every odd and number less than 3.
    if (num & 1 or num < 3):
        return -1
  
    # every number divisible by 4.
    elif (num % 4 == 0):
        return modExp(num / 4, 4)
  
    # every number divisible by 6.
    elif (num % 6 == 0):
        return modExp(num / 3, 2) * modExp(num / 6, 2)
  
    # every number divisible by 10.
    elif (num % 10 == 0):
        return modExp(num / 5, 2) * (num / 10) * (num / 2)
  
    # for every even number which is not
    # divisible by above values.
    else:
        return -1
  
  
# Driver code
if __name__=='__main__':
    num = 10
    print(int(check(num)))
  
# This code is contributed by ash264

C#

// C#  implementation of above approach 
using System;
  
public class GFG{
    // For calculation of a^b 
static int modExp(int a, int b) 
{ 
    int result = 1; 
    while (b > 0) 
    { 
        if (b == 1) 
            result = result * a; 
        a = a * a; 
        b /= 2; 
    } 
  
    return result; 
} 
  
// Function to check 
static int check(int num) 
{ 
    // every odd and number less than 3. 
    if (num == 1 || num < 3) 
        return -1; 
  
    // every number divisible by 4. 
    else if (num % 4 == 0) 
        return modExp(num / 4, 4); 
  
    // every number divisible by 6. 
    else if (num % 6 == 0) 
        return modExp(num / 3, 2) * modExp(num / 6, 2); 
  
    // every number divisible by 10. 
    else if (num % 10 == 0) 
        return modExp(num / 5, 2) * (num / 10) * (num / 2); 
  
    // for every even number which is not 
    // divisible by above values. 
    else
        return -1; 
} 
  
// Driver code 
static public void Main (){
          
    int num = 10; 
    Console.WriteLine(check(num)); 
    } 
} 

的PHP

 0) 
    { 
        if ($b & 1) 
            $result = $result * $a; 
        $a = $a * $a; 
        $b /= 2; 
    } 
  
    return $result; 
} 
  
// Function to check 
function check($num) 
{ 
    // every odd and number less than 3. 
    if ($num & 1 || $num < 3) 
        return -1; 
  
    // every number divisible by 4. 
    else if ($num % 4 == 0) 
        return modExp($num / 4, 4); 
  
    // every number divisible by 6. 
    else if ($num % 6 == 0) 
        return modExp($num / 3, 2) * 
               modExp($num / 6, 2); 
  
    // every number divisible by 10. 
    else if ($num % 10 == 0) 
        return modExp($num / 5, 2) * 
                     ($num / 10) * ($num / 2); 
  
    // for every even number which is not 
    // divisible by above values. 
    else
        return -1; 
} 
  
// Driver code 
  
$num = 10; 
echo check($num); 
      
// This code is contributed 
// by Shivi_Aggarwal
?>
输出:
20

时间复杂度: O(log N)