📜  最小的完美多维数据集可被数组的所有元素整除

📅  最后修改于: 2021-04-22 07:59:41             🧑  作者: Mango

给定数组arr [] ,任务是找到可被给定数组的所有元素整除的最小的完美立方体。
例子:

天真的方法:从1开始一个接一个地检查所有理想的多维数据集,然后选择一个可以被数组中所有元素整除的多维数据集。
高效方法:查找数组所有元素的最小公倍数,并将其存储在变量lcm中。查找找到的LCM的所有素数。
现在,对于将lcm’x’次数除以x%3!= 0的每个素因子事实

  • 如果x%3 = 2,则更新lcm = lcm *事实
  • 如果x%3 = 1,则更新lcm = lcm *事实2

最后打印更新的LCM
下面是上述方法的实现:

C++
// C++ implementation of the approach
#include 
using namespace std;
 
#define ll long long int
 
// Function to return the gcd of two numbers
ll gcd(ll a, ll b)
{
    if (b == 0)
        return a;
    else
        return gcd(b, a % b);
}
 
// Function to return the lcm of
// all the elements of the array
ll lcmOfArray(int arr[], int n)
{
    if (n < 1)
        return 0;
 
    ll lcm = arr[0];
 
    // To calculate lcm of two numbers
    // multiply them and divide the result
    // by gcd of both the numbers
    for (int i = 1; i < n; i++)
        lcm = (lcm * arr[i]) / gcd(lcm, arr[i]);
 
    // Return the LCM of the array elements
    return lcm;
}
 
// Function to return the smallest perfect cube
// divisible by all the elements of arr[]
int minPerfectCube(int arr[], int n)
{
    ll minPerfectCube;
 
    // LCM of all the elements of arr[]
    ll lcm = lcmOfArray(arr, n);
    minPerfectCube = (long long)lcm;
 
    int cnt = 0;
    while (lcm > 1 && lcm % 2 == 0) {
        cnt++;
        lcm /= 2;
    }
 
    // If 2 divides lcm cnt number of times
    if (cnt % 3 == 2)
        minPerfectCube *= 2;
    else if (cnt % 3 == 1)
        minPerfectCube *= 4;
 
    int i = 3;
 
    // Check all the numbers that divide lcm
    while (lcm > 1) {
        cnt = 0;
        while (lcm % i == 0) {
            cnt++;
            lcm /= i;
        }
 
        if (cnt % 3 == 1)
            minPerfectCube *= i * i;
        else if (cnt % 3 == 2)
            minPerfectCube *= i;
 
        i += 2;
    }
 
    // Return the answer
    return minPerfectCube;
}
 
// Driver code
int main()
{
    int arr[] = { 10, 125, 14, 42, 100 };
    int n = sizeof(arr) / sizeof(arr[0]);
    cout << minPerfectCube(arr, n);
 
    return 0;
}


Java
// Java implementation of the approach
import java.util.*;
 
class GFG
{
 
// Function to return the gcd of two numbers
static int gcd(int a, int b)
{
    if (b == 0)
        return a;
    else
        return gcd(b, a % b);
}
 
// Function to return the lcm of
// aint the elements of the array
static int lcmOfArray(int arr[], int n)
{
    if (n < 1)
        return 0;
 
    int lcm = arr[0];
 
    // To calculate lcm of two numbers
    // multiply them and divide the result
    // by gcd of both the numbers
    for (int i = 1; i < n; i++)
        lcm = (lcm * arr[i]) / gcd(lcm, arr[i]);
 
    // Return the LCM of the array elements
    return lcm;
}
 
// Function to return the smaintest perfect cube
// divisible by aint the elements of arr[]
static int minPerfectCube(int arr[], int n)
{
    int minPerfectCube;
 
    // LCM of all the elements of arr[]
    int lcm = lcmOfArray(arr, n);
    minPerfectCube = lcm;
 
    int cnt = 0;
    while (lcm > 1 && lcm % 2 == 0)
    {
        cnt++;
        lcm /= 2;
    }
 
    // If 2 divides lcm cnt number of times
    if (cnt % 3 == 2)
        minPerfectCube *= 2;
    else if (cnt % 3 == 1)
        minPerfectCube *= 4;
 
    int i = 3;
 
    // Check aint the numbers that divide lcm
    while (lcm > 1)
    {
        cnt = 0;
        while (lcm % i == 0)
        {
            cnt++;
            lcm /= i;
        }
 
        if (cnt % 3 == 1)
            minPerfectCube *= i * i;
        else if (cnt % 3 == 2)
            minPerfectCube *= i;
 
        i += 2;
    }
 
    // Return the answer
    return minPerfectCube;
}
 
// Driver code
public static void main(String args[])
{
    int arr[] = { 10, 125, 14, 42, 100 };
    int n = arr.length;
    System.out.println(minPerfectCube(arr, n));
}
}
 
// This code is contributed by
// Surendra_Gangwar


Python3
# Python3 implementation of the approach
 
