📜  两球可达性游戏

📅  最后修改于: 2021-10-26 05:49:07             🧑  作者: 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

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