📌  相关文章
📜  找出第一个数组的倍数和第二个数组的因数

📅  最后修改于: 2021-10-26 06:31:11             🧑  作者: Mango

给定两个数组A[]B[] ,任务是找到可以被数组A[]的所有元素整除的整数,然后将数组B[]的所有元素相除。

例子:

方法:如果X是第一个数组的所有元素的倍数,那么X必须是第一个数组的所有元素的 LCM 的倍数。
类似地,如果X是第二个数组所有元素的因数,那么它必须是第二个数组所有元素的 GCD 的因数,并且只有当第二个数组的 GCD 可被 LCM 整除时,这样的X才会存在第一个数组。
如果它是可整除的,那么X可以是范围[LCM, GCD] 中的任何值它是 LCM 的倍数并均匀划分 GCD。

下面是上述方法的实现:

C++
// C++ implementation of the approach
#include 
using namespace std;
 
// Function to return the LCM of two numbers
int lcm(int x, int y)
{
    int temp = (x * y) / __gcd(x, y);
    return temp;
}
 
// Function to print the required numbers
void findNumbers(int a[], int n, int b[], int m)
{
 
    // To store the lcm of array a[] elements
    // and the gcd of array b[] elements
    int lcmA = 1, gcdB = 0;
 
    // Finding LCM of first array
    for (int i = 0; i < n; i++)
        lcmA = lcm(lcmA, a[i]);
 
    // Finding GCD of second array
    for (int i = 0; i < m; i++)
        gcdB = __gcd(gcdB, b[i]);
 
    // No such element exists
    if (gcdB % lcmA != 0) {
        cout << "-1";
        return;
    }
 
    // All the multiples of lcmA which are
    // less than or equal to gcdB and evenly
    // divide gcdB will satisfy the conditions
    int num = lcmA;
    while (num <= gcdB) {
        if (gcdB % num == 0)
            cout << num << " ";
        num += lcmA;
    }
}
 
// Driver code
int main()
{
 
    int a[] = { 1, 2, 2, 4 };
    int b[] = { 16, 32, 64 };
 
    int n = sizeof(a) / sizeof(a[0]);
    int m = sizeof(b) / sizeof(b[0]);
 
    findNumbers(a, n, b, m);
 
    return 0;
}


Java
// Java implementation of the approach
import java.util.*;
 
class GFG
{
static int __gcd(int a, int b)
{
    if (b == 0)
        return a;
    return __gcd(b, a % b);
     
}
 
// Function to return the LCM of two numbers
static int lcm(int x, int y)
{
    int temp = (x * y) / __gcd(x, y);
    return temp;
}
 
// Function to print the required numbers
static void findNumbers(int a[], int n,
                        int b[], int m)
{
 
    // To store the lcm of array a[] elements
    // and the gcd of array b[] elements
    int lcmA = 1, gcdB = 0;
 
    // Finding LCM of first array
    for (int i = 0; i < n; i++)
        lcmA = lcm(lcmA, a[i]);
 
    // Finding GCD of second array
    for (int i = 0; i < m; i++)
        gcdB = __gcd(gcdB, b[i]);
 
    // No such element exists
    if (gcdB % lcmA != 0)
    {
        System.out.print("-1");
        return;
    }
 
    // All the multiples of lcmA which are
    // less than or equal to gcdB and evenly
    // divide gcdB will satisfy the conditions
    int num = lcmA;
    while (num <= gcdB)
    {
        if (gcdB % num == 0)
            System.out.print(num + " ");
        num += lcmA;
    }
}
 
// Driver code
public static void main(String[] args)
{
    int a[] = { 1, 2, 2, 4 };
    int b[] = { 16, 32, 64 };
 
    int n = a.length;
    int m = b.length;
 
    findNumbers(a, n, b, m);
}
}
 
// This code is contributed by 29AjayKumar


Python3
# Python3 implementation of the approach
from math import gcd
 
# Function to return the LCM of two numbers
def lcm( x, y) :
     
    temp = (x * y) // gcd(x, y);
    return temp;
 
# Function to print the required numbers
def findNumbers(a, n, b, m) :
 
    # To store the lcm of array a[] elements
    # and the gcd of array b[] elements
    lcmA = 1; __gcdB = 0;
 
    # Finding LCM of first array
    for i in range(n) :
        lcmA = lcm(lcmA, a[i]);
 
    # Finding GCD of second array
    for i in range(m) :
        __gcdB = gcd(__gcdB, b[i]);
 
    # No such element exists
    if (__gcdB % lcmA != 0) :
        print("-1");
        return;
 
    # All the multiples of lcmA which are
    # less than or equal to gcdB and evenly
    # divide gcdB will satisfy the conditions
    num = lcmA;
    while (num <= __gcdB) :
        if (__gcdB % num == 0) :
            print(num, end = " ");
             
        num += lcmA;
 
# Driver code
if __name__ == "__main__" :
 
    a = [ 1, 2, 2, 4 ];
    b = [ 16, 32, 64 ];
     
    n = len(a);
    m = len(b);
     
    findNumbers(a, n, b, m);
     
# This code is contributed by AnkitRai01


C#
// C# implementation of the approach
using System;
 
class GFG
{
static int __gcd(int a, int b)
{
    if (b == 0)
        return a;
    return __gcd(b, a % b);
}
 
// Function to return the LCM of two numbers
static int lcm(int x, int y)
{
    int temp = (x * y) / __gcd(x, y);
    return temp;
}
 
// Function to print the required numbers
static void findNumbers(int []a, int n,
                        int []b, int m)
{
 
    // To store the lcm of array a[] elements
    // and the gcd of array b[] elements
    int lcmA = 1, gcdB = 0;
 
    // Finding LCM of first array
    for (int i = 0; i < n; i++)
        lcmA = lcm(lcmA, a[i]);
 
    // Finding GCD of second array
    for (int i = 0; i < m; i++)
        gcdB = __gcd(gcdB, b[i]);
 
    // No such element exists
    if (gcdB % lcmA != 0)
    {
        Console.Write("-1");
        return;
    }
 
    // All the multiples of lcmA which are
    // less than or equal to gcdB and evenly
    // divide gcdB will satisfy the conditions
    int num = lcmA;
    while (num <= gcdB)
    {
        if (gcdB % num == 0)
            Console.Write(num + " ");
        num += lcmA;
    }
}
 
// Driver code
public static void Main(String[] args)
{
    int []a = { 1, 2, 2, 4 };
    int []b = { 16, 32, 64 };
 
    int n = a.Length;
    int m = b.Length;
 
    findNumbers(a, n, b, m);
}
}
 
// This code is contributed by 29AjayKumar


Javascript


输出:
4 8 16

如果您希望与专家一起参加现场课程,请参阅DSA 现场工作专业课程学生竞争性编程现场课程