# Function to return the gcd of two numbers
def gcd(a, b) :
     
    if (b == 0) :
        return a
    else :
        return gcd(b, a % b)
 
# Function to return the lcm of
# all the elements of the array
def lcmOfArray(arr, n) :
     
    if (n < 1) :
        return 0
 
    lcm = arr[0]
 
    # To calculate lcm of two numbers
    # multiply them and divide the result
    # by gcd of both the numbers
    for i in range(n) :
        lcm = (lcm * arr[i]) // gcd(lcm, arr[i]);
 
    # Return the LCM of the array elements
    return lcm
 
# Function to return the smallest perfect cube
# divisible by all the elements of arr[]
def minPerfectCube(arr, n) :
     
    # LCM of all the elements of arr[]
    lcm = lcmOfArray(arr, n)
    minPerfectCube = lcm
 
    cnt = 0
    while (lcm > 1 and lcm % 2 == 0) :
        cnt += 1
        lcm //= 2
     
    # If 2 divides lcm cnt number of times
    if (cnt % 3 == 2) :
        minPerfectCube *= 2
         
    elif (cnt % 3 == 1) :
        minPerfectCube *= 4
 
    i = 3
     
    # Check all the numbers that divide lcm
    while (lcm > 1) :
        cnt = 0
         
        while (lcm % i == 0) :
            cnt += 1
            lcm //= i
         
        if (cnt % 3 == 1) :
            minPerfectCube *= i * i
             
        elif (cnt % 3 == 2) :
            minPerfectCube *= i
 
        i += 2
 
    # Return the answer
    return minPerfectCube
 
# Driver code
if __name__ == "__main__" :
 
    arr = [ 10, 125, 14, 42, 100 ]
     
    n = len(arr)
    print(minPerfectCube(arr, n))
     
# This code is contributed by Ryuga


C#
// C# implementation of the approach
using System;
 
class GFG
{
 
// Function to return the gcd of two numbers
static int gcd(int a, int b)
{
    if (b == 0)
        return a;
    else
        return gcd(b, a % b);
}
 
// Function to return the lcm of
// aint the elements of the array
static int lcmOfArray(int []arr, int n)
{
    if (n < 1)
        return 0;
 
    int lcm = arr[0];
 
    // To calculate lcm of two numbers
    // multiply them and divide the result
    // by gcd of both the numbers
    for (int i = 1; i < n; i++)
        lcm = (lcm * arr[i]) / gcd(lcm, arr[i]);
 
    // Return the LCM of the array elements
    return lcm;
}
 
// Function to return the smaintest perfect cube
// divisible by aint the elements of arr[]
static int minPerfectCube(int []arr, int n)
{
    int minPerfectCube;
 
    // LCM of all the elements of arr[]
    int lcm = lcmOfArray(arr, n);
    minPerfectCube = lcm;
 
    int cnt = 0;
    while (lcm > 1 && lcm % 2 == 0)
    {
        cnt++;
        lcm /= 2;
    }
 
    // If 2 divides lcm cnt number of times
    if (cnt % 3 == 2)
        minPerfectCube *= 2;
    else if (cnt % 3 == 1)
        minPerfectCube *= 4;
 
    int i = 3;
 
    // Check aint the numbers that divide lcm
    while (lcm > 1)
    {
        cnt = 0;
        while (lcm % i == 0)
        {
            cnt++;
            lcm /= i;
        }
 
        if (cnt % 3 == 1)
            minPerfectCube *= i * i;
        else if (cnt % 3 == 2)
            minPerfectCube *= i;
 
        i += 2;
    }
 
    // Return the answer
    return minPerfectCube;
}
 
// Driver code
public static void Main()
{
    int []arr = { 10, 125, 14, 42, 100 };
    int n = arr.Length;
    Console.WriteLine(minPerfectCube(arr, n));
}
}
 
// This code is contributed by chandan_jnu


PHP
 1 && $lcm % 2 == 0)
    {
        $cnt++;
        $lcm /= 2;
    }
 
    // If 2 divides lcm cnt number of times
    if ($cnt % 3 == 2)
        $minPerfectCube *= 2;
    else if ($cnt % 3 == 1)
        $minPerfectCube *= 4;
 
    $i = 3;
 
    // Check all the numbers that divide lcm
    while ($lcm > 1)
    {
        $cnt = 0;
        while ($lcm % $i == 0)
        {
            $cnt++;
            $lcm /= $i;
        }
 
        if ($cnt % 3 == 1)
            $minPerfectCube *= $i * $i;
        else if ($cnt % 3 == 2)
            $minPerfectCube *= $i;
 
        $i += 2;
    }
 
    // Return the answer
    return $minPerfectCube;
}
 
// Driver code
$arr = array(10, 125, 14, 42, 100 );
$n = sizeof($arr);
echo(minPerfectCube($arr, $n));
 
// This code is contributed by Shivi_Aggarwal
?>


Javascript


输出:
9261000

时间复杂度: O(n * log(arr [i])

辅助空间: O(1)