📌  相关文章
📜  根据更多的除数找到游戏的获胜者

📅  最后修改于: 2021-04-29 01:09:03             🧑  作者: Mango

给定两个数组arr1 []arr2 [] ,玩家Aarr1 []中选择一个元素,玩家Barr2 []中选择一个元素,该元素具有除数更多的元素的玩家将赢得本轮比赛。如果两个元素的除数均相同,则玩家A赢得该回合。任务是确定玩家A或玩家B赢得游戏的可能性更大。
例子:

方法:

  • 对于这两个数组,代替元素,存储元素的除数的数量。
  • 将两个数组按升序排序。
  • 找到A赢得游戏的所有可能的对选择(X,Y)
  • 假设Aarr1 []中选择一个元素。现在,可以使用二进制搜索来查找arr2 []中的元素数,该元素的除数小于arr1 []中的所选元素。这将被添加到A获胜的对数中。对arr1 []的每个元素都执行此操作。
  • N * M是可能的总数对。 A获胜的对数为XB获胜的对数为Y。
    • 如果X> Y,A更有可能赢得比赛。
    • 如果Y> X,B具有更大的获胜概率。
    • 如果X = Y,则抽奖的可能性更大。

下面是上述方法的实现:

C++
// C++ implementation of the approach
#include 
using namespace std;
 
#define ll long long int
 
// Function to return the count
// of divisors of elem
int divisorcount(int elem)
{
    int ans = 0;
    for (int i = 1; i <= sqrt(elem); i++) {
        if (elem % i == 0) {
            if (i * i == elem)
                ans++;
            else
                ans += 2;
        }
    }
 
    return ans;
}
 
// Function to return the winner of the game
string findwinner(int A[], int B[], int N, int M)
{
    // Convert every element of A[]
    // to their divisor count
    for (int i = 0; i < N; i++) {
        A[i] = divisorcount(A[i]);
    }
 
    // Convert every element of B[]
    // to their divisor count
    for (int i = 0; i < M; i++) {
        B[i] = divisorcount(B[i]);
    }
 
    // Sort both the arrays
    sort(A, A + N);
    sort(B, B + M);
 
    int winA = 0;
    for (int i = 0; i < N; i++) {
        int val = A[i];
        int start = 0;
        int end = M - 1;
        int index = -1;
 
        // For every element of A apply Binary Search
        // to find number of pairs where A wins
        while (start <= end) {
            int mid = (start + end) / 2;
            if (B[mid] <= val) {
                index = mid;
                start = mid + 1;
            }
            else {
                end = mid - 1;
            }
        }
 
        winA += (index + 1);
    }
 
    // B wins if A doesnot win
    int winB = N * M - winA;
 
    if (winA > winB) {
        return "A";
    }
    else if (winB > winA) {
        return "B";
    }
 
    return "Draw";
}
 
// Driver code
int main()
{
    int A[] = { 4, 12, 24 };
    int N = sizeof(A) / sizeof(A[0]);
 
    int B[] = { 25, 28, 13, 45 };
    int M = sizeof(B) / sizeof(B[0]);
 
    cout << findwinner(A, B, N, M);
 
    return 0;
}


Java
// Java implementation of the
// above approach
import java.util.*;
class GFG{
 
// Function to return the count
// of divisors of elem
static int divisorcount(int elem)
{
  int ans = 0;
  for (int i = 1;
           i <= Math.sqrt(elem); i++)
  {
    if (elem % i == 0)
    {
      if (i * i == elem)
        ans++;
      else
        ans += 2;
    }
  }
 
  return ans;
}
 
// Function to return the
// winner of the game
static String findwinner(int A[], int B[],
                         int N, int M)
{
  // Convert every element of A[]
  // to their divisor count
  for (int i = 0; i < N; i++)
  {
    A[i] = divisorcount(A[i]);
  }
 
  // Convert every element of B[]
  // to their divisor count
  for (int i = 0; i < M; i++)
  {
    B[i] = divisorcount(B[i]);
  }
 
  // Sort both the arrays
  Arrays.sort(A);
  Arrays.sort(B);
 
  int winA = 0;
  for (int i = 0; i < N; i++)
  {
    int val = A[i];
    int start = 0;
    int end = M - 1;
    int index = -1;
 
    // For every element of A
    // apply Binary Search to
    // find number of pairs
    // where A wins
    while (start <= end)
    {
      int mid = (start +
                 end) / 2;
      if (B[mid] <= val)
      {
        index = mid;
        start = mid + 1;
      }
      else
      {
        end = mid - 1;
      }
    }
 
    winA += (index + 1);
  }
 
  // B wins if A doesnot
  // win
  int winB = N * M - winA;
 
  if (winA > winB)
  {
    return "A";
  }
  else if (winB > winA)
  {
    return "B";
  }
 
  return "Draw";
}
 
// Driver code
public static void main(String[] args)
{
  int A[] = {4, 12, 24};
  int N = A.length;
 
  int B[] = {25, 28, 13, 45};
  int M = B.length;
 
  System.out.print(findwinner(A, B, N, M));
}
}
 
// This code is contributed by Chitranayal


Python3
# Python3 implementation of the approach
from math import *
 
# Function to return the count
# of divisors of elem
def divisorcount(elem):
 
    ans = 0
    for i in range(1, int(sqrt(elem)) + 1):
        if (elem % i == 0):
            if (i * i == elem):
                ans += 1
            else:
                ans += 2
 
    return ans
 
# Function to return the winner of the game
def findwinner(A, B, N, M):
 
    # Convert every element of A[]
    # to their divisor count
    for i in range(N):
        A[i] = divisorcount(A[i])
 
    # Convert every element of B[]
    # to their divisor count
    for i in range(M):
        B[i] = divisorcount(B[i])
 
    # Sort both the arrays
    A.sort()
    B.sort()
     
    winA = 0
    for i in range(N):
        val = A[i]
        start = 0
        end = M - 1
        index = -1
         
        # For every element of A[] apply
        # binary search to find number
        # of pairs where A wins
        while (start <= end):
            mid = (start + end) // 2
             
            if (B[mid] <= val):
                index = mid
                start = mid + 1
                 
            else:
                end = mid - 1
         
        winA += (index + 1)
     
    # B wins if A doesnot win
    winB = N * M - winA
 
    if (winA > winB):
        return "A"
    elif (winB > winA):
        return "B"
 
    return "Draw"
 
# Driver code
if __name__ == '__main__':
         
    A = [ 4, 12, 24 ]
    N = len(A)
 
    B = [ 25, 28, 13, 45 ]
    M = len(B)
 
    print(findwinner(A, B, N, M))
 
# This code is contributed by himanshu77


输出:
A