📜  两球可达性游戏

📅  最后修改于: 2021-04-23 21:59:02             🧑  作者: Mango

给定A个白色球和B个黑色球。您需要通过执行一些(零次或多次)操作来赢得X个白色球和Y个黑色球(A <= X,B <= Y)才能赢得比赛。
一种操作:在任何时候,如果您有p个白色和q个黑色的球,那么此时您可以购买q个白色或p个黑色的球。
查找是否可能在末尾有X个白色和Y个黑色的球。
例子:

Input:  A = 1, B = 1, X = 3, Y = 8
Output: POSSIBLE

Explanation:
The steps are, (1, 1)->(1, 2)->(3, 2)->(3, 5)->(3, 8)

Input: A = 3, Y = 2, X = 4, Y = 6
Output: NOT POSSIBLE

方法:
我们必须使用gcd的属性来解决此问题。让我们看看如何。

  1. 最初,我们有一个A白色和B黑色的球。我们必须获得XA红球和YB黑球的其余部分。
  2. 以下是我们将使用的两个数字的gcd属性,
gcd(x, y) = gcd(x + y, y)
gcd(x, y) = gcd(x, y + x)
  1. 此属性与问题中提到的操作相同。因此,从这里我们可以得到,如果最终状态的gcd与初始状态的gcd相同,那么总有可能达到目标,否则就没有达到目标。

下面是上述方法的实现:

C++
// C++ program to Find is it possible
// to have X white and Y black
// balls at the end.
#include 
using namespace std;
 
// Recursive function to return
// gcd of a and b
int gcd(int a, int b)
{
    if (b == 0)
        return a;
    return gcd(b, a % b);
}
 
// Function returns if it's 
// possible to have X white
// and Y black balls or not.
void IsPossible(int a, int b,
                int x, int y)
{
 
    // Finding gcd of (x, y)
    // and (a, b)
    int final = gcd(x, y);
    int initial = gcd(a, b);
 
     // If gcd is same, it's always
    // possible to reach (x, y)
    if (initial == final)
    {
        
        cout << "POSSIBLE\n";
    }
    else
    {
        // Here it's never possible
        // if gcd is not same
        cout << "NOT POSSIBLE\n";
    }
}
 
// Driver Code
int main()
{
 
    int A = 1, B = 2, X = 4, Y = 11;
    IsPossible(A, B, X, Y);
 
    A = 2, B = 2, X = 3, Y = 6;
    IsPossible(A, B, X, Y);
 
    return 0;
}


Java
// Java program to Find is it possible
// to have X white and Y black
// balls at the end.
import java.io.*;
 
class GFG{
 
// Recursive function to return
// gcd of a and b
static int gcd(int a, int b)
{
    if (b == 0)
        return a;
    return gcd(b, a % b);
}
 
// Function returns if it's
// possible to have X white
// and Y black balls or not.
static void IsPossible(int a, int b,
                       int x, int y)
{
 
    // Finding gcd of (x, y)
    // and (a, b)
    int g = gcd(x, y);
    int initial = gcd(a, b);
     
    // If gcd is same, it's always
    // possible to reach (x, y)
    if (initial == g)
    {
        System.out.print("POSSIBLE\n");
    }
    else
    {
        // Here it's never possible
        // if gcd is not same
        System.out.print("NOT POSSIBLE\n");
    }
}
 
// Driver code
public static void main(String args[])
{
    int A = 1, B = 2, X = 4, Y = 11;
    IsPossible(A, B, X, Y);
 
    A = 2; B = 2; X = 3; Y = 6;
    IsPossible(A, B, X, Y);
}
}
 
// This code is contributed by shivanisinghss2110


Python3
# Python3 program to find is it possible
# to have X white and Y black
# balls at the end.
 
# Recursive function to return
# gcd of a and b
def gcd(a, b) :
 
    if (b == 0) :
        return a;
    return gcd(b, a % b);
 
 
# Function returns if it's
# possible to have X white
# and Y black balls or not.
def IsPossible(a, b, x, y) :
 
    # Finding gcd of (x, y)
    # and (a, b)
    final = gcd(x, y);
    initial = gcd(a, b);
 
    # If gcd is same, it's always
    # possible to reach (x, y)
    if (initial == final) :
        print("POSSIBLE");
     
    else :
         
        # Here it's never possible
        # if gcd is not same
        print("NOT POSSIBLE");
 
 
# Driver Code
if __name__ == "__main__" :
     
    A = 1; B = 2; X = 4; Y = 11;
    IsPossible(A, B, X, Y);
     
    A = 2; B = 2; X = 3; Y = 6;
    IsPossible(A, B, X, Y);
 
# This code is contributed by AnkitRai01


C#
// C# program to Find is it possible
// to have X white and Y black
// balls at the end.
using System;
using System.Linq;
 
class GFG {
     
// Recursive function to return
// gcd of a and b
static int gcd(int a, int b)
{
    if (b == 0)
        return a;
 
    return gcd(b, a % b);
}
 
// Function returns if it's
// possible to have X white
// and Y black balls or not.
static void IsPossible(int a, int b,
                       int x, int y)
{
     
    // Finding gcd of (x, y)
    // and (a, b)
    int g = gcd(x, y);
    int initial = gcd(a, b);
     
    // If gcd is same, it's always
    // possible to reach (x, y)
    if (initial == g)
    {
        Console.Write("POSSIBLE\n");
    }
    else
    {
         
        // Here it's never possible
        // if gcd is not same
        Console.Write("NOT POSSIBLE\n");
    }
}
 
// Driver code
static public void Main()
{
    int A = 1, B = 2;
    int X = 4, Y = 11;
    IsPossible(A, B, X, Y);
 
    A = 2; B = 2;
    X = 3; Y = 6;
    IsPossible(A, B, X, Y);
}
}
 
// This code is contributed by shivanisinghss2110


Javascript


输出:
POSSIBLE
NOT POSSIBLE