📜  寻找移除GCD等于1的数组元素的游戏赢家

📅  最后修改于: 2021-05-14 01:51:28             🧑  作者: Mango

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

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

例子:

方法:播放器1和播放器2的最佳方法是始终删除大多数数组元素中至少具有一个公共质因数的数组元素。请按照以下步骤解决问题:

  • 初始化一个映射,例如mp ,这样mp [i]存储素数为i的数组元素的数量
  • 穿越地图,找到存在于地图的最大值,说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)