📌  相关文章
📜  要与整数相乘以使每对 Array 成为完美正方形的元素计数

📅  最后修改于: 2021-09-07 02:08:53             🧑  作者: Mango

给定一个包含正整数的数组arr[] ,任务是找到要对数组执行的最小操作数,以使数组中的每个数字都成为超级大国。
在每个操作中,我们可以将数组的任何元素乘以一个整数。

例子:

方法:由于我们需要检查所有数字的质因数,所以想法是首先预先计算所有数字的唯一质因数并将其存储在hashmap中。然后,我们创建一个变量来存储素数在数组的每个元素中出现的次数。我们还需要观察,我们需要找到转换数组的最小步骤数。因此,我们计算向量中奇数的个数和偶数素因数的个数,以最小值为准。可以计算以下步骤来找到答案:

  1. 我们首先需要计算 spf[] 数组。这是存储所有元素的最小素因数的数组。
  2. 然后,我们需要找到唯一的质因数并将其存储在哈希图中。
  3. 现在,遍历哈希图以找到一个数的唯一质因数,并迭代给定数组中的元素以计算质因数的频率。
  4. 现在,两个变量被初始化,它们存储偶数出现的素数的频率,另一个存储奇数的素数的频率。
  5. 现在,我们需要在我们的最终变量中添加两个变量中的最小值,这是获得该素数超幂的最小操作。
  6. 对数组中的所有数字重复上述步骤。

下面是上述方法的实现:

C++
// C++ program to find the minimum
// number of steps to modify the
// array such that the product
// of any two numbers in the
// array is a perfect square
 
#include 
#include 
#include 
using namespace std;
 
// Function to find the smallest
// prime factor of the elements
void spf_array(int spf[])
{
 
    // Initializing the first element
    // of the array
    spf[1] = 1;
 
    // Loop to add the remaining
    // elements to the array
    for (int i = 2; i < 1000; i++)
 
        // Marking the smallest prime
        // factor for every
        // number to be itself
        spf[i] = i;
 
    // Separately marking spf for
    // every even number as 2
    for (int i = 4; i < 1000; i += 2)
        spf[i] = 2;
 
    for (int i = 3; i * i < 1000; i++) {
 
        // Checking if i is prime
        if (spf[i] == i) {
 
            // Marking SPF for all the
            // numbers divisible by i
            for (int j = i * i; j < 1000; j += i)
 
                // Marking spf[j] if it is not
                // previously marked
                if (spf[j] == j)
                    spf[j] = i;
        }
    }
}
 
// Function to find the minimum
// number of steps to modify the
// array such that the product
// of any two numbers in the
// array is a perfect square
int minimum_operation(int b[], int d,
                    int spf[])
{
 
    // Map created to store
    // the unique prime numbers
    unordered_map m;
    int i = 0;
 
    // Variable to store the
    // minimum number of operations
    int c = 0;
 
    // Loop to store every
    // unique prime number
    for (i = 0; i < d; i++) {
        int x = b[i];
        while (x != 1) {
            x = x / spf[x];
            if (m[spf[x]] == 0) {
                m[spf[x]] = 1;
            }
        }
    }
 
    // Erasing 1 as a key because
    // it is not a prime number
    m.erase(1);
 
    // Iterating through the hash
    for (auto x : m) {
 
        // Two variables used for
        // counting the frequency
        // of prime is even or odd
        int e = 0, o = 0;
 
        // First prime number
        int j = x.first;
 
        // Iterating the number D
        for (i = 0; i < d; i++) {
 
            // check if prime is a
            // factor of the element
            // in the array
            if (b[i] % j == 0) {
                int h = 0;
                int g = b[i];
 
                // Loop for calculating the
                // frequency of the element
                while (g != 0) {
                    if (g % j != 0) {
                        break;
                    }
                    g = g / j;
                    h = h + 1;
                }
 
                // Check for frequency
                // odd or even
                if (h % 2 == 0) {
                    e = e + 1;
                }
                else {
                    o = o + 1;
                }
            }
            else {
 
                // If it is not a factor of the
                // element, then it is automatically
                // even
                e = e + 1;
            }
        }
 
        // Storing the minimum of two variable
        // even or odd
        c = c + min(o, e);
    }
    return c;
}
 
// Driver code
int main()
{
    int spf[1001];
 
    // Input array
    int b[] = { 1, 4, 6 };
 
    // Creating shortest prime
    // factorisation array
    int d = sizeof(b) / sizeof(b[0]);
    spf_array(spf);
 
    cout << minimum_operation(b, d, spf)
        << endl;
}


Java
// Java program for the above approach
import java.io.*;
import java.util.*;
 
