📌  相关文章
📜  根据给定数组中连续元素的GCD构造一个数组

📅  最后修改于: 2021-04-24 16:43:20             🧑  作者: Mango

给定一个由n个元素组成的数组a [] 。任务是找到n + 1的数组(例如b []),以使b [i]和b [i + 1]的最大公约数等于a [i]。如果存在多个解决方案,请打印其数组总和最小的解决方案。

例子:

Input : a[] = { 1, 2, 3 }
Output : 1 2 6 3
GCD(1, 2) = 1
GCD(2, 6) = 2
GCD(6, 3) = 3
Also, 1 + 2 + 6 + 3 = 12 which is smallest among all
possible value of array that can be constructed.

Input : a[] = { 5, 10, 5 }
Output : 5 10 10 5

假设给定数组a []中只有一个数字。假设它为K,则构造数组中的两个数字(例如b [])将分别为K和K。

因此,b [0]的值将仅为a [0]。现在考虑到,我们完成了直到索引i的工作,即我们已经处理了直到索引i的工作并计算了b [i + 1]。

现在,gcd(b [i + 1],b [i + 2])= a [i +1]和gcd(b [i + 2],b [i + 3])= a [i + 2]。因此,b [i + 2]> = lcm(a [i + 1],a [i + 2])。或者,b [i + 2]将是lcm(a [i + 1],a [i + 2])的倍数。由于我们需要最小的总和,因此我们需要b [i + 2]的最小值。因此,b [i + 2] = 1cm(a [i + 2],a [i + 3])。

以下是此方法的实现:

C++
// CPP Program to construct an array whose GCD of
// every consecutive element is the given array
#include 
using namespace std;
  
// Return the LCM of two numbers.
int lcm(int a, int b)
{
    return (a * b) / __gcd(a, b);
}
  
// Print the required constructed array
void printArray(int a[], int n)
{
    // printing the first element.
    cout << a[0] << " ";
  
    // finding and printing the LCM of consecutive
    // element of given array.
    for (int i = 0; i < n - 1; i++)
        cout << lcm(a[i], a[i + 1]) << " ";
  
    // printing the last element of the given array.
    cout << a[n - 1] << endl;
}
  
// Driven Program
int main()
{
    int a[] = { 1, 2, 3 };
    int n = sizeof(a) / sizeof(a[0]);
    printArray(a, n);
    return 0;
}


Java
// Java Program to construct an array whose
// GCD of every consecutive element is the
// given array
  
import java.io.*;
  
class GFG {
      
    // Recursive function to return gcd of
    // a and b
    static int __gcd(int a, int b)
    {
          
        // Everything divides 0 
        if (a == 0 || b == 0)
        return 0;
      
        // base case
        if (a == b)
            return a;
      
        // a is greater
        if (a > b)
            return __gcd(a - b, b);
              
        return __gcd(a, b - a);
    }
  
    // Return the LCM of two numbers.
    static int lcm(int a, int b)
    {
        return (a * b) / __gcd(a, b);
    }
      
    // Print the required constructed array
    static void printArray(int a[], int n)
    {
          
        // printing the first element.
        System.out.print( a[0] + " ");
      
        // finding and printing the LCM of 
        // consecutive element of given array.
        for (int i = 0; i < n - 1; i++)
            System.out.print(lcm(a[i],
                            a[i + 1]) + " ");
      
        // printing the last element of the
        // given array.
        System.out.print(a[n - 1]);
    }
      
    // Driven Program
    public static void main (String[] args)
    {
        int a[] = { 1, 2, 3 };
        int n = a.length;
        printArray(a, n);
    }
}
  
// This code is contributed by anuj_67.


Python3
# Python Program to construct an array whose
# GCD of every consecutive element is the
# given array
  
# Recursive function to return gcd of
# a and b
def __gcd( a, b):
          
    # Everything divides 0 
    if (a == 0 or b == 0):
        return 0
          
    # base case
    if (a == b):
        return a 
          
    # a is greater
    if (a > b):
        return __gcd(a - b, b)         
    return __gcd(a, b - a)
      
# Return the LCM of two numbers.
def lcm(a, b):
    return (a * b) / __gcd(a, b)
  
# Print the required constructed array
def printArray(a, n):
          
    # printing the first element.
    print ( str(a[0]) + " ")
          
    # finding and printing the LCM of 
    # consecutive element of given array.
    for i in range(0,n-1):
        print (str(lcm(a[i],a[i + 1])) + " ")
          
    # printing the last element of the
    # given array.
    print (a[n - 1])
  
# Driver code
a = [1, 2, 3 ]
n = len(a)
printArray(a, n)
  
# This code is contributed by Prateek Bajaj


C#
// C# Program to construct an array whose
// GCD of every consecutive element is the
// given array
using System;
class GFG {
      
    // Recursive function to return
    // gcd of a and b
    static int __gcd(int a, int b)
    {
        // Everything divides 0 
        if (a == 0 || b == 0)
        return 0;
      
        // base case
        if (a == b)
            return a;
      
        // a is greater
        if (a > b)
            return __gcd(a - b, b);
              
        return __gcd(a, b - a);
    }
  
    // Return the LCM of two numbers.
    static int lcm(int a, int b)
    {
        return (a * b) / __gcd(a, b);
    }
      
    // Print the required constructed array
    static void printArray(int []a, int n)
    {
          
        // printing the first element.
        Console.Write( a[0] + " ");
      
        // finding and printing the LCM of 
        // consecutive element of given array.
        for (int i = 0; i < n - 1; i++)
            Console.Write(lcm(a[i],
                  a[i + 1]) + " ");
      
        // printing the last element 
        // of the given array.
        Console.Write(a[n - 1]);
    }
      
    // Driver Code
    public static void Main ()
    {
        int []a = {1, 2, 3};
        int n = a.Length;
        printArray(a, n);
    }
}
  
// This code is contributed by anuj_67.


PHP
 $b)
        return __gcd( $a - $b , $b ) ;
  
    return __gcd( $a , $b - $a ) ;
}
  
// Return the LCM of two numbers.
function lcm($a, $b)
{
    return ($a * $b) / __gcd($a, $b);
}
  
// Print the required constructed array
function printArray( $a, $n)
{
      
    // printing the first element.
    echo $a[0] , " ";
  
    // finding and printing 
    // the LCM of consecutive
    // element of given array.
    for ( $i = 0; $i < $n - 1; $i++)
        echo lcm($a[$i], $a[$i + 1]) , " ";
  
    // printing the last element
    // of the given array.
    echo $a[$n - 1] ,"\n";
}
  
    // Driver Code
    $a = array(1, 2, 3);
    $n = count($a);
    printArray($a, $n);
  
// This code is contributed by anuj_67.
?>


输出

1 2 6 3