使用给定的操作任意次数检查 X 和 Y 是否可以归零

原文:https://www . geesforgeks . org/check-if-x-and-y-可以通过使用给定操作任意次数来使零/

给定两个整数 XY ,任务是通过使用给定的操作任意多次来检查这两个整数是否可以等于 0。操作描述如下–

  • 选择任意整数 Z
  • 使用以下任一值更新这些值:
    • X = X–2 * Z,Y = Y–3 * Z
    • X = X–3 * Z,Y = Y–2 * Z

例:

输入: X = 6,Y = 9 输出:说明: 运算 1:选择 Z = 3,X = 6–2 * 3 = 0 和 Y = 9–3 * 3 = 0 由于使用 1 运算可以使 X 和 Y 等于 0,所以要求的答案是肯定的。 输入: X = 33,Y = 27 输出:解释: 操作 1:选择 Z = 9,X:= 33–3 * 9 = 6 和 Y:= 27–2 * 9 = 9 操作 2:选择 Z = 3,X:= 6–2 * 3 = 0 和 Y:= 9–3 * 3 = 0 因为 X 和 Y 可以做

方法:假设 X ≤ Y,那么如果满足以下两个条件,答案是肯定的:

  • (X + Y) mod 5 = 0 :因为每次操作后(X + Y) mod 5 的值不变。 我们假设已经选择了某个任意数字 Z 。 因此 (X + Y) 值将变为
((X - 3Z) + (Y - 2Z))
  • 这等于
(X + Y - 5Z)
  • 要使该值等于 0, X + Y = 5Z 。因此,在两边取 mod 时,(X + Y) mod 5 必须等于 0。
  • 3X > = 2Y 这样减法就不会使 X 和 Y 的值为负。

以下是上述方法的实现:

C++

// C++ implementation of the approach

#include <bits/stdc++.h>
using namespace std;

// Function to check if X and Y
// can be made equal to zero by
// using given operation any number of times
void ifPossible(int X, int Y)
{
    if (X > Y)
        swap(X, Y);

    // Check for the two conditions
    if ((X + Y) % 5 == 0 and 3 * X >= 2 * Y)
        cout << "Yes";
    else
        cout << "No";
}

// Driver code
int main()
{
    int X = 33, Y = 27;

    ifPossible(X, Y);

    return 0;
}

Java 语言(一种计算机语言,尤用于创建网站)

// Java implementation of the approach
class GFG
{

// Function to check if X and Y
// can be made equal to zero by
// using given operation any number of times
static void ifPossible(int X, int Y)
{
    if (X > Y)
        swap(X, Y);

    // Check for the two conditions
    if ((X + Y) % 5 == 0 && 3 * X >= 2 * Y)
        System.out.print("Yes");
    else
        System.out.print("No");
}
static void swap(int x, int y)
{
    int temp = x;
    x = y;
    y = temp;
}

// Driver code
public static void main(String[] args)
{
    int X = 33, Y = 27;

    ifPossible(X, Y);
}
}

// This code is contributed by Rajput-Ji

Python 3

# Python3 implementation of the approach

# Function to check if X and Y
# can be made equal to zero by
# using given operation any number of times
def ifPossible(X, Y):
    if (X > Y):
        X, Y = Y, X

    # Check for the two conditions
    if ((X + Y) % 5 == 0 and 3 * X >= 2 * Y):
        print("Yes")
    else:
        print("No")

# Driver code
X = 33
Y = 27

ifPossible(X, Y)

# This code is contributed by mohit kumar 29

C

// C# implementation of the approach
using System;

class GFG
{

// Function to check if X and Y
// can be made equal to zero by
// using given operation any number of times
static void ifPossible(int X, int Y)
{
    if (X > Y)
        swap(X, Y);

    // Check for the two conditions
    if ((X + Y) % 5 == 0 && 3 * X >= 2 * Y)
        Console.WriteLine("Yes");
    else
        Console.WriteLine("No");
}
static void swap(int x, int y)
{
    int temp = x;
    x = y;
    y = temp;
}

// Driver code
public static void Main()
{
    int X = 33, Y = 27;

    ifPossible(X, Y);
}
}

// This code is contributed by Yash_R

java 描述语言

<script>

// Javascript implementation of the approach

// Function to check if X and Y
// can be made equal to zero by
// using given operation any number of times
function ifPossible(X, Y)
{
    if (X > Y)
    {
        var temp = X;
        X = Y;
        Y =temp;
    }

    // Check for the two conditions
    if ((X + Y) % 5 == 0 && 3 * X >= 2 * Y)
        document.write( "Yes");
    else
        document.write( "No");
}

// Driver code
var X = 33, Y = 27;
ifPossible(X, Y);

// This code is contributed by rutvik_56.
</script>

Output: 

Yes

时间复杂度:0(1)

辅助空间:0(1)