📌  相关文章
📜  找出 GCD 等于 1 的去除数组元素游戏的获胜者

📅  最后修改于: 2021-09-24 05:03:31             🧑  作者: Mango

给定一个大小为N的数组arr[] ,任务是在两个玩家按照以下规则进行最佳游戏时找到游戏的赢家:

  • 玩家 1开始游戏。
  • 在每一轮中,玩家从数组中删除一个元素。
  • 只有当所有由播放器1删除的元素的GCD等于1玩家2会赢得这场比赛。

例子:

方法:对于玩家 1 和玩家 2 来说,最佳方法是始终删除大多数数组元素中至少具有一个公共素因数的数组元素。请按照以下步骤解决问题:

  • 初始化一个 Map,比如mp ,这样mp[i]存储素数为i的数组元素的计数
  • 遍历地图并找到Map 中存在的最大值,例如maxCnt
  • 如果 N 为奇数且maxCnt等于N或 N 为偶数且maxCnt ≥ N – 1 ,则玩家 1赢得游戏。
  • 否则,玩家 2赢得比赛。

下面是上述方法的实现:

C++
// C++ program to implement
// the above approach
 
#include 
using namespace std;
 
// Function to find the winner of the game by
// removing array elements whose GCD is 1
void findWinnerGameRemoveGCD(int arr[], int n)
{
 
    // mp[i]: Stores count of array
    // elements whose prime factor is i
    unordered_map mp;
 
    // Traverse the array
    for (int i = 0; i < n; i++) {
 
        // Calculate the prime factors
        // of arr[i]
        for (int j = 2; j * j <= arr[i];
             j++) {
 
            // If arr[i] is divisible by j
            if (arr[i] % j == 0) {
 
                // Update mp[j]
                mp[j]++;
 
                while (arr[i] % j == 0) {
 
                    // Update arr[i]
                    arr[i] = arr[i] / j;
                }
            }
        }
 
        // If arr[i] exceeds 1
        if (arr[i] > 1) {
            mp[arr[i]]++;
        }
    }
 
    // Stores maximum value
    // present in the Map
    int maxCnt = 0;
 
    // Traverse the map
    for (auto i : mp) {
 
        // Update maxCnt
        maxCnt = max(maxCnt,
                     i.second);
    }
 
    // If n is an even number
    if (n % 2 == 0) {
 
        // If maxCnt is greater
        // than or equal to n-1
        if (maxCnt >= n - 1) {
 
            // Player 1 wins
            cout << "Player 1";
        }
        else {
 
            // Player 2 wins
            cout << "Player 2";
        }
    }
    else {
 
        // If maxCnt equal to n
        if (maxCnt == n) {
 
            // Player 1 wins
            cout << "Player 1";
        }
        else {
 
            // Player 2 wins
            cout << "Player 2";
        }
    }
}
 
// Driver Code
int main()
{
    int arr[] = { 2, 4, 8 };
    int N = sizeof(arr)
            / sizeof(arr[0]);
 
    findWinnerGameRemoveGCD(arr, N);
 
    return 0;
}


Java
// Java program to implement
// the above approach
import java.util.*;
 
class GFG{
 
  // Function to find the winner of the game by
  // removing array elements whose GCD is 1
  static void findWinnerGameRemoveGCD(int arr[], int n)
  {
 
    // mp[i]: Stores count of array
    // elements whose prime factor is i
    HashMap mp = new HashMap();
 
    // Traverse the array
    for (int i = 0; i < n; i++) {
 
      // Calculate the prime factors
      // of arr[i]
      for (int j = 2; j * j <= arr[i];
           j++) {
 
        // If arr[i] is divisible by j
        if (arr[i] % j == 0) {
 
          // Update mp[j]
          if (mp.containsKey(j))
          {
            mp.put(j, mp.get(j) + 1);
          }
          else
          {
            mp.put(j, 1);
          }
 
          while (arr[i] % j == 0) {
 
            // Update arr[i]
            arr[i] = arr[i] / j;
          }
        }
      }
 
      // If arr[i] exceeds 1
      if (arr[i] > 1) {
        if(mp.containsKey(arr[i]))
        {
          mp.put(arr[i], mp.get(arr[i]) + 1);
        }
        else
        {
          mp.put(arr[i], 1);
        }
      }
    }
 
    // Stores maximum value
    // present in the Map
    int maxCnt = 0;
 
    // Traverse the map
    for (Map.Entry i : mp.entrySet()) {
 
      // Update maxCnt
      maxCnt = Math.max(maxCnt, i.getValue());
    }
 
    // If n is an even number
    if (n % 2 == 0) {
 
      // If maxCnt is greater
      // than or equal to n-1
      if (maxCnt >= n - 1) {
 
        // Player 1 wins
        System.out.print("Player 1");
      }
      else {
 
        // Player 2 wins
        System.out.print("Player 2");
      }
    }
    else {
 
      // If maxCnt equal to n
      if (maxCnt == n) {
 
        // Player 1 wins
        System.out.print("Player 1");
      }
      else {
 
        // Player 2 wins
        System.out.print("Player 2");
      }
    }
  }
 
