给定一个由不同的正整数组成的数组arr[] ,两个玩家A和B正在玩游戏。在每一步,玩家从数组中选择两个数字x和y ,如果|x – y|不存在于数组中,则玩家将此数字添加到数组中(数组的大小增加 1)。不能移动的玩家输掉游戏。如果玩家A总是开始游戏,则任务是找到游戏的获胜者。
例子:
Input: arr[] = {2, 3}
Output: A
After A’s move, array will be {2, 3, 1} and B can’t make any move.
Input: arr[] = {5, 6, 7}
Output: B
方法:在这里观察到,在游戏结束时(当没有更多动作时),结果数组将包含原始数组的 gcd 的所有倍数,直到原始数组的最大元素。
For example, arr[] = {8, 10}
Since, gcd(8, 10) = 2. So the resultant array at the end of the game will contain all the multiples of 2 ≤ max(arr) i.e. 10.
Hence, arr[] = {2, 4, 6, 8, 10}
从上面的观察,可以找到可以在原始数组上执行的移动次数,这将决定游戏的获胜者,如果移动次数为偶数,则B将成为游戏的胜利者,否则A赢得游戏.
移动次数可以通过(max(arr) / gcd) – n 找到,其中gcd是原始数组元素的 gcd, max(arr) / gcd给出结果数组中元素的总数。从结果数组中的元素计数中减去原始元素计数将给出移动次数。
下面是上述方法的实现:
C++
// C++ implementation of the approach
#include
using namespace std;
// Function to return the winner of the game
char getWinner(int arr[], int n)
{
// To store the gcd of the original array
int gcd = arr[0];
// To store the maximum element
// from the original array
int maxEle = arr[0];
for (int i = 1; i < n; i++) {
gcd = __gcd(gcd, arr[i]);
maxEle = max(maxEle, arr[i]);
}
int totalMoves = (maxEle / gcd) - n;
// If number of moves are odd
if (totalMoves % 2 == 1)
return 'A';
return 'B';
}
// Driver Code
int main()
{
int arr[] = { 5, 6, 7 };
int n = sizeof(arr) / sizeof(arr[0]);
cout << getWinner(arr, n);
return 0;
}
Java
// Java implementation of the approach
class GFG
{
// Function to calculate gcd
static int __gcd(int a, int b)
{
if (b == 0)
return a;
return __gcd(b, a % b);
}
// Function to return the winner
// of the game
static char getWinner(int []arr, int n)
{
// To store the gcd of the
// original array
int gcd = arr[0];
// To store the maximum element
// from the original array
int maxEle = arr[0];
for (int i = 1; i < n; i++)
{
gcd = __gcd(gcd, arr[i]);
maxEle = Math.max(maxEle, arr[i]);
}
int totalMoves = (maxEle / gcd) - n;
// If number of moves are odd
if (totalMoves % 2 == 1)
return 'A';
return 'B';
}
// Driver Code
public static void main(String args[])
{
int []arr = { 5, 6, 7 };
int n = arr.length;
System.out.print(getWinner(arr, n));
}
}
// This code is contributed
// by Akanksha Rai
Python3
# Python3 implementation of the approach
from math import gcd
# Function to return the winner
# of the game
def getWinner(arr, n) :
# To store the gcd of the
# original array
__gcd = arr[0];
# To store the maximum element
# from the original array
maxEle = arr[0];
for i in range(1, n) :
__gcd = gcd(__gcd, arr[i]);
maxEle = max(maxEle, arr[i]);
totalMoves = (maxEle / __gcd) - n;
# If number of moves are odd
if (totalMoves % 2 == 1) :
return 'A';
return 'B';
# Driver Code
if __name__ == "__main__" :
arr = [ 5, 6, 7 ];
n = len(arr)
print(getWinner(arr, n))
# This code is contributed by Ryuga
C#
// C# implementation of the approach
using System;
class GFG
{
// Function to calculate gcd
static int __gcd(int a, int b)
{
if (b == 0)
return a;
return __gcd(b, a % b);
}
// Function to return the winner
// of the game
static char getWinner(int []arr, int n)
{
// To store the gcd of the
// original array
int gcd = arr[0];
// To store the maximum element
// from the original array
int maxEle = arr[0];
for (int i = 1; i < n; i++)
{
gcd = __gcd(gcd, arr[i]);
maxEle = Math.Max(maxEle, arr[i]);
}
int totalMoves = (maxEle / gcd) - n;
// If number of moves are odd
if (totalMoves % 2 == 1)
return 'A';
return 'B';
}
// Driver Code
public static void Main()
{
int []arr = { 5, 6, 7 };
int n = arr.Length;
Console.Write(getWinner(arr, n));
}
}
// This code is contributed
// by Akanksha Rai
PHP
Javascript
B