📜  数组的 GCD 大于一

📅  最后修改于: 2022-05-13 01:57:52.737000             🧑  作者: Mango

数组的 GCD 大于一

给定一个包含 n 个整数的数组。如果所有元素的 GCD 都大于 1,则认为该数组是最好的。如果该数组不是最好的,我们可以选择索引 i (1 <= i < n) 并将数字 a i和 a i + 1替换为 a i – 分别为 a i + 1和 a i + a i + 1 。找到要对数组执行的最小操作数以使其最佳。
例子:

Input : n = 2
        a[] = [1, 1]
Output : 1
Explanation:
Here, gcd(1,1) = 1. So, to make 
it best we have to replace array 
by [(1-1), (1+1)] = [0, 2]. Now, 
gcd(0, 2) > 1. Hence, in one
operation array become best.

Input : n = 3
        a[] = [6, 2, 4]
Output :0
Explanation:
Here, gcd(6,2,4) > 1.
Hence, no operation is required. 

我们首先计算 gcd(array) 并检查它是否大于 1。如果是,则该数组已经是最好的,否则我们贪婪地检查是否否。通过使用以下属性使所有的移动都需要移动:当有两个奇数时,您可以在一个移动中使它们成为偶数,否则如果有一个奇数和一个偶数,那么您需要两个移动。
下面是上述方法的实现:

C++
// CPP program to find bestArray
#include
using namespace std;
 
// Calculating gcd of two numbers
int gcd(int a, int b){
        if (a == 0)
            return b;
        return gcd(b%a, a);
}
 
void bestArray(int arr[], int n){
        bool even[n] = {false};
        int ans = 0;
 
        // calculating gcd and
        // counting the even numbers
        for(int i = 0; i < n; i++){
            ans = gcd(ans, arr[i]);
            if(arr[i] % 2 == 0)
                even[i] = true;
        }
 
        // check array is already best
        if(ans > 1)
            cout << 0 << endl;
        else{
 
            // counting the number
            // of operations required.
            ans = 0;
            for(int i = 0; i < n-1; i++){
                if(!even[i]){
                    even[i] = true;
                    even[i+1] = true;
                    if(arr[i+1]%2 != 0){
                        ans+=1;
                    }
                    else
                        ans+=2;
                }
            }
            if(!even[n-1]){
                ans+=2;
            }
            cout << ans << endl;
        }
}
 
// driver code
int main(){
     
        int arr[] = {57, 30, 28, 81, 88, 32, 3, 42, 25};
        int n = 9;
        bestArray(arr, n);
 
        int arr1[] = {1, 1};
        n = 2;
        bestArray(arr1, n);
 
        int arr2[] = {6, 2, 4};
        n = 3;
        bestArray(arr2, n);
}
 
/*This code is contributed by Sagar Shukla.*/


Java
// Java code to find best array
import java.util.*;
import java.lang.*;
 
public class GeeksforGeeks{
     
    // function to calculate gcd of two numbers.
    public static int gcd(int a, int b){
        if (a == 0)
            return b;
        return gcd(b%a, a);
    }
 
    public static void bestArray(int arr[], int n){
        boolean even[] = new boolean[n];
        int ans = 0;
        for(int i=0; i 1)
            System.out.println(0);
        else{
             
            // counting the number of operations required.
            ans = 0;
            for(int i=0; i


Python
# code to find the best array
from fractions import gcd
 
def bestArray(a,n):
 
    even = [False]*n
    ans = 0
 
    # calculating the gcd and
    # counting the even numbers
    for i in xrange(n):
        ans = gcd(ans, a[i])
        if a[i]%2 == 0:
            even[i] = True
 
    # check if array is already best.
    if ans > 1:
        print (0)
    else:      
 
        # calculating the no of
        # operations required.
        ans = 0
        for i in xrange(n-1):
            if not even[i]:
                even[i] = True
                even[i+1] = True
                if a[i+1]%2 != 0:
                    ans += 1
                else:
                    ans += 2
        if not even[n-1]:
            ans += 2
        print (ans)
 
# driver code
n = 9
a = [57, 30, 28, 81, 88, 32, 3, 42, 25]
bestArray(a,n)
n = 2
a = [1, 1]
bestArray(a,n)
n = 3
a = [6, 2, 4]
bestArray(a,n)


C#
// C# code to find best array
using System;
 
public class GFG {
     
    // function to calculate gcd
    // of two numbers.
    public static int gcd(int a, int b)
    {
        if (a == 0)
            return b;
             
        return gcd(b % a, a);
    }
 
    public static void bestArray(int []arr, int n)
    {
        bool []even = new bool[n];
        int ans = 0;
         
        for(int i = 0; i < n; i++)
            even[i] = false;
 
        // calculating gcd and
        // counting the even numbers
        for(int i = 0; i < n; i++)
        {
            ans = gcd(ans, arr[i]);
             
            if(arr[i] % 2 == 0)
                even[i] = true;
        }
         
        // check array is already best
        if(ans > 1)
            Console.WriteLine(0);
        else
        {
             
            // counting the number of
            // operations required.
            ans = 0;
            for(int i = 0; i < n-1; i++)
            {
                if(!even[i])
                {
                    even[i] = true;
                    even[i+1] = true;
                    if(arr[i+1] % 2 != 0)
                        ans += 1;
                    else
                        ans += 2;
                }
            }
            if(!even[n-1])
                ans += 2;
                 
            Console.WriteLine(ans);
        }
    }
     
    // driver code
    public static void Main()
    {
         
        int []arr = {57, 30, 28, 81, 88,
                            32, 3, 42, 25};
        int n = 9;
        bestArray(arr, n);
         
        int []arr1 = {1, 1};
        n = 2;
        bestArray(arr1, n);
         
        int []arr2 = {6, 2, 4};
        n = 3;
        bestArray(arr2, n);
    }
}
 
// This code is contributed by vt_m.


Javascript


输出:

8
1
0