  // Driver code
  public static void main(String[] args)
  {
    int arr[] = { 2, 4, 8 };
    int N = arr.length;
 
    findWinnerGameRemoveGCD(arr, N);
  }
}
 
// This code is contributed by susmitakundugoaldanga


Python3
# Python3 program to implement
# the above approach
 
# Function to find the winner of the game by
# removing array elements whose GCD is 1
def findWinnerGameRemoveGCD(arr, n) :
 
    # mp[i]: Stores count of array
    # elements whose prime factor is i
    mp = dict.fromkeys(arr, 0);
 
    # Traverse the array
    for i in range(n) :
 
        # Calculate the prime factors
        # of arr[i]
        for j in range(2, int(arr[i] * (1/2)) + 1) :
             
            # If arr[i] is divisible by j
            if (arr[i] % j == 0) :
 
                # Update mp[j]
                mp[j] += 1;
 
                while (arr[i] % j == 0) :
 
                    # Update arr[i]
                    arr[i] = arr[i] // j;
 
        # If arr[i] exceeds 1
        if (arr[i] > 1) :
            mp[arr[i]] += 1;
 
    # Stores maximum value
    # present in the Map
    maxCnt = 0;
 
    # Traverse the map
    for i in mp:
 
        # Update maxCnt
        maxCnt = max(maxCnt,mp[i]);
 
    # If n is an even number
    if (n % 2 == 0) :
 
        # If maxCnt is greater
        # than or equal to n-1
        if (maxCnt >= n - 1) :
 
            # Player 1 wins
            print("Player 1",end="");
         
        else :
 
            # Player 2 wins
            print("Player 2",end="");
 
    else :
 
        # If maxCnt equal to n
        if (maxCnt == n) :
 
            # Player 1 wins
            print("Player 1",end="");
         
        else :
 
            # Player 2 wins
            print("Player 2",end="");
 
# Driver Code
if __name__ == "__main__" :
    arr = [ 2, 4, 8 ];
    N = len(arr);
     
    findWinnerGameRemoveGCD(arr, N);
     
    # This code is contributed by AnkThon


C#
// C# program to implement
// the above approach
using System;
using System.Collections.Generic;
 
class GFG{
 
  // Function to find the winner of the game by
  // removing array elements whose GCD is 1
  static void findWinnerGameRemoveGCD(int []arr, int n)
  {
 
    // mp[i]: Stores count of array
    // elements whose prime factor is i
    Dictionary mp = new Dictionary();
 
    // Traverse the array
    for (int i = 0; i < n; i++) {
 
      // Calculate the prime factors
      // of arr[i]
      for (int j = 2; j * j <= arr[i];
           j++) {
 
        // If arr[i] is divisible by j
        if (arr[i] % j == 0)
        {
 
          // Update mp[j]
          if (mp.ContainsKey(j))
          {
            mp[j] = mp[j] + 1;
          }
          else
          {
            mp.Add(j, 1);
          }
 
          while (arr[i] % j == 0)
          {
 
            // Update arr[i]
            arr[i] = arr[i] / j;
          }
        }
      }
 
      // If arr[i] exceeds 1
      if (arr[i] > 1)
      {
        if(mp.ContainsKey(arr[i]))
        {
          mp[arr[i]] =  mp[arr[i]] + 1;
        }
        else
        {
          mp.Add(arr[i], 1);
        }
      }
    }
 
    // Stores maximum value
    // present in the Map
    int maxCnt = 0;
 
    // Traverse the map
    foreach (KeyValuePair i in mp)
    {
 
      // Update maxCnt
      maxCnt = Math.Max(maxCnt, i.Value);
    }
 
    // If n is an even number
    if (n % 2 == 0)
    {
 
      // If maxCnt is greater
      // than or equal to n-1
      if (maxCnt >= n - 1)
      {
 
        // Player 1 wins
        Console.Write("Player 1");
      }
      else
      {
 
        // Player 2 wins
        Console.Write("Player 2");
      }
    }
    else
    {
 
      // If maxCnt equal to n
      if (maxCnt == n)
      {
 
        // Player 1 wins
        Console.Write("Player 1");
      }
      else
      {
 
        // Player 2 wins
        Console.Write("Player 2");
      }
    }
  }
 
  // Driver code
  public static void Main(String[] args)
  {
    int []arr = { 2, 4, 8 };
    int N = arr.Length;
 
    findWinnerGameRemoveGCD(arr, N);
  }
}
 
 
// This code is contributed by 29AjayKumar


输出:
Player 1

时间复杂度: (N * sqrt(X)),其中X是数组的最大元素
辅助空间: O(N)

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