class GFG{
     
// Function to find the smallest
// prime factor of the elements
static void spf_array(int spf[])
{
 
    // Initializing the first element
    // of the array
    spf[1] = 1;
 
    // Loop to add the remaining
    // elements to the array
    for(int i = 2; i < 1000; i++)
 
        // Marking the smallest prime
        // factor for every
        // number to be itself
        spf[i] = i;
 
    // Separately marking spf for
    // every even number as 2
    for(int i = 4; i < 1000; i += 2)
        spf[i] = 2;
 
    for(int i = 3; i * i < 1000; i++)
    {
 
        // Checking if i is prime
        if (spf[i] == i)
        {
             
            // Marking SPF for all the
            // numbers divisible by i
            for(int j = i * i; j < 1000; j += i)
 
                // Marking spf[j] if it is not
                // previously marked
                if (spf[j] == j)
                    spf[j] = i;
        }
    }
}
 
// Function to find the minimum
// number of steps to modify the
// array such that the product
// of any two numbers in the
// array is a perfect square
static int minimum_operation(int b[], int d,
                             int spf[])
{
     
    // Map created to store
    // the unique prime numbers
    Map m=new HashMap<>();
    int i = 0;
 
    // Variable to store the
    // minimum number of operations
    int c = 0;
 
    // Loop to store every
    // unique prime number
    for(i = 0; i < d; i++)
    {
        int x = b[i];
        while (x != 1)
        {
            x = x / spf[x];
            if (m.get(spf[x]) == null)
            {
                m.put(spf[x],1);
            }
        }
    }
 
    // Erasing 1 as a key because
    // it is not a prime number
    m.remove(1);
 
    // Iterating through the hash
    for(Map.Entry x : m.entrySet())
    {
 
        // Two variables used for
        // counting the frequency
        // of prime is even or odd
        int e = 0, o = 0;
 
        // First prime number
        int j = x.getKey();
 
        // Iterating the number D
        for(i = 0; i < d; i++)
        {
 
            // Check if prime is a
            // factor of the element
            // in the array
            if (b[i] % j == 0)
            {
                int h = 0;
                int g = b[i];
 
                // Loop for calculating the
                // frequency of the element
                while (g != 0)
                {
                    if (g % j != 0)
                    {
                        break;
                    }
                    g = g / j;
                    h = h + 1;
                }
 
                // Check for frequency
                // odd or even
                if (h % 2 == 0)
                {
                    e = e + 1;
                }
                else
                {
                    o = o + 1;
                }
            }
            else
            {
 
                // If it is not a factor of the
                // element, then it is automatically
                // even
                e = e + 1;
            }
        }
 
        // Storing the minimum of two variable
        // even or odd
        c = c + Math.min(o, e);
    }
    return c;
}
 
// Driver Code
public static void main (String[] args)
{
    int[] spf = new int[1001];
     
    // Input array
    int b[] = { 1, 4, 6 };
     
    // Creating shortest prime
    // factorisation array
    int d = b.length;
    spf_array(spf);
     
    System.out.print(minimum_operation(b, d, spf));
}
}
 
// This code is contributed by offbeat


C#
// C# program for
// the above approach
using System;
using System.Collections.Generic;
class GFG{
     
// Function to find the smallest
// prime factor of the elements
static void spf_array(int []spf)
{
  // Initializing the first element
  // of the array
  spf[1] = 1;
 
  // Loop to add the remaining
  // elements to the array
  for(int i = 2; i < 1000; i++)
 
    // Marking the smallest prime
    // factor for every
    // number to be itself
    spf[i] = i;
 
  // Separately marking spf for
  // every even number as 2
  for(int i = 4; i < 1000; i += 2)
    spf[i] = 2;
 
  for(int i = 3; i * i < 1000; i++)
  {
    // Checking if i is prime
    if (spf[i] == i)
    {
      // Marking SPF for all the
      // numbers divisible by i
      for(int j = i * i; j < 1000; j += i)
 
        // Marking spf[j] if it is not
        // previously marked
        if (spf[j] == j)
          spf[j] = i;
    }
  }
}
 
// Function to find the minimum
// number of steps to modify the
// array such that the product
// of any two numbers in the
// array is a perfect square
static int minimum_operation(int []b,
                             int d, int []spf)
{
  // Map created to store
  // the unique prime numbers
  Dictionary m = new Dictionary();
  int i = 0;
 
  // Variable to store the
  // minimum number of operations
  int c = 0;
 
  // Loop to store every
  // unique prime number
  for(i = 0; i < d; i++)
  {
    int x = b[i];
    while (x != 1)
    {
      x = x / spf[x];
      if (!m.ContainsKey(spf[x]))
      {
        m.Add(spf[x], 1);
      }
    }
  }
 
  // Erasing 1 as a key because
  // it is not a prime number
  m.Remove(1);
 
  // Iterating through the hash
  foreach(KeyValuePair x in m)
  {
    // Two variables used for
    // counting the frequency
    // of prime is even or odd
    int e = 0, o = 0;
 
    // First prime number
    int j = x.Key;
 
    // Iterating the number D
    for(i = 0; i < d; i++)
    {
      // Check if prime is a
      // factor of the element
      // in the array
      if (b[i] % j == 0)
      {
        int h = 0;
        int g = b[i];
 
        // Loop for calculating the
        // frequency of the element
        while (g != 0)
        {
          if (g % j != 0)
          {
            break;
          }
          g = g / j;
          h = h + 1;
        }
 
        // Check for frequency
        // odd or even
        if (h % 2 == 0)
        {
          e = e + 1;
        }
        else
        {
          o = o + 1;
        }
      }
      else
      {
        // If it is not a factor of the
        // element, then it is automatically
        // even
        e = e + 1;
      }
    }
 
    // Storing the minimum of two variable
    // even or odd
    c = c + Math.Min(o, e);
  }
  return c;
}
 
// Driver Code
public static void Main(String[] args)
{
  int[] spf = new int[1001];
 
  // Input array
  int []b = {1, 4, 6};
 
  // Creating shortest prime
  // factorisation array
  int d = b.Length;
  spf_array(spf);
 
  Console.Write(minimum_operation(b, d, spf));
}
}
 
// This code is contributed by shikhasingrajput


Javascript


输出:
2

时间复杂度: O(N * log(N)) ,其中 N 是输入数组的大小。

如果您想与行业专家一起参加直播课程,请参阅Geeks Classes Live